From ebfdedfcdb088871c24204370bf36c14928065c4 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jan 2024 17:24:00 +0000 Subject: [PATCH 1/3] Add preset argument to fan TurnOnAction --- esphome/components/fan/__init__.py | 5 +++++ esphome/components/fan/automation.h | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index fd0f2f66cb..b292aad029 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -21,6 +21,7 @@ from esphome.const import ( CONF_ON_PRESET_SET, CONF_TRIGGER_ID, CONF_DIRECTION, + CONF_PRESET, CONF_RESTORE_MODE, ) from esphome.core import CORE, coroutine_with_priority @@ -244,6 +245,7 @@ async def fan_turn_off_to_code(config, action_id, template_arg, args): cv.Optional(CONF_DIRECTION): cv.templatable( cv.enum(FAN_DIRECTION_ENUM, upper=True) ), + cv.Optional(CONF_PRESET): cv.templatable(cv.string), } ), ) @@ -259,6 +261,9 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args): if CONF_DIRECTION in config: template_ = await cg.templatable(config[CONF_DIRECTION], args, FanDirection) cg.add(var.set_direction(template_)) + if CONF_PRESET in config: + template_ = await cg.templatable(config[CONF_PRESET], args, cg.std_string) + cg.add(var.set_preset_mode(template_)) return var diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index b5bdeb8a29..922c78553a 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -14,6 +14,7 @@ template class TurnOnAction : public Action { TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(int, speed) TEMPLATABLE_VALUE(FanDirection, direction) + TEMPLATABLE_VALUE(std::string, preset_mode) void play(Ts... x) override { auto call = this->state_->turn_on(); @@ -26,6 +27,9 @@ template class TurnOnAction : public Action { if (this->direction_.has_value()) { call.set_direction(this->direction_.value(x...)); } + if (this->preset_mode_.has_value()) { + call.set_preset_mode(this->preset_mode_.value(x...)); + } call.perform(); } From 2482287f7902156c1141d9e8fe5a83770b91a929 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Wed, 17 Jan 2024 21:31:52 +0000 Subject: [PATCH 2/3] Add compile tests for preset option to fan.turn_on --- tests/test1.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test1.yaml b/tests/test1.yaml index afd199d264..ef321396ac 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -4159,6 +4159,20 @@ button: memory_location: 0xA0 memory_address: 0x7D memory_data: 0x0F + - platform: template + name: Fan Turn On Action w/ Preset + on_press: + then: + - fan.turn_on: + id: fan_speed_presets + preset: Preset 1 + - platform: template + name: Fan Turn On Action w/ Preset Template + on_press: + then: + - fan.turn_on: + id: fan_hbridge_presets + preset: !lambda 'return "Preset 2";' ld2410: id: my_ld2410 From cb7068bd3c8bdbdc2d658cdf9c0e1acf26841064 Mon Sep 17 00:00:00 2001 From: Tucker Kern Date: Fri, 22 Mar 2024 17:37:09 +0000 Subject: [PATCH 3/3] Rename "preset" to "preset_mode" to stay in line with the other uses in Fan --- esphome/components/fan/__init__.py | 8 ++++---- esphome/const.py | 1 + tests/test1.yaml | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index b292aad029..7b159de9a0 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -21,7 +21,7 @@ from esphome.const import ( CONF_ON_PRESET_SET, CONF_TRIGGER_ID, CONF_DIRECTION, - CONF_PRESET, + CONF_PRESET_MODE, CONF_RESTORE_MODE, ) from esphome.core import CORE, coroutine_with_priority @@ -245,7 +245,7 @@ async def fan_turn_off_to_code(config, action_id, template_arg, args): cv.Optional(CONF_DIRECTION): cv.templatable( cv.enum(FAN_DIRECTION_ENUM, upper=True) ), - cv.Optional(CONF_PRESET): cv.templatable(cv.string), + cv.Optional(CONF_PRESET_MODE): cv.templatable(cv.string), } ), ) @@ -261,8 +261,8 @@ async def fan_turn_on_to_code(config, action_id, template_arg, args): if CONF_DIRECTION in config: template_ = await cg.templatable(config[CONF_DIRECTION], args, FanDirection) cg.add(var.set_direction(template_)) - if CONF_PRESET in config: - template_ = await cg.templatable(config[CONF_PRESET], args, cg.std_string) + if CONF_PRESET_MODE in config: + template_ = await cg.templatable(config[CONF_PRESET_MODE], args, cg.std_string) cg.add(var.set_preset_mode(template_)) return var diff --git a/esphome/const.py b/esphome/const.py index c35adc74ee..bbece14c4f 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -615,6 +615,7 @@ CONF_PRESET = "preset" CONF_PRESET_BOOST = "preset_boost" CONF_PRESET_COMMAND_TOPIC = "preset_command_topic" CONF_PRESET_ECO = "preset_eco" +CONF_PRESET_MODE = "preset_mode" CONF_PRESET_MODES = "preset_modes" CONF_PRESET_SLEEP = "preset_sleep" CONF_PRESET_STATE_TOPIC = "preset_state_topic" diff --git a/tests/test1.yaml b/tests/test1.yaml index ef321396ac..b579799b29 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -4165,14 +4165,14 @@ button: then: - fan.turn_on: id: fan_speed_presets - preset: Preset 1 + preset_mode: Preset 1 - platform: template name: Fan Turn On Action w/ Preset Template on_press: then: - fan.turn_on: id: fan_hbridge_presets - preset: !lambda 'return "Preset 2";' + preset_mode: !lambda 'return "Preset 2";' ld2410: id: my_ld2410