mirror of
https://github.com/esphome/esphome.git
synced 2026-11-09 14:45:57 +00:00
Co-authored-by: Jonathan Swoboda <154711427+swoboda1337@users.noreply.github.com>
69 lines
3.3 KiB
Python
69 lines
3.3 KiB
Python
"""Integration test for light initial_state configuration.
|
|
|
|
Tests that the initial_state values are correctly applied at boot when
|
|
no saved preferences exist. The initial_state callback populates defaults
|
|
that the restore logic uses as a fallback.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from .state_utils import require_entity, subscribe_after_initial_states
|
|
from .types import APIClientConnectedFactory, RunCompiledFunction
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_light_initial_state(
|
|
yaml_config: str,
|
|
run_compiled: RunCompiledFunction,
|
|
api_client_connected: APIClientConnectedFactory,
|
|
) -> None:
|
|
"""Test that initial_state values are applied at boot."""
|
|
async with run_compiled(yaml_config), api_client_connected() as client:
|
|
entities, _ = await client.list_entities_services()
|
|
light = require_entity(entities, "test_light")
|
|
|
|
sub = await subscribe_after_initial_states(client, entities)
|
|
|
|
state = sub.initial_states[light.key]
|
|
|
|
# restore_mode: ALWAYS_OFF overrides state to false
|
|
assert state.state is False
|
|
|
|
# But the color values from initial_state should be applied
|
|
assert state.brightness == pytest.approx(0.75, abs=0.05)
|
|
assert state.red == pytest.approx(1.0, abs=0.01)
|
|
assert state.green == pytest.approx(0.5, abs=0.01)
|
|
assert state.blue == pytest.approx(0.0, abs=0.01)
|
|
|
|
# Regression test: RESTORE_AND_ON always forces the light on at boot, even when
|
|
# the recovered/initial brightness was 0 -- it must never come up on-but-invisible.
|
|
restore_and_on_light = require_entity(entities, "test_restore_and_on_light")
|
|
restore_and_on_state = sub.initial_states[restore_and_on_light.key]
|
|
assert restore_and_on_state.state is True
|
|
assert restore_and_on_state.brightness == pytest.approx(1.0)
|
|
|
|
# initial_state may also name the effect to start with. The light must come up
|
|
# with that effect already active, without an on_boot automation.
|
|
effect_light = require_entity(entities, "test_effect_light")
|
|
effect_state = sub.initial_states[effect_light.key]
|
|
assert effect_state.state is True
|
|
assert effect_state.effect == "Test Pulse"
|
|
|
|
# With neither restore_mode nor restore_state configured, initial_state: must
|
|
# be honored as-is -- unlike every explicit restore_mode: value, omitting both
|
|
# keys entirely must not force the light off.
|
|
no_restore_key_light = require_entity(entities, "test_no_restore_key_light")
|
|
no_restore_key_state = sub.initial_states[no_restore_key_light.key]
|
|
assert no_restore_key_state.state is True
|
|
assert no_restore_key_state.brightness == pytest.approx(0.6, abs=0.01)
|
|
|
|
# color_mode omitted: inferred from the colour fields, so they are applied (the
|
|
# light comes up red rather than the default white). RGB values are normalised to
|
|
# proportions, so 30% red on its own is pure red.
|
|
inferred_light = require_entity(entities, "test_inferred_color_mode_light")
|
|
inferred_state = sub.initial_states[inferred_light.key]
|
|
assert inferred_state.state is True
|
|
assert inferred_state.red == pytest.approx(1.0, abs=0.01)
|
|
assert inferred_state.green == pytest.approx(0.0, abs=0.01)
|
|
assert inferred_state.blue == pytest.approx(0.0, abs=0.01)
|