feat: Einstellungen, Neustart, Diagnose-Sensoren und Diagnose-Download
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
"""Reboot button."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
||||
from homeassistant.components.button import SERVICE_PRESS
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.helpers import entity_id_for
|
||||
|
||||
|
||||
async def test_press_reboots(hass: HomeAssistant, init_integration, mock_client: MagicMock) -> None:
|
||||
entity_id = entity_id_for(hass, "button", "reboot")
|
||||
assert hass.states.get(entity_id).attributes["device_class"] == "restart"
|
||||
await hass.services.async_call(
|
||||
BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||
)
|
||||
mock_client.reboot.assert_awaited_once_with()
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Diagnostics download must not leak secrets or network identity."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from pytest_homeassistant_custom_component.components.diagnostics import (
|
||||
get_diagnostics_for_config_entry,
|
||||
)
|
||||
from pytest_homeassistant_custom_component.typing import ClientSessionGenerator
|
||||
|
||||
from tests_unit.samples import HOST_RGB, MAC_RGB, TOKEN
|
||||
|
||||
|
||||
async def test_diagnostics_redacts_secrets(
|
||||
hass: HomeAssistant, hass_client: ClientSessionGenerator, init_integration
|
||||
) -> None:
|
||||
diagnostics = await get_diagnostics_for_config_entry(hass, hass_client, init_integration)
|
||||
text = json.dumps(diagnostics)
|
||||
for secret in (TOKEN, MAC_RGB, "ninIOT", HOST_RGB):
|
||||
assert secret not in text, secret
|
||||
assert diagnostics["info"]["device_type"] == "gu10_rgb"
|
||||
assert diagnostics["state"]["brightness"] == 37
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Fade and transition settings."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from homeassistant.components.number import ATTR_VALUE, SERVICE_SET_VALUE
|
||||
from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, UnitOfTime
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
import pytest
|
||||
|
||||
from tests.helpers import entity_id_for
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("key", "initial", "new", "maximum"),
|
||||
[
|
||||
("fade_in", "450", 1300, 10000),
|
||||
("fade_out", "1250", 3350, 10000),
|
||||
("transition_default", "700", 42050, 60000),
|
||||
],
|
||||
)
|
||||
async def test_number_reads_and_writes_its_field(
|
||||
hass: HomeAssistant,
|
||||
init_integration,
|
||||
mock_client: MagicMock,
|
||||
key: str,
|
||||
initial: str,
|
||||
new: int,
|
||||
maximum: int,
|
||||
) -> None:
|
||||
entity_id = entity_id_for(hass, "number", key)
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == initial
|
||||
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == UnitOfTime.MILLISECONDS
|
||||
assert state.attributes["max"] == maximum
|
||||
assert er.async_get(hass).async_get(entity_id).entity_category is EntityCategory.CONFIG
|
||||
|
||||
await hass.services.async_call(
|
||||
NUMBER_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_VALUE: new},
|
||||
blocking=True,
|
||||
)
|
||||
mock_client.set_state.assert_awaited_once_with(**{key: new})
|
||||
assert hass.states.get(entity_id).state == str(new)
|
||||
@@ -0,0 +1,39 @@
|
||||
"""Power-on behaviour."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from homeassistant.components.select import ATTR_OPTION, SERVICE_SELECT_OPTION
|
||||
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.helpers import entity_id_for
|
||||
|
||||
|
||||
async def test_select_reads_and_writes(
|
||||
hass: HomeAssistant, init_integration, mock_client: MagicMock
|
||||
) -> None:
|
||||
entity_id = entity_id_for(hass, "select", "power_on_behaviour")
|
||||
state = hass.states.get(entity_id)
|
||||
assert state.state == "last"
|
||||
assert state.attributes["options"] == ["last", "on", "off"]
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{ATTR_ENTITY_ID: entity_id, ATTR_OPTION: "off"},
|
||||
blocking=True,
|
||||
)
|
||||
mock_client.set_state.assert_awaited_once_with(power_on_behaviour="off")
|
||||
assert hass.states.get(entity_id).state == "off"
|
||||
|
||||
|
||||
async def test_unknown_value_from_lamp_is_unknown(
|
||||
hass: HomeAssistant, mock_config_entry, mock_client: MagicMock, lamp_state
|
||||
) -> None:
|
||||
lamp_state["power_on_behaviour"] = "fade"
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(entity_id_for(hass, "select", "power_on_behaviour")).state == "unknown"
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Diagnostic sensors."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.util import dt as dt_util
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
from tests.helpers import entity_id_for
|
||||
|
||||
DISABLED_KEYS = ("ip_address", "free_heap", "last_ota_error", "boot_time")
|
||||
|
||||
|
||||
async def _enable_all(
|
||||
hass: HomeAssistant, entry: MockConfigEntry, freezer: FrozenDateTimeFactory
|
||||
) -> None:
|
||||
registry = er.async_get(hass)
|
||||
for key in DISABLED_KEYS:
|
||||
registry.async_update_entity(entity_id_for(hass, "sensor", key), disabled_by=None)
|
||||
await hass.config_entries.async_reload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
# HA 2026.9: registry.async_update_entity(disabled_by=None) also arms the config entry's
|
||||
# own debounced reload (config_entries.RELOAD_AFTER_UPDATE_DELAY = 30 s), independent of
|
||||
# the explicit reload above. Left alone, that second reload fires mid-test - typically
|
||||
# right on top of the first _poll() below - and silently replaces the boot-time entity,
|
||||
# defeating the jitter-tolerance assertion. Drain it here so the entities are settled
|
||||
# before the test takes its "first" reading.
|
||||
freezer.tick(timedelta(seconds=31))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def _poll(hass: HomeAssistant, freezer: FrozenDateTimeFactory, seconds: int) -> None:
|
||||
freezer.tick(timedelta(seconds=seconds))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_signal_enabled_others_disabled_by_default(
|
||||
hass: HomeAssistant, init_integration
|
||||
) -> None:
|
||||
registry = er.async_get(hass)
|
||||
assert hass.states.get(entity_id_for(hass, "sensor", "rssi")).state == "-61"
|
||||
for key in DISABLED_KEYS:
|
||||
entry = registry.async_get(entity_id_for(hass, "sensor", key))
|
||||
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION, key
|
||||
|
||||
|
||||
async def test_diagnostic_values(
|
||||
hass: HomeAssistant, init_integration: MockConfigEntry, freezer
|
||||
) -> None:
|
||||
await _enable_all(hass, init_integration, freezer)
|
||||
assert hass.states.get(entity_id_for(hass, "sensor", "ip_address")).state == "192.168.2.22"
|
||||
assert hass.states.get(entity_id_for(hass, "sensor", "free_heap")).state == "119872"
|
||||
assert hass.states.get(entity_id_for(hass, "sensor", "last_ota_error")).state == "unknown"
|
||||
expected_boot = dt_util.utcnow() - timedelta(seconds=129832)
|
||||
boot = dt_util.parse_datetime(hass.states.get(entity_id_for(hass, "sensor", "boot_time")).state)
|
||||
assert abs((boot - expected_boot).total_seconds()) < 2
|
||||
|
||||
|
||||
async def test_boot_time_ignores_jitter_but_follows_reboots(
|
||||
hass: HomeAssistant,
|
||||
init_integration: MockConfigEntry,
|
||||
lamp_info: dict[str, Any],
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
await _enable_all(hass, init_integration, freezer)
|
||||
entity_id = entity_id_for(hass, "sensor", "boot_time")
|
||||
first = hass.states.get(entity_id).state
|
||||
|
||||
lamp_info["uptime"] += 31 + 20 # clock drift of 20 s: below the 60 s threshold
|
||||
await _poll(hass, freezer, 31)
|
||||
assert hass.states.get(entity_id).state == first
|
||||
|
||||
lamp_info["uptime"] = 12 # the lamp rebooted
|
||||
await _poll(hass, freezer, 31)
|
||||
rebooted = dt_util.parse_datetime(hass.states.get(entity_id).state)
|
||||
assert abs((rebooted - (dt_util.utcnow() - timedelta(seconds=12))).total_seconds()) < 2
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Wake-on-change switch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.helpers import entity_id_for
|
||||
|
||||
|
||||
async def test_switch_reads_and_writes(
|
||||
hass: HomeAssistant, init_integration, mock_client: MagicMock
|
||||
) -> None:
|
||||
entity_id = entity_id_for(hass, "switch", "wake_on_change")
|
||||
assert hass.states.get(entity_id).state == STATE_ON
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||
)
|
||||
mock_client.set_state.assert_awaited_once_with(wake_on_change=False)
|
||||
assert hass.states.get(entity_id).state == STATE_OFF
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True
|
||||
)
|
||||
mock_client.set_state.assert_awaited_with(wake_on_change=True)
|
||||
Reference in New Issue
Block a user