add fan.cycle_speed action (#2329)

This commit is contained in:
WeekendWarrior1 2021-09-27 05:32:46 +10:00 committed by GitHub
parent 7246f42a8e
commit 4d28afc153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -41,6 +41,7 @@ FAN_DIRECTION_ENUM = {
TurnOnAction = fan_ns.class_("TurnOnAction", automation.Action)
TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action)
ToggleAction = fan_ns.class_("ToggleAction", automation.Action)
CycleSpeedAction = fan_ns.class_("CycleSpeedAction", automation.Action)
FanTurnOnTrigger = fan_ns.class_("FanTurnOnTrigger", 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
@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(
"fan.is_on",
FanIsOnCondition,

View File

@ -50,6 +50,42 @@ template<typename... Ts> class ToggleAction : public Action<Ts...> {
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...> {
public:
explicit FanIsOnCondition(FanState *state) : state_(state) {}