mirror of
https://github.com/esphome/esphome.git
synced 2024-11-26 00:48:19 +01:00
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:
parent
3be56fd502
commit
de871862a8
2 changed files with 17 additions and 0 deletions
|
@ -18,6 +18,7 @@ from esphome.const import (
|
||||||
CONF_ON_TURN_OFF,
|
CONF_ON_TURN_OFF,
|
||||||
CONF_ON_TURN_ON,
|
CONF_ON_TURN_ON,
|
||||||
CONF_TRIGGER_ID,
|
CONF_TRIGGER_ID,
|
||||||
|
CONF_DIRECTION,
|
||||||
)
|
)
|
||||||
from esphome.core import CORE, coroutine_with_priority
|
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)
|
FanState = fan_ns.class_("FanState", cg.Nameable, cg.Component)
|
||||||
MakeFan = cg.Application.struct("MakeFan")
|
MakeFan = cg.Application.struct("MakeFan")
|
||||||
|
|
||||||
|
FanDirection = fan_ns.enum("FanDirection")
|
||||||
|
FAN_DIRECTION_ENUM = {
|
||||||
|
"FORWARD": FanDirection.FAN_DIRECTION_FORWARD,
|
||||||
|
"REVERSE": FanDirection.FAN_DIRECTION_REVERSE,
|
||||||
|
}
|
||||||
|
|
||||||
# Actions
|
# Actions
|
||||||
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)
|
||||||
|
@ -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.Required(CONF_ID): cv.use_id(FanState),
|
||||||
cv.Optional(CONF_OSCILLATING): cv.templatable(cv.boolean),
|
cv.Optional(CONF_OSCILLATING): cv.templatable(cv.boolean),
|
||||||
cv.Optional(CONF_SPEED): cv.templatable(cv.int_range(1)),
|
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:
|
if CONF_SPEED in config:
|
||||||
template_ = await cg.templatable(config[CONF_SPEED], args, int)
|
template_ = await cg.templatable(config[CONF_SPEED], args, int)
|
||||||
cg.add(var.set_speed(template_))
|
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
|
return var
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ template<typename... Ts> class TurnOnAction : public Action<Ts...> {
|
||||||
|
|
||||||
TEMPLATABLE_VALUE(bool, oscillating)
|
TEMPLATABLE_VALUE(bool, oscillating)
|
||||||
TEMPLATABLE_VALUE(int, speed)
|
TEMPLATABLE_VALUE(int, speed)
|
||||||
|
TEMPLATABLE_VALUE(FanDirection, direction)
|
||||||
|
|
||||||
void play(Ts... x) override {
|
void play(Ts... x) override {
|
||||||
auto call = this->state_->turn_on();
|
auto call = this->state_->turn_on();
|
||||||
|
@ -22,6 +23,9 @@ template<typename... Ts> class TurnOnAction : public Action<Ts...> {
|
||||||
if (this->speed_.has_value()) {
|
if (this->speed_.has_value()) {
|
||||||
call.set_speed(this->speed_.value(x...));
|
call.set_speed(this->speed_.value(x...));
|
||||||
}
|
}
|
||||||
|
if (this->direction_.has_value()) {
|
||||||
|
call.set_direction(this->direction_.value(x...));
|
||||||
|
}
|
||||||
call.perform();
|
call.perform();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue