diff --git a/homeassistant/components/okok_scale/diagnostics.py b/homeassistant/components/okok_scale/diagnostics.py new file mode 100644 index 000000000000..4d03f8ad324a --- /dev/null +++ b/homeassistant/components/okok_scale/diagnostics.py @@ -0,0 +1,28 @@ +"""Diagnostics support for the OKOK Scale integration.""" + +from typing import Any + +from homeassistant.components import bluetooth +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.core import HomeAssistant + +from . import OKOKScaleConfigEntry + +SERVICE_INFO_TO_REDACT = frozenset( + {"address", "advertisement", "device", "name", "source"} +) + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: OKOKScaleConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + address = entry.unique_id + 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/okok_scale/manifest.json b/homeassistant/components/okok_scale/manifest.json index 0a8b038d388f..7ece0cbc326d 100644 --- a/homeassistant/components/okok_scale/manifest.json +++ b/homeassistant/components/okok_scale/manifest.json @@ -28,6 +28,6 @@ "integration_type": "device", "iot_class": "local_push", "loggers": ["okokscale"], - "quality_scale": "silver", + "quality_scale": "platinum", "requirements": ["okokscale==0.0.4"] } diff --git a/homeassistant/components/okok_scale/quality_scale.yaml b/homeassistant/components/okok_scale/quality_scale.yaml index 57b6115437a9..c0b01370565c 100644 --- a/homeassistant/components/okok_scale/quality_scale.yaml +++ b/homeassistant/components/okok_scale/quality_scale.yaml @@ -64,7 +64,7 @@ rules: # Gold devices: done - diagnostics: todo + diagnostics: done discovery-update-info: status: exempt comment: This integration uses Bluetooth and addresses don't change. diff --git a/tests/components/okok_scale/snapshots/test_diagnostics.ambr b/tests/components/okok_scale/snapshots/test_diagnostics.ambr new file mode 100644 index 000000000000..298f4b6b3a89 --- /dev/null +++ b/tests/components/okok_scale/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({ + '61695': dict({ + '__type': "", + 'repr': "b'\\x02\\x04T\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00P\\xfb\\x19\\x01#E'", + }), + }), + 'name': '**REDACTED**', + 'raw': None, + 'rssi': -60, + 'service_data': dict({ + }), + 'service_uuids': list([ + ]), + 'source': '**REDACTED**', + 'tx_power': -127, + }), + }) +# --- diff --git a/tests/components/okok_scale/test_diagnostics.py b/tests/components/okok_scale/test_diagnostics.py new file mode 100644 index 000000000000..d64c84a45c1b --- /dev/null +++ b/tests/components/okok_scale/test_diagnostics.py @@ -0,0 +1,35 @@ +"""Tests for the diagnostics data provided by the OKOK Scale integration.""" + +import pytest +from pytest_homeassistant.common import MockConfigEntry +from pytest_homeassistant.components.bluetooth import ( + inject_bluetooth_service_info_bleak, +) +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.core import HomeAssistant + +from . import OKOK_F0_SERVICE_INFO + + +@pytest.mark.usefixtures("mock_bluetooth") +async def test_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, +) -> None: + """Test diagnostics for a config entry.""" + mock_config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + inject_bluetooth_service_info_bleak(hass, OKOK_F0_SERVICE_INFO) + await hass.async_block_till_done(wait_background_tasks=True) + + assert await get_diagnostics_for_config_entry( + hass, hass_client, mock_config_entry + ) == snapshot(exclude=props("created_at", "modified_at", "entry_id", "time"))