mirror of
https://github.com/esphome/esphome.git
synced 2025-10-05 19:33:47 +01:00
Add is_on and is_off conditions for the fan component (#2225)
Co-authored-by: Chris Nussbaum <chris.nussbaum@protolabs.com>
This commit is contained in:
@@ -42,6 +42,9 @@ ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
|
||||
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
|
||||
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
|
||||
|
||||
FanIsOnCondition = fan_ns.class_("FanIsOnCondition", automation.Condition.template())
|
||||
FanIsOffCondition = fan_ns.class_("FanIsOffCondition", automation.Condition.template())
|
||||
|
||||
FAN_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(FanState),
|
||||
@@ -171,6 +174,29 @@ async def fan_turn_on_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(FanState),
|
||||
}
|
||||
),
|
||||
)
|
||||
@automation.register_condition(
|
||||
"fan.is_off",
|
||||
FanIsOffCondition,
|
||||
automation.maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(FanState),
|
||||
}
|
||||
),
|
||||
)
|
||||
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(100.0)
|
||||
async def to_code(config):
|
||||
cg.add_define("USE_FAN")
|
||||
|
@@ -50,6 +50,23 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
||||
FanState *state_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class FanIsOnCondition : public Condition<Ts...> {
|
||||
public:
|
||||
explicit FanIsOnCondition(FanState *state) : state_(state) {}
|
||||
bool check(Ts... x) override { return this->state_->state; }
|
||||
|
||||
protected:
|
||||
FanState *state_;
|
||||
};
|
||||
template<typename... Ts> class FanIsOffCondition : public Condition<Ts...> {
|
||||
public:
|
||||
explicit FanIsOffCondition(FanState *state) : state_(state) {}
|
||||
bool check(Ts... x) override { return !this->state_->state; }
|
||||
|
||||
protected:
|
||||
FanState *state_;
|
||||
};
|
||||
|
||||
class FanTurnOnTrigger : public Trigger<> {
|
||||
public:
|
||||
FanTurnOnTrigger(FanState *state) {
|
||||
|
Reference in New Issue
Block a user