From a278b2a59504d32239b1d4be8e3f08dd752ac5f3 Mon Sep 17 00:00:00 2001 From: Sander Date: Sat, 18 May 2024 19:29:00 +0000 Subject: [PATCH] Add test for `diagnostics` --- tests/test_diagnostics.py | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test_diagnostics.py diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py new file mode 100644 index 0000000..8a6a059 --- /dev/null +++ b/tests/test_diagnostics.py @@ -0,0 +1,63 @@ +"""Test kamstrup_403 diagnostics.""" + +from http import HTTPStatus +from typing import cast + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component +from homeassistant.util.json import JsonObjectType +from pytest_homeassistant_custom_component.common import MockConfigEntry +from pytest_homeassistant_custom_component.typing import ClientSessionGenerator + +from custom_components.kamstrup_403.const import DOMAIN + +from .const import MOCK_CONFIG + + +async def test_config_entry_diagnostics( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + bypass_get_data, +) -> None: + """Test config entry diagnostics.""" + + entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) + + assert result["config_entry"]["entry_id"] == "test" + assert result["config_entry"]["domain"] == DOMAIN + + assert result["registered_commands"] == [60, 68, 99, 113, 1001, 1004] + + +# The following 2 functions are copied from https://github.com/home-assistant/core/blob/dev/tests/components/diagnostics/__init__.py +async def _get_diagnostics_for_config_entry( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + config_entry: ConfigEntry, +) -> JsonObjectType: + """Return the diagnostics config entry for the specified domain.""" + assert await async_setup_component(hass, "diagnostics", {}) + await hass.async_block_till_done() + + client = await hass_client() + response = await client.get( + f"/api/diagnostics/config_entry/{config_entry.entry_id}" + ) + assert response.status == HTTPStatus.OK + return cast(JsonObjectType, await response.json()) + + +async def get_diagnostics_for_config_entry( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + config_entry: ConfigEntry, +) -> JsonObjectType: + """Return the diagnostics config entry for the specified domain.""" + data = await _get_diagnostics_for_config_entry(hass, hass_client, config_entry) + return cast(JsonObjectType, data["data"])