diff --git a/homeassistant/components/chef_iq/diagnostics.py b/homeassistant/components/chef_iq/diagnostics.py new file mode 100644 index 000000000000..d54672ca99a7 --- /dev/null +++ b/homeassistant/components/chef_iq/diagnostics.py @@ -0,0 +1,29 @@ +"""Diagnostics support for Chef iQ.""" + +from typing import TYPE_CHECKING, Any + +from homeassistant.components import bluetooth +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.core import HomeAssistant + +from . import ChefIqConfigEntry + +SERVICE_INFO_TO_REDACT = frozenset( + {"address", "advertisement", "device", "name", "source"} +) + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ChefIqConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + address = entry.unique_id + if TYPE_CHECKING: + assert address is not None + service_info = bluetooth.async_last_service_info(hass, address, connectable=False) + return { + "service_info": async_redact_data( + service_info.as_dict() if service_info else None, + SERVICE_INFO_TO_REDACT, + ), + } diff --git a/homeassistant/components/chef_iq/quality_scale.yaml b/homeassistant/components/chef_iq/quality_scale.yaml index bb584377a43a..bf74f8e8a20a 100644 --- a/homeassistant/components/chef_iq/quality_scale.yaml +++ b/homeassistant/components/chef_iq/quality_scale.yaml @@ -68,7 +68,7 @@ rules: test-coverage: done # Gold devices: done - diagnostics: todo + diagnostics: done discovery-update-info: status: exempt comment: | diff --git a/tests/components/chef_iq/snapshots/test_diagnostics.ambr b/tests/components/chef_iq/snapshots/test_diagnostics.ambr new file mode 100644 index 000000000000..f354038ac666 --- /dev/null +++ b/tests/components/chef_iq/snapshots/test_diagnostics.ambr @@ -0,0 +1,26 @@ +# serializer version: 1 +# name: test_diagnostics + dict({ + 'service_info': dict({ + 'address': '**REDACTED**', + 'advertisement': '**REDACTED**', + 'connectable': True, + 'device': '**REDACTED**', + 'manufacturer_data': dict({ + '1485': dict({ + '__type': "", + 'repr': "b'\\x03P\\xc9\\x14e\\xca\\x07\\x9ad \\x1e\\x01\\x03\\x01\\x00z\\xc4'", + }), + }), + 'name': '**REDACTED**', + 'raw': None, + 'rssi': -60, + 'service_data': dict({ + }), + 'service_uuids': list([ + ]), + 'source': '**REDACTED**', + 'tx_power': -127, + }), + }) +# --- diff --git a/tests/components/chef_iq/test_diagnostics.py b/tests/components/chef_iq/test_diagnostics.py new file mode 100644 index 000000000000..210f599d769c --- /dev/null +++ b/tests/components/chef_iq/test_diagnostics.py @@ -0,0 +1,47 @@ +"""Test Chef iQ diagnostics.""" + +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 syrupy.filters import props + +from homeassistant.components.chef_iq.const import DOMAIN +from homeassistant.core import HomeAssistant + +from . import ADDRESS, CHEFIQ_STATUS_SERVICE_INFO, TITLE + + +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + snapshot: SnapshotAssertion, +) -> None: + """Test the diagnostics hold the latest advertisement, redacted.""" + # A user-edited title may hold personal information; it must not be exported. + entry = MockConfigEntry(domain=DOMAIN, unique_id=ADDRESS, title="Backyard smoker") + 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, CHEFIQ_STATUS_SERVICE_INFO) + await hass.async_block_till_done() + + assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == snapshot( + exclude=props("time") + ) + + +async def test_diagnostics_without_advertisement( + hass: HomeAssistant, hass_client: ClientSessionGenerator +) -> None: + """Test the diagnostics of an entry whose probe has not been seen yet.""" + entry = MockConfigEntry(domain=DOMAIN, unique_id=ADDRESS, title=TITLE) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert await get_diagnostics_for_config_entry(hass, hass_client, entry) == { + "service_info": None + }