Skip to content

Commit 415c8b4

Browse files
epenetCopilot
andauthored
Add device diagnostics to onewire (home-assistant#154617)
Co-authored-by: Copilot <[email protected]>
1 parent 6038f15 commit 415c8b4

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

homeassistant/components/onewire/diagnostics.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from homeassistant.components.diagnostics import async_redact_data
99
from homeassistant.const import CONF_HOST
1010
from homeassistant.core import HomeAssistant
11+
from homeassistant.helpers import device_registry as dr
1112

1213
from .onewirehub import OneWireConfigEntry
1314

@@ -26,7 +27,28 @@ async def async_get_config_entry_diagnostics(
2627
"data": async_redact_data(entry.data, TO_REDACT),
2728
"options": {**entry.options},
2829
},
29-
"devices": [asdict(device_details) for device_details in onewire_hub.devices]
30-
if onewire_hub.devices
31-
else [],
30+
"devices": [asdict(device_details) for device_details in onewire_hub.devices],
31+
}
32+
33+
34+
async def async_get_device_diagnostics(
35+
hass: HomeAssistant, entry: OneWireConfigEntry, device_entry: dr.DeviceEntry
36+
) -> dict[str, Any]:
37+
"""Return diagnostics for a device."""
38+
39+
onewire_hub = entry.runtime_data
40+
41+
return {
42+
"entry": {
43+
"title": entry.title,
44+
"data": async_redact_data(entry.data, TO_REDACT),
45+
"options": {**entry.options},
46+
},
47+
"device": asdict(
48+
next(
49+
device_details
50+
for device_details in onewire_hub.devices
51+
if device_details.id[3:] == device_entry.serial_number
52+
)
53+
),
3254
}

homeassistant/components/onewire/quality_scale.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ rules:
5959
comment: >
6060
Manual removal, as it is not possible to distinguish
6161
between a flaky device and a device that has been removed
62-
diagnostics:
63-
status: todo
64-
comment: config-entry diagnostics level available, might be nice to have device-level diagnostics
62+
diagnostics: done
6563
exception-translations:
6664
status: todo
6765
comment: Under review

tests/components/onewire/snapshots/test_diagnostics.ambr

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
# serializer version: 1
2+
# name: test_device_diagnostics[EF.111111111113]
3+
dict({
4+
'device': dict({
5+
'device_info': dict({
6+
'identifiers': list([
7+
list([
8+
'onewire',
9+
'EF.111111111113',
10+
]),
11+
]),
12+
'manufacturer': 'Hobby Boards',
13+
'model': None,
14+
'model_id': 'HB_HUB',
15+
'name': 'EF.111111111113',
16+
'serial_number': '111111111113',
17+
'sw_version': '3.2',
18+
}),
19+
'family': 'EF',
20+
'id': 'EF.111111111113',
21+
'path': '/EF.111111111113/',
22+
'type': 'HB_HUB',
23+
}),
24+
'entry': dict({
25+
'data': dict({
26+
'host': '**REDACTED**',
27+
'port': 1234,
28+
}),
29+
'options': dict({
30+
'device_options': dict({
31+
'28.222222222222': dict({
32+
'precision': 'temperature9',
33+
}),
34+
'28.222222222223': dict({
35+
'precision': 'temperature5',
36+
}),
37+
}),
38+
}),
39+
'title': 'Mock Title',
40+
}),
41+
})
42+
# ---
243
# name: test_entry_diagnostics[EF.111111111113]
344
dict({
445
'devices': list([

tests/components/onewire/test_diagnostics.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
import pytest
77
from syrupy.assertion import SnapshotAssertion
88

9+
from homeassistant.components.onewire import DOMAIN
910
from homeassistant.const import Platform
1011
from homeassistant.core import HomeAssistant
12+
from homeassistant.helpers import device_registry as dr
1113

1214
from . import setup_owproxy_mock_devices
1315

1416
from tests.common import MockConfigEntry
15-
from tests.components.diagnostics import get_diagnostics_for_config_entry
17+
from tests.components.diagnostics import (
18+
get_diagnostics_for_config_entry,
19+
get_diagnostics_for_device,
20+
)
1621
from tests.typing import ClientSessionGenerator
1722

1823

@@ -41,3 +46,27 @@ async def test_entry_diagnostics(
4146
await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
4247
== snapshot
4348
)
49+
50+
51+
@pytest.mark.parametrize("device_id", ["EF.111111111113"], indirect=True)
52+
async def test_device_diagnostics(
53+
hass: HomeAssistant,
54+
config_entry: MockConfigEntry,
55+
device_registry: dr.DeviceRegistry,
56+
hass_client: ClientSessionGenerator,
57+
owproxy: MagicMock,
58+
device_id: str,
59+
snapshot: SnapshotAssertion,
60+
) -> None:
61+
"""Test device diagnostics."""
62+
setup_owproxy_mock_devices(owproxy, [device_id])
63+
await hass.config_entries.async_setup(config_entry.entry_id)
64+
await hass.async_block_till_done()
65+
66+
device = device_registry.async_get_device(identifiers={(DOMAIN, "EF.111111111113")})
67+
assert device is not None
68+
69+
assert (
70+
await get_diagnostics_for_device(hass, hass_client, config_entry, device)
71+
== snapshot
72+
)

0 commit comments

Comments
 (0)