From 11de453848dd44d0c3c0fa0ce9e5361aa7466041 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 20 Jan 2022 23:47:42 +0100 Subject: [PATCH] Add diagnostics to deCONZ integration (#64585) --- .../components/deconz/diagnostics.py | 39 ++++++++++++++ tests/components/deconz/test_diagnostics.py | 54 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 homeassistant/components/deconz/diagnostics.py create mode 100644 tests/components/deconz/test_diagnostics.py diff --git a/homeassistant/components/deconz/diagnostics.py b/homeassistant/components/deconz/diagnostics.py new file mode 100644 index 00000000000..9417f9cd23f --- /dev/null +++ b/homeassistant/components/deconz/diagnostics.py @@ -0,0 +1,39 @@ +"""Diagnostics support for deCONZ.""" +from __future__ import annotations + +from typing import Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.system_info import async_get_system_info + +from .gateway import get_gateway_from_config_entry + + +async def async_get_config_entry_diagnostics( + hass: HomeAssistant, config_entry: ConfigEntry +) -> dict[str, Any]: + """Return diagnostics for a config entry.""" + gateway = get_gateway_from_config_entry(hass, config_entry) + diag: dict[str, Any] = {} + + diag["home_assistant"] = await async_get_system_info(hass) + diag["config_entry"] = dict(config_entry.data) + diag["deconz_config"] = gateway.api.config.raw + diag["websocket_state"] = gateway.api.websocket.state + diag["deconz_ids"] = gateway.deconz_ids + diag["entities"] = gateway.entities + diag["events"] = { + event.serial: { + "event_id": event.event_id, + "event_type": type(event).__name__, + } + for event in gateway.events + } + diag["alarm_systems"] = {k: v.raw for k, v in gateway.api.alarmsystems.items()} + diag["groups"] = {k: v.raw for k, v in gateway.api.groups.items()} + diag["lights"] = {k: v.raw for k, v in gateway.api.lights.items()} + diag["scenes"] = {k: v.raw for k, v in gateway.api.scenes.items()} + diag["sensors"] = {k: v.raw for k, v in gateway.api.sensors.items()} + + return diag diff --git a/tests/components/deconz/test_diagnostics.py b/tests/components/deconz/test_diagnostics.py new file mode 100644 index 00000000000..078fed475eb --- /dev/null +++ b/tests/components/deconz/test_diagnostics.py @@ -0,0 +1,54 @@ +"""Test deCONZ diagnostics.""" + +from unittest.mock import patch + +from pydeconz.websocket import STATE_RUNNING + +from homeassistant.const import Platform + +from .test_gateway import DECONZ_CONFIG, setup_deconz_integration + +from tests.components.diagnostics import get_diagnostics_for_config_entry + + +async def test_entry_diagnostics( + hass, hass_client, aioclient_mock, mock_deconz_websocket +): + """Test config entry diagnostics.""" + config_entry = await setup_deconz_integration(hass, aioclient_mock) + + await mock_deconz_websocket(state=STATE_RUNNING) + await hass.async_block_till_done() + + with patch( + "homeassistant.helpers.system_info.async_get_system_info", + return_value={"get_system_info": "fake data"}, + ): + assert await get_diagnostics_for_config_entry( + hass, hass_client, config_entry + ) == { + "home_assistant": {"get_system_info": "fake data"}, + "config_entry": dict(config_entry.data), + "deconz_config": DECONZ_CONFIG, + "websocket_state": STATE_RUNNING, + "deconz_ids": {}, + "entities": { + str(Platform.ALARM_CONTROL_PANEL): [], + str(Platform.BINARY_SENSOR): [], + str(Platform.CLIMATE): [], + str(Platform.COVER): [], + str(Platform.FAN): [], + str(Platform.LIGHT): [], + str(Platform.LOCK): [], + str(Platform.NUMBER): [], + str(Platform.SENSOR): [], + str(Platform.SIREN): [], + str(Platform.SWITCH): [], + }, + "events": {}, + "alarm_systems": {}, + "groups": {}, + "lights": {}, + "scenes": {}, + "sensors": {}, + }