Aufgebaut wie ha-ninlight: die Abbilder liegen unter ninstrip_firmware im Konfigurationsverzeichnis, Home Assistant liefert sie selbst unter einer einmaligen Adresse aus, und zwar genau die Bytes, die vorher geprueft wurden. Dazu firmware.py (Anhaenger, Versionsreihenfolge, WLAN-Pruefung, Katalog), http.py, urls.py und shared.py. Drei Unterschiede zu ninlight, alle aus der Firmware heraus: * Verglichen wird device_type - die Platine -, nicht product. Letzteres ist bei allen Platinen 'ninstrip'; die Abbilder unterscheiden sich aber hart, die K1Y invertiert das Datensignal und die KL-RGBC3 nicht. Firmware ohne dieses Feld bekommt gar kein Angebot. * Die WLAN-Pruefung sucht die SSID unverpackt: ein ESP32-Abbild ist nicht gzip-verpackt wie das des W600. An einem mit sdkconfig.local gebauten Abbild gegengeprueft - die SSID steht genau einmal NUL-gerahmt drin. * Der Fehlerschluessel heisst device_url_too_long statt lamp_url_too_long. Dazu das Testgeruest, das es hier bisher gar nicht gab: 204 Tests in tests_unit/ (ohne HA-Instanz) und tests/ (mit), Abdeckung 97 %, dazu scripts/check.sh als Ersatz fuer die fehlende CI. Die Nutzdaten in tests_unit/samples.py sind an der Stehlampe abgefragt. Beim Gegenpruefen per Mutation fiel auf, dass die Platinenpruefung unmittelbar vor dem Senden ungeprueft war - der Test dazu ist jetzt da und faellt, wenn man die Pruefung entfernt.
94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
"""Szenenauswahl."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from unittest.mock import MagicMock
|
|
|
|
from freezegun.api import FrozenDateTimeFactory
|
|
from homeassistant.const import ATTR_ENTITY_ID
|
|
from homeassistant.core import HomeAssistant
|
|
from pytest_homeassistant_custom_component.common import MockConfigEntry
|
|
|
|
from custom_components.ninstrip.select import NONE_OPTION
|
|
from tests.helpers import abfragen, entity_id_for
|
|
|
|
|
|
def preset_id(hass: HomeAssistant) -> str:
|
|
return entity_id_for(hass, "select", "preset")
|
|
|
|
|
|
async def test_nur_belegte_szenen_stehen_zur_wahl(
|
|
hass: HomeAssistant, init_integration: MockConfigEntry
|
|
) -> None:
|
|
state = hass.states.get(preset_id(hass))
|
|
assert state.attributes["options"] == [
|
|
NONE_OPTION,
|
|
"Warmweiss",
|
|
"Kaltweiss",
|
|
"Regenbogen",
|
|
"Feuer",
|
|
]
|
|
assert state.state == "Regenbogen"
|
|
|
|
|
|
async def test_geaenderte_segmente_zeigen_keine_szene_mehr(
|
|
hass: HomeAssistant,
|
|
init_integration: MockConfigEntry,
|
|
strip_state: dict[str, Any],
|
|
freezer: FrozenDateTimeFactory,
|
|
) -> None:
|
|
strip_state["preset_modified"] = True
|
|
await abfragen(hass, freezer)
|
|
assert hass.states.get(preset_id(hass)).state == NONE_OPTION
|
|
|
|
|
|
async def test_unbekannte_szenennummer_zeigt_keine_szene(
|
|
hass: HomeAssistant,
|
|
init_integration: MockConfigEntry,
|
|
strip_state: dict[str, Any],
|
|
freezer: FrozenDateTimeFactory,
|
|
) -> None:
|
|
strip_state["preset"] = 6 # vorhanden, aber unbelegt
|
|
await abfragen(hass, freezer)
|
|
assert hass.states.get(preset_id(hass)).state == NONE_OPTION
|
|
|
|
|
|
async def test_namenlose_szene_bekommt_eine_nummer(
|
|
hass: HomeAssistant,
|
|
init_integration: MockConfigEntry,
|
|
strip_presets: dict[str, Any],
|
|
freezer: FrozenDateTimeFactory,
|
|
) -> None:
|
|
strip_presets["presets"][2]["name"] = ""
|
|
await abfragen(hass, freezer)
|
|
state = hass.states.get(preset_id(hass))
|
|
assert "Szene 3" in state.attributes["options"]
|
|
assert state.state == "Szene 3"
|
|
|
|
|
|
async def test_szene_waehlen_schaltet_sie_auf(
|
|
hass: HomeAssistant, init_integration: MockConfigEntry, mock_client: MagicMock
|
|
) -> None:
|
|
await hass.services.async_call(
|
|
"select",
|
|
"select_option",
|
|
{ATTR_ENTITY_ID: preset_id(hass), "option": "Feuer"},
|
|
blocking=True,
|
|
)
|
|
mock_client.apply_preset.assert_awaited_once_with(3)
|
|
|
|
|
|
async def test_die_leere_wahl_schaltet_aus(
|
|
hass: HomeAssistant, init_integration: MockConfigEntry, mock_client: MagicMock
|
|
) -> None:
|
|
"""Es gibt kein 'Szene abwaehlen' - das Gegenstueck ist Ausschalten."""
|
|
await hass.services.async_call(
|
|
"select",
|
|
"select_option",
|
|
{ATTR_ENTITY_ID: preset_id(hass), "option": NONE_OPTION},
|
|
blocking=True,
|
|
)
|
|
mock_client.set_state.assert_awaited_once_with(on=False)
|
|
mock_client.apply_preset.assert_not_awaited()
|