mirror of
https://github.com/esphome/esphome.git
synced 2024-12-19 16:07:47 +01:00
add fan.cycle_speed action (#2329)
This commit is contained in:
parent
7246f42a8e
commit
4d28afc153
@ -41,6 +41,7 @@ FAN_DIRECTION_ENUM = {
|
|||||||
TurnOnAction = fan_ns.class_("TurnOnAction", automation.Action)
|
TurnOnAction = fan_ns.class_("TurnOnAction", automation.Action)
|
||||||
TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action)
|
TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action)
|
||||||
ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
|
ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
|
||||||
|
CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action)
|
||||||
|
|
||||||
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
|
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", automation.Trigger.template())
|
||||||
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
|
FanTurnOffTrigger = fan_ns.class_("FanTurnOffTrigger", automation.Trigger.template())
|
||||||
@ -204,6 +205,12 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args):
|
|||||||
return var
|
return var
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action("fan.cycle_speed", CycleSpeedAction, FAN_ACTION_SCHEMA)
|
||||||
|
async def fan_cycle_speed_to_code(config, action_id, template_arg, args):
|
||||||
|
paren = await cg.get_variable(config[CONF_ID])
|
||||||
|
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||||
|
|
||||||
|
|
||||||
@automation.register_condition(
|
@automation.register_condition(
|
||||||
"fan.is_on",
|
"fan.is_on",
|
||||||
FanIsOnCondition,
|
FanIsOnCondition,
|
||||||
|
@ -50,6 +50,42 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
|
|||||||
FanState *state_;
|
FanState *state_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class CycleSpeedAction : public Action<Ts...> {
|
||||||
|
public:
|
||||||
|
explicit CycleSpeedAction(FanState *state) : state_(state) {}
|
||||||
|
|
||||||
|
void play(Ts... x) override {
|
||||||
|
// check to see if fan supports speeds and is on
|
||||||
|
if (this->state_->get_traits().supported_speed_count()) {
|
||||||
|
if (this->state_->state) {
|
||||||
|
int speed = this->state_->speed + 1;
|
||||||
|
int supported_speed_count = this->state_->get_traits().supported_speed_count();
|
||||||
|
if (speed > supported_speed_count) {
|
||||||
|
// was running at max speed, so turn off
|
||||||
|
speed = 1;
|
||||||
|
auto call = this->state_->turn_off();
|
||||||
|
call.set_speed(speed);
|
||||||
|
call.perform();
|
||||||
|
} else {
|
||||||
|
auto call = this->state_->turn_on();
|
||||||
|
call.set_speed(speed);
|
||||||
|
call.perform();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// fan was off, so set speed to 1
|
||||||
|
auto call = this->state_->turn_on();
|
||||||
|
call.set_speed(1);
|
||||||
|
call.perform();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// fan doesn't support speed counts, so toggle
|
||||||
|
this->state_->toggle().perform();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FanState *state_;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename... Ts> class FanIsOnCondition : public Condition<Ts...> {
|
template<typename... Ts> class FanIsOnCondition : public Condition<Ts...> {
|
||||||
public:
|
public:
|
||||||
explicit FanIsOnCondition(FanState *state) : state_(state) {}
|
explicit FanIsOnCondition(FanState *state) : state_(state) {}
|
||||||
|
Loading…
Reference in New Issue
Block a user