mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Fan no off cycle action (#5564)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
b632ae49d4
commit
9f033bce3b
4 changed files with 38 additions and 5 deletions
|
@ -14,6 +14,7 @@ from esphome.const import (
|
|||
CONF_SPEED_LEVEL_STATE_TOPIC,
|
||||
CONF_SPEED_COMMAND_TOPIC,
|
||||
CONF_SPEED_STATE_TOPIC,
|
||||
CONF_OFF_SPEED_CYCLE,
|
||||
CONF_ON_SPEED_SET,
|
||||
CONF_ON_TURN_OFF,
|
||||
CONF_ON_TURN_ON,
|
||||
|
@ -217,10 +218,22 @@ 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)
|
||||
@automation.register_action(
|
||||
"fan.cycle_speed",
|
||||
CycleSpeedAction,
|
||||
maybe_simple_id(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(Fan),
|
||||
cv.Optional(CONF_OFF_SPEED_CYCLE, default=True): cv.boolean,
|
||||
}
|
||||
),
|
||||
)
|
||||
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)
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
template_ = await cg.templatable(config[CONF_OFF_SPEED_CYCLE], args, bool)
|
||||
cg.add(var.set_no_off_cycle(template_))
|
||||
return var
|
||||
|
||||
|
||||
@automation.register_condition(
|
||||
|
|
|
@ -54,18 +54,26 @@ template<typename... Ts> class CycleSpeedAction : public Action<Ts...> {
|
|||
public:
|
||||
explicit CycleSpeedAction(Fan *state) : state_(state) {}
|
||||
|
||||
TEMPLATABLE_VALUE(bool, no_off_cycle)
|
||||
|
||||
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
|
||||
bool off_speed_cycle = no_off_cycle_.value(x...);
|
||||
if (speed > supported_speed_count && off_speed_cycle) {
|
||||
// was running at max speed, off speed cycle enabled, so turn off
|
||||
speed = 1;
|
||||
auto call = this->state_->turn_off();
|
||||
call.set_speed(speed);
|
||||
call.perform();
|
||||
} else if (speed > supported_speed_count && !off_speed_cycle) {
|
||||
// was running at max speed, off speed cycle disabled, so set to lowest speed
|
||||
auto call = this->state_->turn_on();
|
||||
call.set_speed(1);
|
||||
call.perform();
|
||||
} else {
|
||||
auto call = this->state_->turn_on();
|
||||
call.set_speed(speed);
|
||||
|
|
|
@ -475,6 +475,7 @@ CONF_NUM_SCANS = "num_scans"
|
|||
CONF_NUMBER = "number"
|
||||
CONF_NUMBER_DATAPOINT = "number_datapoint"
|
||||
CONF_OFF_MODE = "off_mode"
|
||||
CONF_OFF_SPEED_CYCLE = "off_speed_cycle"
|
||||
CONF_OFFSET = "offset"
|
||||
CONF_ON = "on"
|
||||
CONF_ON_BLE_ADVERTISE = "on_ble_advertise"
|
||||
|
|
|
@ -1693,7 +1693,18 @@ binary_sensor:
|
|||
number: 7
|
||||
mode: INPUT
|
||||
inverted: false
|
||||
|
||||
- platform: gpio
|
||||
name: Speed Fan Cycle binary sensor"
|
||||
pin:
|
||||
number: 18
|
||||
mode:
|
||||
input: true
|
||||
pulldown: true
|
||||
on_press:
|
||||
- fan.cycle_speed:
|
||||
id: fan_speed
|
||||
off_speed_cycle: False
|
||||
- logger.log: "Cycle speed clicked"
|
||||
- platform: remote_receiver
|
||||
name: Raw Remote Receiver Test
|
||||
raw:
|
||||
|
|
Loading…
Reference in a new issue