Optionally set direction on fan.turn_on action (#2171)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
WeekendWarrior1 2021-08-26 08:25:24 +10:00 committed by GitHub
parent 3be56fd502
commit de871862a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from esphome.const import (
CONF_ON_TURN_OFF,
CONF_ON_TURN_ON,
CONF_TRIGGER_ID,
CONF_DIRECTION,
)
from esphome.core import CORE, coroutine_with_priority
@ -27,6 +28,12 @@ fan_ns = cg.esphome_ns.namespace("fan")
FanState = fan_ns.class_("FanState", cg.Nameable, cg.Component)
MakeFan = cg.Application.struct("MakeFan")
FanDirection = fan_ns.enum("FanDirection")
FAN_DIRECTION_ENUM = {
"FORWARD": FanDirection.FAN_DIRECTION_FORWARD,
"REVERSE": FanDirection.FAN_DIRECTION_REVERSE,
}
# Actions
TurnOnAction = fan_ns.class_("TurnOnAction", automation.Action)
TurnOffAction = fan_ns.class_("TurnOffAction", automation.Action)
@ -143,6 +150,9 @@ async def fan_turn_off_to_code(config, action_id, template_arg, args):
cv.Required(CONF_ID): cv.use_id(FanState),
cv.Optional(CONF_OSCILLATING): cv.templatable(cv.boolean),
cv.Optional(CONF_SPEED): cv.templatable(cv.int_range(1)),
cv.Optional(CONF_DIRECTION): cv.templatable(
cv.enum(FAN_DIRECTION_ENUM, upper=True)
),
}
),
)
@ -155,6 +165,9 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args):
if CONF_SPEED in config:
template_ = await cg.templatable(config[CONF_SPEED], args, int)
cg.add(var.set_speed(template_))
if CONF_DIRECTION in config:
template_ = await cg.templatable(config[CONF_DIRECTION], args, FanDirection)
cg.add(var.set_direction(template_))
return var

View File

@ -13,6 +13,7 @@ template<typename... Ts> class TurnOnAction : public Action<Ts...> {
TEMPLATABLE_VALUE(bool, oscillating)
TEMPLATABLE_VALUE(int, speed)
TEMPLATABLE_VALUE(FanDirection, direction)
void play(Ts... x) override {
auto call = this->state_->turn_on();
@ -22,6 +23,9 @@ template<typename... Ts> class TurnOnAction : public Action<Ts...> {
if (this->speed_.has_value()) {
call.set_speed(this->speed_.value(x...));
}
if (this->direction_.has_value()) {
call.set_direction(this->direction_.value(x...));
}
call.perform();
}