From b54722952c79f34e30fd1f791aa6b1e9117b457b Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Mon, 24 Jan 2022 08:15:45 -0700 Subject: [PATCH] Add diagnostics to Notion (#64713) --- .../components/notion/diagnostics.py | 32 +++++ tests/components/notion/conftest.py | 41 ++++++- .../notion/fixtures/bridge_data.json | 26 ++++ .../notion/fixtures/sensor_data.json | 70 +++++++++++ .../components/notion/fixtures/task_data.json | 86 +++++++++++++ tests/components/notion/test_diagnostics.py | 113 ++++++++++++++++++ 6 files changed, 362 insertions(+), 6 deletions(-) create mode 100644 homeassistant/components/notion/diagnostics.py create mode 100644 tests/components/notion/fixtures/bridge_data.json create mode 100644 tests/components/notion/fixtures/sensor_data.json create mode 100644 tests/components/notion/fixtures/task_data.json create mode 100644 tests/components/notion/test_diagnostics.py diff --git a/homeassistant/components/notion/diagnostics.py b/homeassistant/components/notion/diagnostics.py new file mode 100644 index 00000000000..051ffde412b --- /dev/null +++ b/homeassistant/components/notion/diagnostics.py @@ -0,0 +1,32 @@ +"""Diagnostics support for Notion.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.components.diagnostics import async_redact_data +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_USERNAME +from homeassistant.core import HomeAssistant +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + +from .const import DOMAIN + +CONF_DEVICE_KEY = "device_key" + +TO_REDACT = { + CONF_DEVICE_KEY, + CONF_EMAIL, + CONF_PASSWORD, + CONF_USERNAME, +} + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + coordinator: DataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] + + return { + "data": async_redact_data(coordinator.data, TO_REDACT), + } diff --git a/tests/components/notion/conftest.py b/tests/components/notion/conftest.py index 18ff4d0264e..77efdacf943 100644 --- a/tests/components/notion/conftest.py +++ b/tests/components/notion/conftest.py @@ -1,5 +1,6 @@ """Define fixtures for Notion tests.""" -from unittest.mock import patch +import json +from unittest.mock import AsyncMock, patch import pytest @@ -7,7 +8,17 @@ from homeassistant.components.notion import DOMAIN from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.setup import async_setup_component -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, load_fixture + + +@pytest.fixture(name="client") +def client_fixture(data_bridge, data_sensor, data_task): + """Define a fixture for an aionotion client.""" + client = AsyncMock() + client.bridge.async_all.return_value = data_bridge + client.sensor.async_all.return_value = data_sensor + client.task.async_all.return_value = data_task + return client @pytest.fixture(name="config_entry") @@ -27,12 +38,30 @@ def config_fixture(hass): } +@pytest.fixture(name="data_bridge", scope="session") +def data_bridge_fixture(): + """Define bridge data.""" + return json.loads(load_fixture("bridge_data.json", "notion")) + + +@pytest.fixture(name="data_sensor", scope="session") +def data_sensor_fixture(): + """Define sensor data.""" + return json.loads(load_fixture("sensor_data.json", "notion")) + + +@pytest.fixture(name="data_task", scope="session") +def data_task_fixture(): + """Define task data.""" + return json.loads(load_fixture("task_data.json", "notion")) + + @pytest.fixture(name="setup_notion") -async def setup_notion_fixture(hass, config): +async def setup_notion_fixture(hass, client, config): """Define a fixture to set up Notion.""" - with patch("homeassistant.components.notion.async_get_client"), patch( - "homeassistant.components.notion.config_flow.async_get_client" - ), patch("homeassistant.components.notion.PLATFORMS", []): + with patch("homeassistant.components.notion.config_flow.async_get_client"), patch( + "homeassistant.components.notion.PLATFORMS", [] + ), patch("homeassistant.components.notion.async_get_client", return_value=client): assert await async_setup_component(hass, DOMAIN, config) await hass.async_block_till_done() yield diff --git a/tests/components/notion/fixtures/bridge_data.json b/tests/components/notion/fixtures/bridge_data.json new file mode 100644 index 00000000000..c865dd18bb3 --- /dev/null +++ b/tests/components/notion/fixtures/bridge_data.json @@ -0,0 +1,26 @@ +[ + { + "id": 12345, + "name": null, + "mode": "home", + "hardware_id": "0x1234567890abcdef", + "hardware_revision": 4, + "firmware_version": { + "wifi": "0.121.0", + "wifi_app": "3.3.0", + "silabs": "1.0.1" + }, + "missing_at": null, + "created_at": "2019-04-30T01:43:50.497Z", + "updated_at": "2019-04-30T01:44:43.749Z", + "system_id": 12345, + "firmware": { + "wifi": "0.121.0", + "wifi_app": "3.3.0", + "silabs": "1.0.1" + }, + "links": { + "system": 12345 + } + } +] diff --git a/tests/components/notion/fixtures/sensor_data.json b/tests/components/notion/fixtures/sensor_data.json new file mode 100644 index 00000000000..e631f856207 --- /dev/null +++ b/tests/components/notion/fixtures/sensor_data.json @@ -0,0 +1,70 @@ +[ + { + "id": 123456, + "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "user": { + "id": 12345, + "email": "user@email.com" + }, + "bridge": { + "id": 12345, + "hardware_id": "0x1234567890abcdef" + }, + "last_bridge_hardware_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "name": "Bathroom Sensor", + "location_id": 123456, + "system_id": 12345, + "hardware_id": "0x1234567890abcdef", + "firmware_version": "1.1.2", + "hardware_revision": 5, + "device_key": "0x1234567890abcdef", + "encryption_key": true, + "installed_at": "2019-04-30T01:57:34.443Z", + "calibrated_at": "2019-04-30T01:57:35.651Z", + "last_reported_at": "2019-04-30T02:20:04.821Z", + "missing_at": null, + "updated_at": "2019-04-30T01:57:36.129Z", + "created_at": "2019-04-30T01:56:45.932Z", + "signal_strength": 5, + "links": { + "location": 123456 + }, + "lqi": 0, + "rssi": -46, + "surface_type": null + }, + { + "id": 132462, + "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "user": { + "id": 12345, + "email": "user@email.com" + }, + "bridge": { + "id": 12345, + "hardware_id": "0x1234567890abcdef" + }, + "last_bridge_hardware_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "name": "Living Room Sensor", + "location_id": 123456, + "system_id": 12345, + "hardware_id": "0x1234567890abcdef", + "firmware_version": "1.1.2", + "hardware_revision": 5, + "device_key": "0x1234567890abcdef", + "encryption_key": true, + "installed_at": "2019-04-30T01:45:56.169Z", + "calibrated_at": "2019-04-30T01:46:06.256Z", + "last_reported_at": "2019-04-30T02:20:04.829Z", + "missing_at": null, + "updated_at": "2019-04-30T01:46:07.717Z", + "created_at": "2019-04-30T01:45:14.148Z", + "signal_strength": 5, + "links": { + "location": 123456 + }, + "lqi": 0, + "rssi": -30, + "surface_type": null + } +] diff --git a/tests/components/notion/fixtures/task_data.json b/tests/components/notion/fixtures/task_data.json new file mode 100644 index 00000000000..a56d734fb77 --- /dev/null +++ b/tests/components/notion/fixtures/task_data.json @@ -0,0 +1,86 @@ +[ + { + "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "task_type": "missing", + "sensor_data": [], + "status": { + "value": "not_missing", + "received_at": "2020-11-11T21:18:06.613Z" + }, + "created_at": "2020-11-11T21:18:06.613Z", + "updated_at": "2020-11-11T21:18:06.617Z", + "sensor_id": 525993, + "model_version": "2.0", + "configuration": {}, + "links": { + "sensor": 525993 + } + }, + { + "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "task_type": "leak", + "sensor_data": [], + "status": { + "insights": { + "primary": { + "from_state": null, + "to_state": "no_leak", + "data_received_at": "2020-11-11T21:19:13.755Z", + "origin": {} + } + } + }, + "created_at": "2020-11-11T21:19:13.755Z", + "updated_at": "2020-11-11T21:19:13.764Z", + "sensor_id": 525993, + "model_version": "2.1", + "configuration": {}, + "links": { + "sensor": 525993 + } + }, + { + "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "task_type": "temperature", + "sensor_data": [], + "status": { + "value": "20.991287231445312", + "received_at": "2021-01-27T15:18:49.996Z" + }, + "created_at": "2020-11-11T21:19:13.856Z", + "updated_at": "2020-11-11T21:19:13.865Z", + "sensor_id": 525993, + "model_version": "2.1", + "configuration": { + "lower": 15.56, + "upper": 29.44, + "offset": 0 + }, + "links": { + "sensor": 525993 + } + }, + { + "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "task_type": "low_battery", + "sensor_data": [], + "status": { + "insights": { + "primary": { + "from_state": null, + "to_state": "high", + "data_received_at": "2020-11-17T18:40:27.024Z", + "origin": {} + } + } + }, + "created_at": "2020-11-17T18:40:27.024Z", + "updated_at": "2020-11-17T18:40:27.033Z", + "sensor_id": 525993, + "model_version": "4.1", + "configuration": {}, + "links": { + "sensor": 525993 + } + } +] diff --git a/tests/components/notion/test_diagnostics.py b/tests/components/notion/test_diagnostics.py new file mode 100644 index 00000000000..5e6498d3cbc --- /dev/null +++ b/tests/components/notion/test_diagnostics.py @@ -0,0 +1,113 @@ +"""Test Notion diagnostics.""" +from homeassistant.components.diagnostics import REDACTED + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_entry_diagnostics(hass, config_entry, hass_client, setup_notion): + """Test config entry diagnostics.""" + assert await get_diagnostics_for_config_entry(hass, hass_client, config_entry) == { + "data": { + "bridges": { + "12345": { + "id": 12345, + "name": None, + "mode": "home", + "hardware_id": "0x1234567890abcdef", + "hardware_revision": 4, + "firmware_version": { + "wifi": "0.121.0", + "wifi_app": "3.3.0", + "silabs": "1.0.1", + }, + "missing_at": None, + "created_at": "2019-04-30T01:43:50.497Z", + "updated_at": "2019-04-30T01:44:43.749Z", + "system_id": 12345, + "firmware": { + "wifi": "0.121.0", + "wifi_app": "3.3.0", + "silabs": "1.0.1", + }, + "links": {"system": 12345}, + } + }, + "sensors": { + "123456": { + "id": 123456, + "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "user": {"id": 12345, "email": REDACTED}, + "bridge": {"id": 12345, "hardware_id": "0x1234567890abcdef"}, + "last_bridge_hardware_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "name": "Bathroom Sensor", + "location_id": 123456, + "system_id": 12345, + "hardware_id": "0x1234567890abcdef", + "firmware_version": "1.1.2", + "hardware_revision": 5, + "device_key": REDACTED, + "encryption_key": True, + "installed_at": "2019-04-30T01:57:34.443Z", + "calibrated_at": "2019-04-30T01:57:35.651Z", + "last_reported_at": "2019-04-30T02:20:04.821Z", + "missing_at": None, + "updated_at": "2019-04-30T01:57:36.129Z", + "created_at": "2019-04-30T01:56:45.932Z", + "signal_strength": 5, + "links": {"location": 123456}, + "lqi": 0, + "rssi": -46, + "surface_type": None, + }, + "132462": { + "id": 132462, + "uuid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "user": {"id": 12345, "email": REDACTED}, + "bridge": {"id": 12345, "hardware_id": "0x1234567890abcdef"}, + "last_bridge_hardware_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "name": "Living Room Sensor", + "location_id": 123456, + "system_id": 12345, + "hardware_id": "0x1234567890abcdef", + "firmware_version": "1.1.2", + "hardware_revision": 5, + "device_key": REDACTED, + "encryption_key": True, + "installed_at": "2019-04-30T01:45:56.169Z", + "calibrated_at": "2019-04-30T01:46:06.256Z", + "last_reported_at": "2019-04-30T02:20:04.829Z", + "missing_at": None, + "updated_at": "2019-04-30T01:46:07.717Z", + "created_at": "2019-04-30T01:45:14.148Z", + "signal_strength": 5, + "links": {"location": 123456}, + "lqi": 0, + "rssi": -30, + "surface_type": None, + }, + }, + "tasks": { + "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx": { + "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", + "task_type": "low_battery", + "sensor_data": [], + "status": { + "insights": { + "primary": { + "from_state": None, + "to_state": "high", + "data_received_at": "2020-11-17T18:40:27.024Z", + "origin": {}, + } + } + }, + "created_at": "2020-11-17T18:40:27.024Z", + "updated_at": "2020-11-17T18:40:27.033Z", + "sensor_id": 525993, + "model_version": "4.1", + "configuration": {}, + "links": {"sensor": 525993}, + } + }, + }, + }