mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 23:40:25 +00:00
[light] Use register_apply_condition for light.is_on and light.is_off (#19556)
This commit is contained in:
@@ -117,23 +117,6 @@ template<bool Forward, typename... Ts> class LightEffectCycleAction final : publ
|
||||
bool include_none_{false};
|
||||
};
|
||||
|
||||
template<typename... Ts> class LightIsOnCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
explicit LightIsOnCondition(LightState *state) : state_(state) {}
|
||||
bool check(const Ts &...x) override { return this->state_->current_values.is_on(); }
|
||||
|
||||
protected:
|
||||
LightState *state_;
|
||||
};
|
||||
template<typename... Ts> class LightIsOffCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
explicit LightIsOffCondition(LightState *state) : state_(state) {}
|
||||
bool check(const Ts &...x) override { return !this->state_->current_values.is_on(); }
|
||||
|
||||
protected:
|
||||
LightState *state_;
|
||||
};
|
||||
|
||||
class LightTurnOnTrigger final : public Trigger<>, public LightRemoteValuesListener {
|
||||
public:
|
||||
explicit LightTurnOnTrigger(LightState *a_light) : light_(a_light) {
|
||||
|
||||
@@ -38,8 +38,6 @@ from .types import (
|
||||
ColorMode,
|
||||
DimRelativeAction,
|
||||
LightEffectCycleAction,
|
||||
LightIsOffCondition,
|
||||
LightIsOnCondition,
|
||||
LightState,
|
||||
ToggleAction,
|
||||
)
|
||||
@@ -381,24 +379,15 @@ async def light_addressable_set_to_code(config, action_id, template_arg, args):
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
"light.is_on",
|
||||
LightIsOnCondition,
|
||||
automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(LightState),
|
||||
}
|
||||
),
|
||||
LIGHT_CONDITION_SCHEMA = automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(LightState),
|
||||
}
|
||||
)
|
||||
@automation.register_condition(
|
||||
"light.is_off",
|
||||
LightIsOffCondition,
|
||||
automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(LightState),
|
||||
}
|
||||
),
|
||||
|
||||
automation.register_apply_condition(
|
||||
"light.is_on", LIGHT_CONDITION_SCHEMA, "current_values.is_on()"
|
||||
)
|
||||
automation.register_apply_condition(
|
||||
"light.is_off", LIGHT_CONDITION_SCHEMA, "current_values.is_on() == false"
|
||||
)
|
||||
async def light_is_on_off_to_code(config, condition_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(condition_id, template_arg, paren)
|
||||
|
||||
@@ -44,8 +44,6 @@ ToggleAction = light_ns.class_("ToggleAction", automation.Action)
|
||||
LightEffectCycleAction = light_ns.class_("LightEffectCycleAction", automation.Action)
|
||||
DimRelativeAction = light_ns.class_("DimRelativeAction", automation.Action)
|
||||
AddressableSet = light_ns.class_("AddressableSet", automation.Action)
|
||||
LightIsOnCondition = light_ns.class_("LightIsOnCondition", automation.Condition)
|
||||
LightIsOffCondition = light_ns.class_("LightIsOffCondition", automation.Condition)
|
||||
|
||||
# Triggers
|
||||
LightTurnOnTrigger = light_ns.class_(
|
||||
|
||||
@@ -68,6 +68,17 @@ esphome:
|
||||
|
||||
- light.toggle: test_binary_light
|
||||
- light.turn_off: test_rgb_light
|
||||
- if:
|
||||
condition:
|
||||
light.is_on: test_binary_light
|
||||
then:
|
||||
- logger.log: binary light is on
|
||||
- if:
|
||||
condition:
|
||||
light.is_off:
|
||||
id: test_rgb_light
|
||||
then:
|
||||
- logger.log: rgb light is off
|
||||
- light.turn_on:
|
||||
id: test_rgb_light
|
||||
brightness: 100%
|
||||
|
||||
Reference in New Issue
Block a user