-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Add diagnostics to Amazon devices #145964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edenhaus
merged 2 commits into
home-assistant:dev
from
chemelli74:chemelli74-amazon-devices-diagnostic
Jun 2, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Diagnostics support for Amazon Devices integration.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from aioamazondevices.api import AmazonDevice | ||
|
||
from homeassistant.components.diagnostics import async_redact_data | ||
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.device_registry import DeviceEntry | ||
|
||
from .coordinator import AmazonConfigEntry | ||
|
||
TO_REDACT = {CONF_PASSWORD, CONF_USERNAME, CONF_NAME, "title"} | ||
|
||
|
||
async def async_get_config_entry_diagnostics( | ||
hass: HomeAssistant, entry: AmazonConfigEntry | ||
) -> dict[str, Any]: | ||
"""Return diagnostics for a config entry.""" | ||
|
||
coordinator = entry.runtime_data | ||
|
||
devices: list[dict[str, dict[str, Any]]] = [ | ||
build_device_data(device) for device in coordinator.data.values() | ||
] | ||
|
||
return { | ||
"entry": async_redact_data(entry.as_dict(), TO_REDACT), | ||
"device_info": { | ||
"last_update success": coordinator.last_update_success, | ||
"last_exception": repr(coordinator.last_exception), | ||
"devices": devices, | ||
}, | ||
} | ||
|
||
|
||
async def async_get_device_diagnostics( | ||
hass: HomeAssistant, entry: AmazonConfigEntry, device_entry: DeviceEntry | ||
) -> dict[str, Any]: | ||
"""Return diagnostics for a device.""" | ||
|
||
coordinator = entry.runtime_data | ||
|
||
assert device_entry.serial_number | ||
|
||
return build_device_data(coordinator.data[device_entry.serial_number]) | ||
|
||
|
||
def build_device_data(device: AmazonDevice) -> dict[str, Any]: | ||
"""Build device data for diagnostics.""" | ||
return { | ||
"account name": device.account_name, | ||
"capabilities": device.capabilities, | ||
"device family": device.device_family, | ||
"device type": device.device_type, | ||
"device cluster members": device.device_cluster_members, | ||
"online": device.online, | ||
"serial number": device.serial_number, | ||
"software version": device.software_version, | ||
"do not disturb": device.do_not_disturb, | ||
"response style": device.response_style, | ||
"bluetooth state": device.bluetooth_state, | ||
} |
74 changes: 74 additions & 0 deletions
74
tests/components/amazon_devices/snapshots/test_diagnostics.ambr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# serializer version: 1 | ||
# name: test_device_diagnostics | ||
dict({ | ||
'account name': 'Echo Test', | ||
'bluetooth state': True, | ||
'capabilities': list([ | ||
'AUDIO_PLAYER', | ||
'MICROPHONE', | ||
]), | ||
'device cluster members': list([ | ||
'echo_test_serial_number', | ||
]), | ||
'device family': 'mine', | ||
'device type': 'echo', | ||
'do not disturb': False, | ||
'online': True, | ||
'response style': None, | ||
'serial number': 'echo_test_serial_number', | ||
'software version': 'echo_test_software_version', | ||
}) | ||
# --- | ||
# name: test_entry_diagnostics | ||
dict({ | ||
'device_info': dict({ | ||
'devices': list([ | ||
dict({ | ||
'account name': 'Echo Test', | ||
'bluetooth state': True, | ||
'capabilities': list([ | ||
'AUDIO_PLAYER', | ||
'MICROPHONE', | ||
]), | ||
'device cluster members': list([ | ||
'echo_test_serial_number', | ||
]), | ||
'device family': 'mine', | ||
'device type': 'echo', | ||
'do not disturb': False, | ||
'online': True, | ||
'response style': None, | ||
'serial number': 'echo_test_serial_number', | ||
'software version': 'echo_test_software_version', | ||
}), | ||
]), | ||
'last_exception': 'None', | ||
'last_update success': True, | ||
}), | ||
'entry': dict({ | ||
'data': dict({ | ||
'country': 'IT', | ||
'login_data': dict({ | ||
'session': 'test-session', | ||
}), | ||
'password': '**REDACTED**', | ||
'username': '**REDACTED**', | ||
}), | ||
'disabled_by': None, | ||
'discovery_keys': dict({ | ||
}), | ||
'domain': 'amazon_devices', | ||
'minor_version': 1, | ||
'options': dict({ | ||
}), | ||
'pref_disable_new_entities': False, | ||
'pref_disable_polling': False, | ||
'source': 'user', | ||
'subentries': list([ | ||
]), | ||
'title': '**REDACTED**', | ||
'unique_id': 'fake_email@gmail.com', | ||
'version': 1, | ||
}), | ||
}) | ||
# --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Tests for Amazon Devices diagnostics platform.""" | ||
|
||
from __future__ import annotations | ||
|
||
from unittest.mock import AsyncMock | ||
|
||
from syrupy.assertion import SnapshotAssertion | ||
from syrupy.filters import props | ||
|
||
from homeassistant.components.amazon_devices.const import DOMAIN | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import device_registry as dr | ||
|
||
from . import setup_integration | ||
from .const import TEST_SERIAL_NUMBER | ||
|
||
from tests.common import MockConfigEntry | ||
from tests.components.diagnostics import ( | ||
get_diagnostics_for_config_entry, | ||
get_diagnostics_for_device, | ||
) | ||
from tests.typing import ClientSessionGenerator | ||
|
||
|
||
async def test_entry_diagnostics( | ||
hass: HomeAssistant, | ||
mock_amazon_devices_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
hass_client: ClientSessionGenerator, | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
"""Test Amazon config entry diagnostics.""" | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
assert await get_diagnostics_for_config_entry( | ||
hass, hass_client, mock_config_entry | ||
) == snapshot( | ||
exclude=props( | ||
"entry_id", | ||
"created_at", | ||
"modified_at", | ||
) | ||
) | ||
|
||
|
||
async def test_device_diagnostics( | ||
hass: HomeAssistant, | ||
mock_amazon_devices_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
hass_client: ClientSessionGenerator, | ||
device_registry: dr.DeviceRegistry, | ||
snapshot: SnapshotAssertion, | ||
) -> None: | ||
"""Test Amazon device diagnostics.""" | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
device = device_registry.async_get_device( | ||
identifiers={(DOMAIN, TEST_SERIAL_NUMBER)} | ||
) | ||
assert device, repr(device_registry.devices) | ||
|
||
assert await get_diagnostics_for_device( | ||
hass, hass_client, mock_config_entry, device | ||
) == snapshot( | ||
exclude=props( | ||
"entry_id", | ||
"created_at", | ||
"modified_at", | ||
) | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions
2C79
cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.