Add diagnostics to SmartThings (#139423)
parent
0da6b28808
commit
f677b910a6
|
@ -0,0 +1,50 @@
|
||||||
|
"""Diagnostics support for SmartThings."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
from dataclasses import asdict
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from pysmartthings import DeviceEvent
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntry
|
||||||
|
|
||||||
|
from . import SmartThingsConfigEntry
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
EVENT_WAIT_TIME = 5
|
||||||
|
|
||||||
|
|
||||||
|
async def async_get_device_diagnostics(
|
||||||
|
hass: HomeAssistant, entry: SmartThingsConfigEntry, device: DeviceEntry
|
||||||
|
) -> dict[str, Any]:
|
||||||
|
"""Return diagnostics for a device entry."""
|
||||||
|
device_id = next(
|
||||||
|
identifier for identifier in device.identifiers if identifier[0] == DOMAIN
|
||||||
|
)[0]
|
||||||
|
|
||||||
|
events: list[DeviceEvent] = []
|
||||||
|
|
||||||
|
def register_event(event: DeviceEvent) -> None:
|
||||||
|
events.append(event)
|
||||||
|
|
||||||
|
client = entry.runtime_data.client
|
||||||
|
|
||||||
|
listener = client.add_device_event_listener(device_id, register_event)
|
||||||
|
|
||||||
|
await asyncio.sleep(EVENT_WAIT_TIME)
|
||||||
|
|
||||||
|
listener()
|
||||||
|
|
||||||
|
device_status = await client.get_device_status(device_id)
|
||||||
|
|
||||||
|
status: dict[str, Any] = {}
|
||||||
|
for component, capabilities in device_status.items():
|
||||||
|
status[component] = {}
|
||||||
|
for capability, attributes in capabilities.items():
|
||||||
|
status[component][capability] = {}
|
||||||
|
for attribute, value in attributes.items():
|
||||||
|
status[component][capability][attribute] = asdict(value)
|
||||||
|
return {"events": [asdict(event) for event in events], "status": status}
|
|
@ -57,19 +57,21 @@ async def trigger_update(
|
||||||
data: dict[str, Any] | None = None,
|
data: dict[str, Any] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Trigger an update."""
|
"""Trigger an update."""
|
||||||
|
event = DeviceEvent(
|
||||||
|
"abc",
|
||||||
|
"abc",
|
||||||
|
"abc",
|
||||||
|
device_id,
|
||||||
|
MAIN,
|
||||||
|
capability,
|
||||||
|
attribute,
|
||||||
|
value,
|
||||||
|
data,
|
||||||
|
)
|
||||||
|
for call in mock.add_device_event_listener.call_args_list:
|
||||||
|
if call[0][0] == device_id:
|
||||||
|
call[0][3](event)
|
||||||
for call in mock.add_device_capability_event_listener.call_args_list:
|
for call in mock.add_device_capability_event_listener.call_args_list:
|
||||||
if call[0][0] == device_id and call[0][2] == capability:
|
if call[0][0] == device_id and call[0][2] == capability:
|
||||||
call[0][3](
|
call[0][3](event)
|
||||||
DeviceEvent(
|
|
||||||
"abc",
|
|
||||||
"abc",
|
|
||||||
"abc",
|
|
||||||
device_id,
|
|
||||||
MAIN,
|
|
||||||
capability,
|
|
||||||
attribute,
|
|
||||||
value,
|
|
||||||
data,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,44 @@
|
||||||
|
"""Test SmartThings diagnostics."""
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from syrupy import SnapshotAssertion
|
||||||
|
from syrupy.filters import props
|
||||||
|
|
||||||
|
from homeassistant.components.smartthings.const import DOMAIN
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
|
from . import setup_integration
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
from tests.components.diagnostics import get_diagnostics_for_device
|
||||||
|
from tests.typing import ClientSessionGenerator
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||||
|
async def test_device(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
hass_client: ClientSessionGenerator,
|
||||||
|
device_registry: dr.DeviceRegistry,
|
||||||
|
devices: AsyncMock,
|
||||||
|
mock_smartthings: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
) -> None:
|
||||||
|
"""Test generating diagnostics for a device entry."""
|
||||||
|
await setup_integration(hass, mock_config_entry)
|
||||||
|
|
||||||
|
device = device_registry.async_get_device(
|
||||||
|
identifiers={(DOMAIN, "96a5ef74-5832-a84b-f1f7-ca799957065d")}
|
||||||
|
)
|
||||||
|
|
||||||
|
with patch("homeassistant.components.smartthings.diagnostics.EVENT_WAIT_TIME", 0.1):
|
||||||
|
diag = await get_diagnostics_for_device(
|
||||||
|
hass, hass_client, mock_config_entry, device
|
||||||
|
)
|
||||||
|
|
||||||
|
assert diag == snapshot(
|
||||||
|
exclude=props("last_changed", "last_reported", "last_updated")
|
||||||
|
)
|
Loading…
Reference in New Issue