8000 Add diagnostics to Amazon devices (#145964) · home-assistant/core@1899388 · GitHub
[go: up one dir, main page]

Sk 8000 ip to content

Commit 1899388

Browse files
authored
Add diagnostics to Amazon devices (#145964)
1 parent 4d833e9 commit 1899388

File tree

3 files changed

+210
-0
lines changed

3 files changed

+210
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Diagnostics support for Amazon Devices integration."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Any
6+
7+
from aioamazondevices.api import AmazonDevice
8+
9+
from homeassistant.components.diagnostics import async_redact_data
10+
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
11+
from homeassistant.core import HomeAssistant
12+
from homeassistant.helpers.device_registry import DeviceEntry
13+
14+
from .coordinator import AmazonConfigEntry
15+
16+
TO_REDACT = {CONF_PASSWORD, CONF_USERNAME, CONF_NAME, "title"}
17+
18+
19+
async def async_get_config_entry_diagnostics(
20+
hass: HomeAssistant, entry: AmazonConfigEntry
21+
) -> dict[str, Any]:
22+
"""Return diagnostics for a config entry."""
23+
24+
coordinator = entry.runtime_data
25+
26+
devices: list[dict[str, dict[str, Any]]] = [
27+
build_device_data(device) for device in coordinator.data.values()
28+
]
29+
30+
return {
31+
"entry": async_redact_data(entry.as_dict(), TO_REDACT),
32+
"device_info": {
33+
"last_update success": coordinator.last_update_success,
34+
"last_exception": repr(coordinator.last_exception),
35+
"devices": devices,
36+
},
37+
}
38+
39+
40+
async def async_get_device_diagnostics(
41+
hass: HomeAssistant, entry: AmazonConfigEntry, device_entry: DeviceEntry
42+
) -> dict[str, Any]:
43+
"""Return diagnostics for a device."""
44+
45+
coordinator = entry.runtime_data
46+
47+
assert device_entry.serial_number
48+
49+
return build_device_data(coordinator.data[device_entry.serial_number])
50+
51+
52+
def build_device_data(device: AmazonDevice) -> dict[str, Any]:
53+
"""Build device data for diagnostics."""
54+
return {
55+
"account name": device.account_name,
56+
"capabilities": device.capabilities,
57+
"device family": device.device_family,
58+
"device type": device.device_type,
59+
"device cluster members": device.device_cluster_members,
60+
"online": device.online,
61+
"serial number": device.serial_number,
62+
"software version": device.software_version,
63+
"do not disturb": device.do_not_disturb,
64+
"response style": device.response_style,
65+
"bluetooth state": device.bluetooth_state,
66+
}
Lines changed: 74 additions & 0 deletions
F438
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# serializer version: 1
2+
# name: test_device_diagnostics
3+
dict({
4+
'account name': 'Echo Test',
5+
'bluetooth state': True,
6+
'capabilities': list([
7+
'AUDIO_PLAYER',
8+
'MICROPHONE',
9+
]),
10+
'device cluster members': list([
11+
'echo_test_serial_number',
12+
]),
13+
'device family': 'mine',
14+
'device type': 'echo',
15+
'do not disturb': False,
16+
'online': True,
17+
'response style': None,
18+
'serial number': 'echo_test_serial_number',
19+
'software version': 'echo_test_software_version',
20+
})
21+
# ---
22+
# name: test_entry_diagnostics
23+
dict({
24+
'device_info': dict({
25+
'devices': list([
26+
dict({
27+
'account name': 'Echo Test',
28+
'bluetooth state': True,
29+
'capabilities': list([
30+
'AUDIO_PLAYER',
31+
'MICROPHONE',
32+
]),
33+
'device cluster members': list([
34+
'echo_test_serial_number',
35+
]),
36+
'device family': 'mine',
37+
'device type': 'echo',
38+
'do not disturb': False,
39+
'online': True,
40+
'response style': None,
41+
'serial number': 'echo_test_serial_number',
42+
'software version': 'echo_test_software_version',
43+
}),
44+
]),
45+
'last_exception': 'None',
46+
'last_update success': True,
47+
}),
48+
'entry': dict({
49+
'data': dict({
50+
'country': 'IT',
51+
'login_data': dict({
52+
'session': 'test-session',
53+
}),
54+
'password': '**REDACTED**',
55+
'username': '**REDACTED**',
56+
}),
57+
'disabled_by': None,
58+
'discovery_keys': dict({
59+
}),
60+
'domain': 'amazon_devices',
61+
'minor_version': 1,
62+
'options': dict({
63+
}),
64+
'pref_disable_new_entities': False,
65+
'pref_disable_polling': False,
66+
'source': 'user',
67+
'subentries': list([
68+
]),
69+
'title': '**REDACTED**',
70+
'unique_id': 'fake_email@gmail.com',
71+
'version': 1,
72+
}),
73+
})
74+
# ---
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Tests for Amazon Devices diagnostics platform."""
2+
3+
from __future__ import annotations
4+
5+
from unittest.mock import AsyncMock
6+
7+
from syrupy.assertion import SnapshotAssertion
8+
from syrupy.filters import props
9+
10+
from homeassistant.components.amazon_devices.const import DOMAIN
11+
from homeassistant.core import HomeAssistant
12+
from homeassistant.helpers import device_registry as dr
13+
14+
from . import setup_integration
15+
from .const import TEST_SERIAL_NUMBER
16+
17+
from tests.common import MockConfigEntry
18+
from tests.components.diagnostics import (
19+
get_diagnostics_for_config_entry,
20+
get_diagnostics_for_device,
21+
)
22+
from tests.typing import ClientSessionGenerator
23+
24+
25+
async def test_entry_diagnostics(
26+
hass: HomeAssistant,
27+
mock_amazon_devices_client: AsyncMock,
28+
mock_config_entry: MockConfigEntry,
29+
hass_client: ClientSessionGenerator,
30+
snapshot: SnapshotAssertion,
31+
) -> None:
32+
"""Test Amazon config entry diagnostics."""
33+
await setup_integration(hass, mock_config_entry)
34+
35+
assert await get_diagnostics_for_config_entry(
36+
hass, hass_client, mock_config_entry
37+
) == snapshot(
38+
exclude=props(
39+
"entry_id",
40+
"created_at",
41+
"modified_at",
42+
)
43+
)
44+
45+
46+
async def test_device_diagnostics(
47+
hass: HomeAssistant,
48+
mock_amazon_devices_client: AsyncMock,
49+
mock_config_entry: MockConfigEntry,
50+
hass_client: ClientSessionGenerator,
51+
device_registry: dr.DeviceRegistry,
52+
snapshot: SnapshotAssertion,
53+
) -> None:
54+
"""Test Amazon device diagnostics."""
55+
await setup_integration(hass, mock_config_entry)
56+
57+
device = device_registry.async_get_device(
58+
identifiers={(DOMAIN, TEST_SERIAL_NUMBER)}
59+
)
60+
assert device, repr(device_registry.devices)
61+
62+
assert await get_diagnostics_for_device(
63+
hass, hass_client, mock_config_entry, device
64+
) == snapshot(
65+
exclude=props(
66+
"entry_id",
67+
"created_at",
68+
"modified_at",
69+
)
70+
)

0 commit comments

Comments
 (0)
0