|
| 1 | +"""Test knmi diagnostics.""" |
| 2 | + |
| 3 | +from http import HTTPStatus |
| 4 | +from typing import cast |
| 5 | + |
| 6 | +from homeassistant.config_entries import ConfigEntry |
| 7 | +from homeassistant.core import HomeAssistant |
| 8 | +from homeassistant.setup import async_setup_component |
| 9 | +from homeassistant.util.json import JsonObjectType |
| 10 | +from pytest_homeassistant_custom_component.common import MockConfigEntry |
| 11 | +from pytest_homeassistant_custom_component.typing import ClientSessionGenerator |
| 12 | + |
| 13 | +from custom_components.knmi.const import DOMAIN |
| 14 | +from custom_components.knmi.diagnostics import TO_REDACT |
| 15 | + |
| 16 | +from .const import MOCK_CONFIG |
| 17 | + |
| 18 | + |
| 19 | +async def test_config_entry_diagnostics( |
| 20 | + hass: HomeAssistant, |
| 21 | + hass_client: ClientSessionGenerator, |
| 22 | + mocked_data, |
| 23 | +) -> None: |
| 24 | + """Test config entry diagnostics.""" |
| 25 | + |
| 26 | + entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") |
| 27 | + entry.add_to_hass(hass) |
| 28 | + await hass.config_entries.async_setup(entry.entry_id) |
| 29 | + await hass.async_block_till_done() |
| 30 | + |
| 31 | + result = await get_diagnostics_for_config_entry(hass, hass_client, entry) |
| 32 | + |
| 33 | + assert result["config_entry"]["entry_id"] == "test" |
| 34 | + assert result["config_entry"]["domain"] == DOMAIN |
| 35 | + |
| 36 | + for key in TO_REDACT: |
| 37 | + assert result["config_entry"]["data"][key] == "**REDACTED**" |
| 38 | + |
| 39 | + assert result["data"]["liveweer"][0]["plaats"] == "Purmerend" |
| 40 | + |
| 41 | + |
| 42 | +# The following 2 functions are copied from https://github.com/home-assistant/core/blob/dev/tests/components/diagnostics/__init__.py |
| 43 | +async def _get_diagnostics_for_config_entry( |
| 44 | + hass: HomeAssistant, |
| 45 | + hass_client: ClientSessionGenerator, |
| 46 | + config_entry: ConfigEntry, |
| 47 | +) -> JsonObjectType: |
| 48 | + """Return the diagnostics config entry for the specified domain.""" |
| 49 | + assert await async_setup_component(hass, "diagnostics", {}) |
| 50 | + await hass.async_block_till_done() |
| 51 | + |
| 52 | + client = await hass_client() |
| 53 | + response = await client.get( |
| 54 | + f"/api/diagnostics/config_entry/{config_entry.entry_id}" |
| 55 | + ) |
| 56 | + assert response.status == HTTPStatus.OK |
| 57 | + return cast(JsonObjectType, await response.json()) |
| 58 | + |
| 59 | + |
| 60 | +async def get_diagnostics_for_config_entry( |
| 61 | + hass: HomeAssistant, |
| 62 | + hass_client: ClientSessionGenerator, |
| 63 | + config_entry: ConfigEntry, |
| 64 | +) -> JsonObjectType: |
| 65 | + """Return the diagnostics config entry for the specified domain.""" |
| 66 | + data = await _get_diagnostics_for_config_entry(hass, hass_client, config_entry) |
| 67 | + return cast(JsonObjectType, data["data"]) |
0 commit comments