[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

@@ -24,46 +24,51 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
LightState *state_; LightState *state_;
}; };
/// Compact light control action — each field is a function pointer (nullptr = unset).
/// Codegen wraps constants in stateless lambdas. 72 bytes vs 128 with TemplatableValue.
template<typename... Ts> class LightControlAction : public Action<Ts...> { template<typename... Ts> class LightControlAction : public Action<Ts...> {
public: public:
explicit LightControlAction(LightState *parent) : parent_(parent) {} explicit LightControlAction(LightState *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(ColorMode, color_mode) #define LIGHT_CONTROL_FIELDS(X) \
TEMPLATABLE_VALUE(bool, state) X(ColorMode, color_mode) \
TEMPLATABLE_VALUE(uint32_t, transition_length) X(bool, state) \
TEMPLATABLE_VALUE(uint32_t, flash_length) X(uint32_t, transition_length) \
TEMPLATABLE_VALUE(float, brightness) X(uint32_t, flash_length) \
TEMPLATABLE_VALUE(float, color_brightness) X(float, brightness) \
TEMPLATABLE_VALUE(float, red) X(float, color_brightness) \
TEMPLATABLE_VALUE(float, green) X(float, red) \
TEMPLATABLE_VALUE(float, blue) X(float, green) \
TEMPLATABLE_VALUE(float, white) X(float, blue) \
TEMPLATABLE_VALUE(float, color_temperature) X(float, white) \
TEMPLATABLE_VALUE(float, cold_white) X(float, color_temperature) \
TEMPLATABLE_VALUE(float, warm_white) X(float, cold_white) \
TEMPLATABLE_VALUE(uint32_t, effect) X(float, warm_white) \
X(uint32_t, effect)
#define LIGHT_FIELD_SETTER_(type, name) \
void set_##name(type (*f)(Ts...)) { this->name##_ = f; }
#define LIGHT_FIELD_APPLY_(type, name) \
if (this->name##_) \
call.set_##name(this->name##_(x...));
#define LIGHT_FIELD_DECL_(type, name) type (*name##_)(Ts...){nullptr};
LIGHT_CONTROL_FIELDS(LIGHT_FIELD_SETTER_)
void play(const Ts &...x) override { void play(const Ts &...x) override {
auto call = this->parent_->make_call(); auto call = this->parent_->make_call();
call.set_color_mode(this->color_mode_.optional_value(x...)); LIGHT_CONTROL_FIELDS(LIGHT_FIELD_APPLY_)
call.set_state(this->state_.optional_value(x...));
call.set_brightness(this->brightness_.optional_value(x...));
call.set_color_brightness(this->color_brightness_.optional_value(x...));
call.set_red(this->red_.optional_value(x...));
call.set_green(this->green_.optional_value(x...));
call.set_blue(this->blue_.optional_value(x...));
call.set_white(this->white_.optional_value(x...));
call.set_color_temperature(this->color_temperature_.optional_value(x...));
call.set_cold_white(this->cold_white_.optional_value(x...));
call.set_warm_white(this->warm_white_.optional_value(x...));
call.set_effect(this->effect_.optional_value(x...));
call.set_flash_length(this->flash_length_.optional_value(x...));
call.set_transition_length(this->transition_length_.optional_value(x...));
call.perform(); call.perform();
} }
protected: protected:
LightState *parent_; LightState *parent_;
LIGHT_CONTROL_FIELDS(LIGHT_FIELD_DECL_)
#undef LIGHT_FIELD_DECL_
#undef LIGHT_FIELD_APPLY_
#undef LIGHT_FIELD_SETTER_
#undef LIGHT_CONTROL_FIELDS
}; };
template<typename... Ts> class DimRelativeAction : public Action<Ts...> { template<typename... Ts> class DimRelativeAction : public Action<Ts...> {

View File

@@ -1,3 +1,5 @@
from typing import Any
from esphome import automation from esphome import automation
import esphome.codegen as cg import esphome.codegen as cg
from esphome.config import path_context from esphome.config import path_context
@@ -28,7 +30,7 @@ from esphome.const import (
) )
from esphome.core import CORE, EsphomeError, Lambda from esphome.core import CORE, EsphomeError, Lambda
from esphome.cpp_generator import LambdaExpression from esphome.cpp_generator import LambdaExpression
from esphome.types import ConfigType from esphome.types import ConfigType, SafeExpType
from .types import ( from .types import (
COLOR_MODES, COLOR_MODES,
@@ -141,6 +143,28 @@ LIGHT_TURN_ON_ACTION_SCHEMA = automation.maybe_simple_id(
) )
async def _as_lambda(
value: Any,
args: list[tuple[SafeExpType, str]],
output_type: SafeExpType,
) -> LambdaExpression:
"""Return a stateless lambda expression for a templatable value.
If value is already a lambda, process it normally. Otherwise wrap
the constant in a ``[](...) -> T { return <value>; }`` expression
so that LightControlAction can store every field as a plain
function pointer.
"""
if cg.is_template(value):
return await cg.process_lambda(value, args, return_type=output_type)
return LambdaExpression(
f"return {cg.safe_exp(value)};",
args,
capture="",
return_type=output_type,
)
def _resolve_effect_index(config: ConfigType) -> int: def _resolve_effect_index(config: ConfigType) -> int:
"""Resolve a static effect name to its 1-based index at codegen time. """Resolve a static effect name to its 1-based index at codegen time.
@@ -179,47 +203,29 @@ def _resolve_effect_index(config: ConfigType) -> int:
async def light_control_to_code(config, action_id, template_arg, args): async def light_control_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
if CONF_COLOR_MODE in config:
template_ = await cg.templatable(config[CONF_COLOR_MODE], args, ColorMode) # (config_key, setter_name, c++ type)
cg.add(var.set_color_mode(template_)) FIELDS = (
if CONF_STATE in config: (CONF_COLOR_MODE, "set_color_mode", ColorMode),
template_ = await cg.templatable(config[CONF_STATE], args, bool) (CONF_STATE, "set_state", bool),
cg.add(var.set_state(template_)) (CONF_TRANSITION_LENGTH, "set_transition_length", cg.uint32),
if CONF_TRANSITION_LENGTH in config: (CONF_FLASH_LENGTH, "set_flash_length", cg.uint32),
template_ = await cg.templatable( (CONF_BRIGHTNESS, "set_brightness", float),
config[CONF_TRANSITION_LENGTH], args, cg.uint32 (CONF_COLOR_BRIGHTNESS, "set_color_brightness", float),
) (CONF_RED, "set_red", float),
cg.add(var.set_transition_length(template_)) (CONF_GREEN, "set_green", float),
if CONF_FLASH_LENGTH in config: (CONF_BLUE, "set_blue", float),
template_ = await cg.templatable(config[CONF_FLASH_LENGTH], args, cg.uint32) (CONF_WHITE, "set_white", float),
cg.add(var.set_flash_length(template_)) (CONF_COLOR_TEMPERATURE, "set_color_temperature", float),
if CONF_BRIGHTNESS in config: (CONF_COLD_WHITE, "set_cold_white", float),
template_ = await cg.templatable(config[CONF_BRIGHTNESS], args, float) (CONF_WARM_WHITE, "set_warm_white", float),
cg.add(var.set_brightness(template_)) )
if CONF_COLOR_BRIGHTNESS in config: for conf_key, setter, type_ in FIELDS:
template_ = await cg.templatable(config[CONF_COLOR_BRIGHTNESS], args, float) if conf_key in config:
cg.add(var.set_color_brightness(template_)) cg.add(
if CONF_RED in config: getattr(var, setter)(await _as_lambda(config[conf_key], args, type_))
template_ = await cg.templatable(config[CONF_RED], args, float) )
cg.add(var.set_red(template_))
if CONF_GREEN in config:
template_ = await cg.templatable(config[CONF_GREEN], args, float)
cg.add(var.set_green(template_))
if CONF_BLUE in config:
template_ = await cg.templatable(config[CONF_BLUE], args, float)
cg.add(var.set_blue(template_))
if CONF_WHITE in config:
template_ = await cg.templatable(config[CONF_WHITE], args, float)
cg.add(var.set_white(template_))
if CONF_COLOR_TEMPERATURE in config:
template_ = await cg.templatable(config[CONF_COLOR_TEMPERATURE], args, float)
cg.add(var.set_color_temperature(template_))
if CONF_COLD_WHITE in config:
template_ = await cg.templatable(config[CONF_COLD_WHITE], args, float)
cg.add(var.set_cold_white(template_))
if CONF_WARM_WHITE in config:
template_ = await cg.templatable(config[CONF_WARM_WHITE], args, float)
cg.add(var.set_warm_white(template_))
if CONF_EFFECT in config: if CONF_EFFECT in config:
if isinstance(config[CONF_EFFECT], Lambda): if isinstance(config[CONF_EFFECT], Lambda):
# Lambda returns a string — wrap in a C++ lambda that resolves # Lambda returns a string — wrap in a C++ lambda that resolves
@@ -242,8 +248,11 @@ async def light_control_to_code(config, action_id, template_arg, args):
cg.add(var.set_effect(wrapper)) cg.add(var.set_effect(wrapper))
else: else:
# Static string — resolve effect name to index at codegen time # Static string — resolve effect name to index at codegen time
effect_index = _resolve_effect_index(config) cg.add(
cg.add(var.set_effect(effect_index)) var.set_effect(
await _as_lambda(_resolve_effect_index(config), args, cg.uint32)
)
)
return var return var

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)