コード例 #1
0
ファイル: diagnostics.py プロジェクト: yangbo1979/core
def _async_get_diagnostics(
        hass: HomeAssistant,
        entry: ConfigEntry,
        device: DeviceEntry | None = None) -> dict[str, Any]:
    """Return diagnostics for a config entry."""
    hkid = entry.data["AccessoryPairingID"]
    connection: HKDevice = hass.data[KNOWN_DEVICES][hkid]

    data = {
        "config-entry": {
            "title": entry.title,
            "version": entry.version,
            "data": async_redact_data(entry.data, REDACTED_CONFIG_ENTRY_KEYS),
        }
    }

    # This is the raw data as returned by homekit
    # It is roughly equivalent to what is in .storage/homekit_controller-entity-map
    # But it also has the latest values seen by the polling or events
    data["entity-map"] = accessories = connection.entity_map.serialize()

    # It contains serial numbers, which we should strip out
    for accessory in accessories:
        for service in accessory.get("services", []):
            for char in service.get("characteristics", []):
                try:
                    normalized = CharacteristicsTypes.get_uuid(char["type"])
                except KeyError:
                    normalized = char["type"]

                if normalized in REDACTED_CHARACTERISTICS:
                    char["value"] = REDACTED

    if device:
        data["device"] = _async_get_diagnostics_for_device(hass, device)
    else:
        device_registry = dr.async_get(hass)

        devices = data["devices"] = []
        for device_id in connection.devices.values():
            device = device_registry.async_get(device_id)
            devices.append(_async_get_diagnostics_for_device(hass, device))

    return data
def test_get_short():
    assert CharacteristicsTypes.get_short(CharacteristicsTypes.ON) == "on"
    assert (
        CharacteristicsTypes.get_short(
            CharacteristicsTypes.get_uuid(CharacteristicsTypes.ON)
        )
        == "on"
    )
    assert (
        CharacteristicsTypes.get_short(CharacteristicsTypes.DOOR_STATE_TARGET)
        == "door-state.target"
    )
    assert (
        CharacteristicsTypes.get_short(CharacteristicsTypes.AIR_PURIFIER_STATE_CURRENT)
        == "air-purifier.state.current"
    )
    assert CharacteristicsTypes.get_short("1a") == "lock-management.auto-secure-timeout"
def test_get_short_uuid_full_uuid():
    assert "6D" == CharacteristicsTypes.get_short_uuid(
        "0000006D-0000-1000-8000-0026BB765291"
    )
def test_get_uuid_unknown_2():
    with pytest.raises(KeyError):
        CharacteristicsTypes.get_uuid("UNKNOWN")
def test_get_uuid_name():
    assert "0000006D-0000-1000-8000-0026BB765291" == CharacteristicsTypes.get_uuid(
        "public.hap.characteristic.position.current"
    )
def test_get_uuid_unknown():
    with pytest.raises(KeyError):
        CharacteristicsTypes.get_uuid("XXX")
def test_get_uuid_reverse():
    assert (
        CharacteristicsTypes.get_uuid("public.hap.characteristic.on")
        == "00000025-0000-1000-8000-0026BB765291"
    )
def test_get_uuid_forward():
    assert (
        CharacteristicsTypes.get_uuid(CharacteristicsTypes.ON)
        == "00000025-0000-1000-8000-0026BB765291"
    )
def test_get_short_unknown():
    assert "Unknown Characteristic 1234" == CharacteristicsTypes.get_short("1234")
def test_get_short_short_uuid():
    assert "position.current" == CharacteristicsTypes.get_short("6D")
def test_get_short_full_uuid():
    assert "position.current" == CharacteristicsTypes.get_short(
        "0000006D-0000-1000-8000-0026BB765291"
    )
def test_get_short_uuid_passthrough():
    assert (
        "0000006D-1234-1234-1234-012345678901"
        == CharacteristicsTypes.get_short_uuid("0000006D-1234-1234-1234-012345678901")
    )
def test_get_short_uuid_short():
    assert "6D" == CharacteristicsTypes.get_short_uuid("6D")
def test_get_short_uuid_name():
    assert "6D" == CharacteristicsTypes.get_short_uuid(
        "public.hap.characteristic.position.current"
    )
コード例 #15
0
ファイル: diagnostics.py プロジェクト: yangbo1979/core
from typing import Any

from aiohomekit.model.characteristics.characteristic_types import CharacteristicsTypes

from homeassistant.components.diagnostics import REDACTED, async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import DeviceEntry

from .connection import HKDevice
from .const import KNOWN_DEVICES

REDACTED_CHARACTERISTICS = [
    CharacteristicsTypes.get_uuid(CharacteristicsTypes.SERIAL_NUMBER),
]

REDACTED_CONFIG_ENTRY_KEYS = [
    "AccessoryIP",
    "iOSDeviceLTSK",
]

REDACTED_STATE = ["access_token", "entity_picture"]


async def async_get_config_entry_diagnostics(
        hass: HomeAssistant, entry: ConfigEntry) -> dict[str, Any]:
    """Return diagnostics for a config entry."""
    return _async_get_diagnostics(hass, entry)