mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 11:33:11 -04:00
Add diagnostics to BTHome (#189143)
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
019d233236
commit
91be52a870
@@ -0,0 +1,59 @@
|
||||
"""Diagnostics support for the BTHome integration."""
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components import bluetooth
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from .const import CONF_BINDKEY, CONF_DISCOVERED_EVENT_CLASSES
|
||||
from .types import BTHomeConfigEntry
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: BTHomeConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = entry.runtime_data
|
||||
device_data = coordinator.device_data
|
||||
service_info = bluetooth.async_last_service_info(
|
||||
hass, coordinator.address, connectable=False
|
||||
)
|
||||
return {
|
||||
"config_entry": {
|
||||
"encryption_configured": bool(entry.data.get(CONF_BINDKEY)),
|
||||
"sleepy_device": coordinator.sleepy_device,
|
||||
"discovered_event_classes": sorted(
|
||||
entry.data.get(CONF_DISCOVERED_EVENT_CLASSES, [])
|
||||
),
|
||||
},
|
||||
"devices": [
|
||||
{
|
||||
"manufacturer": device.manufacturer,
|
||||
"model": device.model,
|
||||
}
|
||||
for device in dr.async_entries_for_config_entry(
|
||||
dr.async_get(hass), entry.entry_id
|
||||
)
|
||||
],
|
||||
"device_data": {
|
||||
"bthome_version": device_data.bthome_version.value,
|
||||
"encryption_scheme": device_data.encryption_scheme.value,
|
||||
"bindkey_verified": device_data.bindkey_verified,
|
||||
"decryption_failed": device_data.decryption_failed,
|
||||
"downgrade_detected": device_data.downgrade_detected,
|
||||
"sleepy_device": device_data.sleepy_device,
|
||||
"device_type_id": device_data.device_type_id,
|
||||
"packet_id": device_data.packet_id,
|
||||
"encryption_counter": device_data.encryption_counter,
|
||||
"unhandled": device_data.unhandled,
|
||||
},
|
||||
"last_advertisement": service_info
|
||||
and {
|
||||
"rssi": service_info.rssi,
|
||||
"tx_power": service_info.tx_power,
|
||||
"connectable": service_info.connectable,
|
||||
# Raw service/manufacturer data is omitted as it can embed the MAC address
|
||||
"service_uuids": service_info.service_uuids,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
# serializer version: 1
|
||||
# name: test_diagnostics[encrypted]
|
||||
dict({
|
||||
'config_entry': dict({
|
||||
'discovered_event_classes': list([
|
||||
]),
|
||||
'encryption_configured': True,
|
||||
'sleepy_device': False,
|
||||
}),
|
||||
'device_data': dict({
|
||||
'bindkey_verified': True,
|
||||
'bthome_version': 1,
|
||||
'decryption_failed': False,
|
||||
'device_type_id': None,
|
||||
'downgrade_detected': False,
|
||||
'encryption_counter': 857870592.0,
|
||||
'encryption_scheme': 'bthome_bindkey',
|
||||
'packet_id': None,
|
||||
'sleepy_device': False,
|
||||
'unhandled': dict({
|
||||
}),
|
||||
}),
|
||||
'devices': list([
|
||||
dict({
|
||||
'manufacturer': 'Xiaomi',
|
||||
'model': 'Temperature/Humidity sensor',
|
||||
}),
|
||||
]),
|
||||
'last_advertisement': dict({
|
||||
'connectable': True,
|
||||
'rssi': -56,
|
||||
'service_uuids': list([
|
||||
'0000181e-0000-1000-8000-00805f9b34fb',
|
||||
]),
|
||||
'tx_power': -127,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_diagnostics[mac_in_payload]
|
||||
dict({
|
||||
'config_entry': dict({
|
||||
'discovered_event_classes': list([
|
||||
]),
|
||||
'encryption_configured': False,
|
||||
'sleepy_device': False,
|
||||
}),
|
||||
'device_data': dict({
|
||||
'bindkey_verified': False,
|
||||
'bthome_version': 1,
|
||||
'decryption_failed': True,
|
||||
'device_type_id': None,
|
||||
'downgrade_detected': False,
|
||||
'encryption_counter': 0.0,
|
||||
'encryption_scheme': 'none',
|
||||
'packet_id': None,
|
||||
'sleepy_device': False,
|
||||
'unhandled': dict({
|
||||
}),
|
||||
}),
|
||||
'devices': list([
|
||||
dict({
|
||||
'manufacturer': 'Xiaomi',
|
||||
'model': 'Temperature/Humidity sensor',
|
||||
}),
|
||||
]),
|
||||
'last_advertisement': dict({
|
||||
'connectable': True,
|
||||
'rssi': -56,
|
||||
'service_uuids': list([
|
||||
'0000181c-0000-1000-8000-00805f9b34fb',
|
||||
]),
|
||||
'tx_power': -127,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
# name: test_diagnostics[unencrypted]
|
||||
dict({
|
||||
'config_entry': dict({
|
||||
'discovered_event_classes': list([
|
||||
]),
|
||||
'encryption_configured': False,
|
||||
'sleepy_device': False,
|
||||
}),
|
||||
'device_data': dict({
|
||||
'bindkey_verified': False,
|
||||
'bthome_version': 2,
|
||||
'decryption_failed': True,
|
||||
'device_type_id': None,
|
||||
'downgrade_detected': False,
|
||||
'encryption_counter': 0.0,
|
||||
'encryption_scheme': 'none',
|
||||
'packet_id': 12.0,
|
||||
'sleepy_device': False,
|
||||
'unhandled': dict({
|
||||
}),
|
||||
}),
|
||||
'devices': list([
|
||||
dict({
|
||||
'manufacturer': None,
|
||||
'model': 'BTHome sensor',
|
||||
}),
|
||||
]),
|
||||
'last_advertisement': dict({
|
||||
'connectable': True,
|
||||
'rssi': -56,
|
||||
'service_uuids': list([
|
||||
'0000fcd2-0000-1000-8000-00805f9b34fb',
|
||||
]),
|
||||
'tx_power': -127,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Tests for the diagnostics data provided by the BTHome integration."""
|
||||
|
||||
import pytest
|
||||
from pytest_homeassistant.common import MockConfigEntry
|
||||
from pytest_homeassistant.components.bluetooth import inject_bluetooth_service_info
|
||||
from pytest_homeassistant.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from pytest_homeassistant.typing import ClientSessionGenerator
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
|
||||
from homeassistant.components.bthome.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import INVALID_PAYLOAD, make_bthome_v2_adv, make_encrypted_bthome_v1_adv
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("address", "data", "advertisement"),
|
||||
[
|
||||
pytest.param(
|
||||
"A4:C1:38:8D:18:B2",
|
||||
{},
|
||||
make_bthome_v2_adv(
|
||||
"A4:C1:38:8D:18:B2",
|
||||
b"\x40\x00\x0c\x02\xca\x09\x03\xbf\x13\xf1\x00\x01\x02\x04",
|
||||
),
|
||||
id="unencrypted",
|
||||
),
|
||||
pytest.param(
|
||||
"54:48:E6:8F:80:A5",
|
||||
{"bindkey": "231d39c1d7cc1ab1aee224cd096db932"},
|
||||
make_encrypted_bthome_v1_adv(
|
||||
"54:48:E6:8F:80:A5",
|
||||
b'\xfb\xa45\xe4\xd3\xc3\x12\xfb\x00\x11"3W\xd9\n\x99',
|
||||
),
|
||||
id="encrypted",
|
||||
),
|
||||
pytest.param(
|
||||
"A4:C1:38:56:53:84",
|
||||
{},
|
||||
INVALID_PAYLOAD,
|
||||
id="mac_in_payload",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
hass_client: ClientSessionGenerator,
|
||||
snapshot: SnapshotAssertion,
|
||||
address: str,
|
||||
data: dict[str, str],
|
||||
advertisement: BluetoothServiceInfoBleak,
|
||||
) -> None:
|
||||
"""Test diagnostics for a config entry."""
|
||||
entry = MockConfigEntry(domain=DOMAIN, unique_id=address, data=data)
|
||||
entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
inject_bluetooth_service_info(hass, advertisement)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == snapshot
|
||||
Reference in New Issue
Block a user