Add diagnostics to SmartThings (#139423)

pull/139436/head^2
Joost Lekkerkerker 2025-02-27 15:23:25 +01:00 committed by GitHub
parent 0da6b28808
commit f677b910a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 1272 additions and 13 deletions

View File

@ -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}

View File

@ -57,19 +57,21 @@ async def trigger_update(
data: dict[str, Any] | None = None,
) -> None:
"""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:
if call[0][0] == device_id and call[0][2] == capability:
call[0][3](
DeviceEvent(
"abc",
"abc",
"abc",
device_id,
MAIN,
capability,
attribute,
value,
data,
)
)
call[0][3](event)
await hass.async_block_till_done()

File diff suppressed because it is too large Load Diff

View File

@ -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")
)