Files

102 lines
3.9 KiB
Python

"""Setup, unload, repair issues and the device registry."""
from __future__ import annotations
from datetime import timedelta
from typing import Any
from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import issue_registry as ir
from pytest_homeassistant_custom_component.common import MockConfigEntry, async_fire_time_changed
from custom_components.ninlight.api import NinlightConnectionError
from custom_components.ninlight.const import DEFAULT_TOKEN, DOMAIN
from custom_components.ninlight.entity import device_name
from tests_unit.samples import HOST_RGB, INFO_CW, INFO_RGB, MAC_RGB
def test_device_name_prefers_lamp_name() -> None:
assert device_name(INFO_RGB) == "Klyqa GU10 RGB CW/WW 06b8"
assert device_name(INFO_CW) == "Wohnzimmer"
assert device_name({**INFO_RGB, "name": " ", "product_name": ""}) == "gu10_rgb 06b8"
async def test_setup_and_unload(hass: HomeAssistant, init_integration: MockConfigEntry) -> None:
assert init_integration.state is ConfigEntryState.LOADED
device = dr.async_get(hass).async_get_device_by_identifier(
(DOMAIN, MAC_RGB), init_integration.entry_id
)
assert device is not None
assert (device.name, device.model, device.model_id, device.sw_version) == (
"Klyqa GU10 RGB CW/WW 06b8",
"Klyqa GU10 RGB CW/WW",
"gu10_rgb",
"0.3.8-36-gca9bb",
)
assert (dr.CONNECTION_NETWORK_MAC, MAC_RGB) in device.connections
assert await hass.config_entries.async_unload(init_integration.entry_id)
await hass.async_block_till_done()
assert init_integration.state is ConfigEntryState.NOT_LOADED
async def test_unreachable_at_startup_retries(
hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_client: MagicMock
) -> None:
mock_client.get_state.side_effect = NinlightConnectionError("down")
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
async def test_default_token_raises_repair_issue(
hass: HomeAssistant, mock_client: MagicMock
) -> None:
entry = MockConfigEntry(
domain=DOMAIN,
title="Lampe",
unique_id=MAC_RGB,
data={CONF_HOST: HOST_RGB, CONF_TOKEN: DEFAULT_TOKEN},
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
issue = ir.async_get(hass).async_get_issue(DOMAIN, f"default_token_{entry.entry_id}")
assert issue is not None
assert issue.translation_key == "default_token"
assert issue.translation_placeholders == {"name": "Lampe"}
assert await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
assert ir.async_get(hass).async_get_issue(DOMAIN, f"default_token_{entry.entry_id}") is None
async def test_own_token_raises_no_issue(
hass: HomeAssistant, init_integration: MockConfigEntry
) -> None:
assert (
ir.async_get(hass).async_get_issue(DOMAIN, f"default_token_{init_integration.entry_id}")
is None
)
async def test_device_registry_follows_firmware_version(
hass: HomeAssistant,
init_integration: MockConfigEntry,
lamp_info: dict[str, Any],
freezer: FrozenDateTimeFactory,
) -> None:
lamp_info["fw_version"] = "0.3.8-47-g71a57"
freezer.tick(timedelta(seconds=31))
async_fire_time_changed(hass)
await hass.async_block_till_done()
device = dr.async_get(hass).async_get_device_by_identifier(
(DOMAIN, MAC_RGB), init_integration.entry_id
)
assert device.sw_version == "0.3.8-47-g71a57"