Add diagnostics to Notion (#64713)

This commit is contained in:
Aaron Bach 2022-01-24 08:15:45 -07:00 committed by GitHub
parent 5b8f6d1b9a
commit b54722952c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 362 additions and 6 deletions

View file

@ -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),
}

View file

@ -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

View file

@ -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
}
}
]

View file

@ -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
}
]

View file

@ -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
}
}
]

View file

@ -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},
}
},
},
}