mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 23:40:25 +00:00
[fan] Use register_apply_condition for fan.is_on and fan.is_off (#19555)
This commit is contained in:
@@ -83,9 +83,6 @@ FanPresetSetTrigger = fan_ns.class_(
|
||||
"FanPresetSetTrigger", automation.Trigger.template(cg.StringRef)
|
||||
)
|
||||
|
||||
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
|
||||
FanIsOffCondition = fan_ns.class_("FanIsOffCondition", automation.Condition.template())
|
||||
|
||||
_FAN_SCHEMA = (
|
||||
cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA)
|
||||
.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA)
|
||||
@@ -367,27 +364,16 @@ async def fan_cycle_speed_to_code(config, action_id, template_arg, args):
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
"fan.is_on",
|
||||
FanIsOnCondition,
|
||||
automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Fan),
|
||||
}
|
||||
),
|
||||
FAN_CONDITION_SCHEMA = automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Fan),
|
||||
}
|
||||
)
|
||||
@automation.register_condition(
|
||||
"fan.is_off",
|
||||
FanIsOffCondition,
|
||||
automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Fan),
|
||||
}
|
||||
),
|
||||
|
||||
automation.register_apply_condition("fan.is_on", FAN_CONDITION_SCHEMA, "state")
|
||||
automation.register_apply_condition(
|
||||
"fan.is_off", FAN_CONDITION_SCHEMA, "state == false"
|
||||
)
|
||||
async def fan_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)
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.CORE)
|
||||
|
||||
@@ -68,23 +68,6 @@ template<typename... Ts> class CycleSpeedAction final : public Action<Ts...> {
|
||||
Fan *state_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class FanIsOnCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
explicit FanIsOnCondition(Fan *state) : state_(state) {}
|
||||
bool check(const Ts &...x) override { return this->state_->state; }
|
||||
|
||||
protected:
|
||||
Fan *state_;
|
||||
};
|
||||
template<typename... Ts> class FanIsOffCondition final : public Condition<Ts...> {
|
||||
public:
|
||||
explicit FanIsOffCondition(Fan *state) : state_(state) {}
|
||||
bool check(const Ts &...x) override { return !this->state_->state; }
|
||||
|
||||
protected:
|
||||
Fan *state_;
|
||||
};
|
||||
|
||||
class FanStateTrigger final : public Trigger<Fan *> {
|
||||
public:
|
||||
FanStateTrigger(Fan *state) : fan_(state) {
|
||||
|
||||
@@ -96,6 +96,20 @@ button:
|
||||
- fan.turn_on:
|
||||
id: test_fan
|
||||
speed: !lambda 'return 1;'
|
||||
- platform: template
|
||||
name: "Fan Is On Off"
|
||||
on_press:
|
||||
- if:
|
||||
condition:
|
||||
fan.is_on: test_fan
|
||||
then:
|
||||
- fan.turn_off: test_fan
|
||||
- if:
|
||||
condition:
|
||||
fan.is_off:
|
||||
id: test_fan
|
||||
then:
|
||||
- fan.turn_on: test_fan
|
||||
|
||||
# Exercise fan.turn_on inside triggers with non-empty Ts:
|
||||
# - number.on_value: Ts = float (Python value type; previously raised
|
||||
@@ -111,6 +125,10 @@ number:
|
||||
step: 1
|
||||
on_value:
|
||||
then:
|
||||
- fan.turn_on:
|
||||
id: test_fan
|
||||
speed: !lambda "return (int) x;"
|
||||
- if:
|
||||
condition:
|
||||
fan.is_on: test_fan
|
||||
then:
|
||||
- fan.turn_on:
|
||||
id: test_fan
|
||||
speed: !lambda "return (int) x;"
|
||||
|
||||
Reference in New Issue
Block a user