"""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