Zeigt strip.current_ma und strip.power_w aus system/info als Sensoren und macht max_milliamps aus system/settings ueber ein Zahlenfeld einstellbar. Schreiben liest wie bei sACN zurueck statt den geschriebenen Wert anzunehmen.
67 lines
2.5 KiB
Python
67 lines
2.5 KiB
Python
"""Strom, Leistung und das Budget."""
|
|
|
|
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 tests.helpers import abfragen, entity_id_for
|
|
|
|
|
|
async def test_strom_und_leistung(hass: HomeAssistant, init_integration: MockConfigEntry) -> None:
|
|
assert hass.states.get(entity_id_for(hass, "sensor", "current")).state == "2500"
|
|
# power_w kommt als Kommazahl von der Firmware (2,5 A bei 12 V).
|
|
assert hass.states.get(entity_id_for(hass, "sensor", "power")).state == "30.0"
|
|
|
|
|
|
async def test_budget_zeigt_den_eingestellten_wert(
|
|
hass: HomeAssistant, init_integration: MockConfigEntry
|
|
) -> None:
|
|
entity_id = entity_id_for(hass, "number", "max_milliamps")
|
|
assert hass.states.get(entity_id).state == "1800.0"
|
|
assert hass.states.get(entity_id).attributes["max"] == 65535
|
|
|
|
|
|
async def test_budget_setzen(
|
|
hass: HomeAssistant, init_integration: MockConfigEntry, mock_client: MagicMock
|
|
) -> None:
|
|
await hass.services.async_call(
|
|
"number",
|
|
"set_value",
|
|
{ATTR_ENTITY_ID: entity_id_for(hass, "number", "max_milliamps"), "value": 9000},
|
|
blocking=True,
|
|
)
|
|
mock_client.set_strip.assert_awaited_once_with(max_milliamps=9000)
|
|
# Nicht geraten, sondern zurueckgelesen: das Geraet antwortet nur mit "ok".
|
|
assert mock_client.get_settings.await_count >= 2
|
|
assert hass.states.get(entity_id_for(hass, "number", "max_milliamps")).state == "9000.0"
|
|
|
|
|
|
async def test_ohne_strip_block_in_settings_bleibt_das_budget_leer(
|
|
hass: HomeAssistant,
|
|
init_integration: MockConfigEntry,
|
|
strip_settings: dict[str, Any],
|
|
freezer: FrozenDateTimeFactory,
|
|
) -> None:
|
|
"""Aeltere Firmware ohne Strommessung meldet unter system/settings keinen strip-Block."""
|
|
strip_settings.pop("strip")
|
|
await abfragen(hass, freezer)
|
|
assert hass.states.get(entity_id_for(hass, "number", "max_milliamps")).state == "unknown"
|
|
|
|
|
|
async def test_ohne_strip_block_in_info_bleiben_strom_und_leistung_leer(
|
|
hass: HomeAssistant,
|
|
init_integration: MockConfigEntry,
|
|
strip_info: dict[str, Any],
|
|
freezer: FrozenDateTimeFactory,
|
|
) -> None:
|
|
strip_info.pop("strip")
|
|
await abfragen(hass, freezer)
|
|
assert hass.states.get(entity_id_for(hass, "sensor", "current")).state == "unknown"
|
|
assert hass.states.get(entity_id_for(hass, "sensor", "power")).state == "unknown"
|