feat: update-Entitaet fuer die Firmware und das Testgeruest
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.
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
"""Hauptschalter und Helligkeit."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
import pytest
|
||||
from pytest_homeassistant_custom_component.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
from tests.helpers import entity_id_for
|
||||
|
||||
|
||||
def light_id(hass: HomeAssistant) -> str:
|
||||
return entity_id_for(hass, "light", "light")
|
||||
|
||||
|
||||
async def abfragen(hass: HomeAssistant, freezer: FrozenDateTimeFactory) -> None:
|
||||
"""Einen Abfragezyklus vorspulen."""
|
||||
freezer.tick(timedelta(seconds=31))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_zustand_und_helligkeit_aus_prozent(
|
||||
hass: HomeAssistant, init_integration: MockConfigEntry
|
||||
) -> None:
|
||||
state = hass.states.get(light_id(hass))
|
||||
assert state is not None
|
||||
assert state.state == STATE_ON
|
||||
# Das Geraet rechnet in Prozent: 100 % sind 255.
|
||||
assert state.attributes[ATTR_BRIGHTNESS] == 255
|
||||
assert state.attributes["color_mode"] == ColorMode.BRIGHTNESS
|
||||
assert state.attributes["pixel_count"] == 60
|
||||
assert state.attributes["segment_count"] == 1
|
||||
assert state.attributes["preset"] == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("prozent", "erwartet"), [(0, 0), (1, 3), (37, 94), (50, 128), (99, 252), (100, 255)]
|
||||
)
|
||||
async def test_prozent_werden_auf_0_bis_255_umgerechnet(
|
||||
hass: HomeAssistant,
|
||||
init_integration: MockConfigEntry,
|
||||
strip_state: dict[str, Any],
|
||||
freezer: FrozenDateTimeFactory,
|
||||
prozent: int,
|
||||
erwartet: int,
|
||||
) -> None:
|
||||
strip_state["brightness"] = prozent
|
||||
await abfragen(hass, freezer)
|
||||
assert hass.states.get(light_id(hass)).attributes[ATTR_BRIGHTNESS] == erwartet
|
||||
|
||||
|
||||
@pytest.mark.parametrize("wert", [-5, 150])
|
||||
async def test_unsinnige_helligkeit_wird_gekappt(
|
||||
hass: HomeAssistant,
|
||||
init_integration: MockConfigEntry,
|
||||
strip_state: dict[str, Any],
|
||||
freezer: FrozenDateTimeFactory,
|
||||
wert: int,
|
||||
) -> None:
|
||||
"""Ein Geraet, das ausserhalb von 0..100 antwortet, darf HA nicht sprengen."""
|
||||
strip_state["brightness"] = wert
|
||||
await abfragen(hass, freezer)
|
||||
assert hass.states.get(light_id(hass)).attributes[ATTR_BRIGHTNESS] in (0, 255)
|
||||
|
||||
|
||||
async def test_einschalten_ohne_helligkeit_schickt_nur_on(
|
||||
hass: HomeAssistant, init_integration: MockConfigEntry, mock_client: MagicMock
|
||||
) -> None:
|
||||
await hass.services.async_call(
|
||||
"light", "turn_on", {ATTR_ENTITY_ID: light_id(hass)}, blocking=True
|
||||
)
|
||||
mock_client.set_state.assert_awaited_once_with(on=True)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("ha_wert", "prozent"), [(255, 100), (128, 50), (3, 1), (1, 0)])
|
||||
async def test_einschalten_mit_helligkeit_rechnet_in_prozent(
|
||||
hass: HomeAssistant,
|
||||
init_integration: MockConfigEntry,
|
||||
mock_client: MagicMock,
|
||||
ha_wert: int,
|
||||
prozent: int,
|
||||
) -> None:
|
||||
await hass.services.async_call(
|
||||
"light",
|
||||
"turn_on",
|
||||
{ATTR_ENTITY_ID: light_id(hass), ATTR_BRIGHTNESS: ha_wert},
|
||||
blocking=True,
|
||||
)
|
||||
mock_client.set_state.assert_awaited_once_with(on=True, brightness=prozent)
|
||||
|
||||
|
||||
async def test_ausschalten(
|
||||
hass: HomeAssistant, init_integration: MockConfigEntry, mock_client: MagicMock
|
||||
) -> None:
|
||||
await hass.services.async_call(
|
||||
"light", "turn_off", {ATTR_ENTITY_ID: light_id(hass)}, blocking=True
|
||||
)
|
||||
mock_client.set_state.assert_awaited_once_with(on=False)
|
||||
assert hass.states.get(light_id(hass)).state == STATE_OFF
|
||||
|
||||
|
||||
async def test_die_antwort_des_geraets_zaehlt_nicht_die_anfrage(
|
||||
hass: HomeAssistant, init_integration: MockConfigEntry, mock_client: MagicMock
|
||||
) -> None:
|
||||
"""Das Geraet darf die Helligkeit abweichend beantworten - dann gilt seine."""
|
||||
|
||||
async def antwortet_anders(**fields: Any) -> dict[str, Any]:
|
||||
return {**mock_client.get_state.side_effect(), **fields, "brightness": 20}
|
||||
|
||||
mock_client.set_state.side_effect = antwortet_anders
|
||||
await hass.services.async_call(
|
||||
"light",
|
||||
"turn_on",
|
||||
{ATTR_ENTITY_ID: light_id(hass), ATTR_BRIGHTNESS: 255},
|
||||
blocking=True,
|
||||
)
|
||||
assert hass.states.get(light_id(hass)).attributes[ATTR_BRIGHTNESS] == 51
|
||||
Reference in New Issue
Block a user