[light] Use function-pointer fields in LightControlAction (#15132)

This commit is contained in:
J. Nick Koston
2026-04-07 12:00:17 -10:00
committed by GitHub
parent 9fe4d5c63d
commit 5d31f4aeba
4 changed files with 320 additions and 72 deletions

View File

@@ -0,0 +1,139 @@
esphome:
name: light-control-action-test
host:
api: # Port will be automatically injected
logger:
level: DEBUG
globals:
- id: test_brightness
type: float
initial_value: "0.75"
output:
- platform: template
id: test_red
type: float
write_action:
- lambda: ""
- platform: template
id: test_green
type: float
write_action:
- lambda: ""
- platform: template
id: test_blue
type: float
write_action:
- lambda: ""
- platform: template
id: test_cold_white
type: float
write_action:
- lambda: ""
- platform: template
id: test_warm_white
type: float
write_action:
- lambda: ""
light:
- platform: rgbww
name: "Test Light"
id: test_light
red: test_red
green: test_green
blue: test_blue
cold_white: test_cold_white
warm_white: test_warm_white
cold_white_color_temperature: 6536 K
warm_white_color_temperature: 2000 K
effects:
- random:
name: "Test Effect"
transition_length: 10ms
update_interval: 10ms
button:
# Test 1: light.turn_on with RGB constants
- platform: template
id: btn_turn_on_rgb
name: "Turn On RGB"
on_press:
- light.turn_on:
id: test_light
brightness: 1.0
red: 0.0
green: 0.0
blue: 1.0
# Test 2: light.turn_off
- platform: template
id: btn_turn_off
name: "Turn Off"
on_press:
- light.turn_off:
id: test_light
# Test 3: light.turn_on with color_temperature
- platform: template
id: btn_turn_on_ct
name: "Turn On CT"
on_press:
- light.turn_on:
id: test_light
color_temperature: 4000 K
brightness: 0.8
# Test 4: light.turn_on with effect
- platform: template
id: btn_turn_on_effect
name: "Turn On Effect"
on_press:
- light.turn_on:
id: test_light
effect: "Test Effect"
# Test 5: light.turn_on with effect none to clear it
- platform: template
id: btn_clear_effect
name: "Clear Effect"
on_press:
- light.turn_on:
id: test_light
effect: "None"
# Test 6: light.control with cold/warm white
- platform: template
id: btn_control_cw
name: "Control CW"
on_press:
- light.control:
id: test_light
cold_white: 0.9
warm_white: 0.1
# Test 7: light.turn_on with lambda brightness (tests lambda path)
- platform: template
id: btn_lambda_brightness
name: "Lambda Brightness"
on_press:
- light.turn_on:
id: test_light
brightness: !lambda "return id(test_brightness);"
red: 1.0
green: 0.0
blue: 0.0
# Test 8: light.turn_on with transition_length
- platform: template
id: btn_turn_on_transition
name: "Turn On Transition"
on_press:
- light.turn_on:
id: test_light
brightness: 0.5
transition_length: 0s
red: 0.5
green: 0.5
blue: 0.0

View File

@@ -0,0 +1,95 @@
"""Integration test for LightControlAction.
Tests that light.turn_on, light.turn_off, and light.control automation actions
work correctly with the compact per-field union storage. Exercises both constant
value and lambda paths.
"""
import asyncio
from typing import Any
import pytest
from .types import APIClientConnectedFactory, RunCompiledFunction
@pytest.mark.asyncio
async def test_light_control_action(
yaml_config: str,
run_compiled: RunCompiledFunction,
api_client_connected: APIClientConnectedFactory,
) -> None:
"""Test LightControlAction with constants and lambdas."""
async with run_compiled(yaml_config), api_client_connected() as client:
state_futures: dict[int, asyncio.Future[Any]] = {}
def on_state(state: Any) -> None:
if state.key in state_futures and not state_futures[state.key].done():
state_futures[state.key].set_result(state)
client.subscribe_states(on_state)
# Get entities
entities = await client.list_entities_services()
light = next(e for e in entities[0] if e.object_id == "test_light")
buttons = {e.name: e for e in entities[0] if hasattr(e, "name")}
async def wait_for_state(key: int, timeout: float = 5.0) -> Any:
"""Wait for a state change for the given entity key."""
loop = asyncio.get_running_loop()
state_futures[key] = loop.create_future()
try:
return await asyncio.wait_for(state_futures[key], timeout)
finally:
state_futures.pop(key, None)
async def press_and_wait(button_name: str) -> Any:
"""Press a button and wait for light state change."""
btn = buttons[button_name]
client.button_command(btn.key)
return await wait_for_state(light.key)
# Test 1: light.turn_on with RGB constants
state = await press_and_wait("Turn On RGB")
assert state.state is True
assert state.brightness == pytest.approx(1.0)
assert state.red == pytest.approx(0.0, abs=0.01)
assert state.green == pytest.approx(0.0, abs=0.01)
assert state.blue == pytest.approx(1.0, abs=0.01)
# Test 2: light.turn_off
state = await press_and_wait("Turn Off")
assert state.state is False
# Test 3: light.turn_on with color_temperature
state = await press_and_wait("Turn On CT")
assert state.state is True
assert state.brightness == pytest.approx(0.8)
assert state.color_temperature == pytest.approx(250.0) # 4000K = 250 mireds
# Test 4: light.turn_on with effect
state = await press_and_wait("Turn On Effect")
assert state.effect == "Test Effect"
# Test 5: Clear effect
state = await press_and_wait("Clear Effect")
assert state.effect == "None"
# Test 6: light.control with cold/warm white
state = await press_and_wait("Control CW")
assert state.cold_white == pytest.approx(0.9, abs=0.1)
assert state.warm_white == pytest.approx(0.1, abs=0.1)
# Test 7: light.turn_on with lambda brightness
# The global test_brightness is 0.75
state = await press_and_wait("Lambda Brightness")
assert state.state is True
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.0, abs=0.01)
assert state.blue == pytest.approx(0.0, abs=0.01)
# Test 8: light.turn_on with transition_length and brightness
state = await press_and_wait("Turn On Transition")
assert state.state is True
assert state.brightness == pytest.approx(0.5)