diff --git a/esphome/components/airton/switch/__init__.py b/esphome/components/airton/switch/__init__.py index 94cbb5c8c4a..88200891f26 100644 --- a/esphome/components/airton/switch/__init__.py +++ b/esphome/components/airton/switch/__init__.py @@ -8,19 +8,18 @@ from ..climate import CONF_AIRTON_ID, CONF_SLEEP_MODE, AirtonClimate, airton_ns CODEOWNERS = ["@Procsiab"] -AirtonSwitch = airton_ns.class_("AirtonSwitch", switch.Switch) AirtonSetting = airton_ns.enum("AirtonSetting", is_class=True) CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_AIRTON_ID): cv.use_id(AirtonClimate), cv.Optional(CONF_DISPLAY): switch.switch_schema( - AirtonSwitch, + switch.ApplySwitch, entity_category=ENTITY_CATEGORY_CONFIG, default_restore_mode="DISABLED", ), cv.Optional(CONF_SLEEP_MODE): switch.switch_schema( - AirtonSwitch, + switch.ApplySwitch, entity_category=ENTITY_CATEGORY_CONFIG, default_restore_mode="DISABLED", ), @@ -32,10 +31,12 @@ async def to_code(config: ConfigType) -> None: hub = await cg.get_variable(config[CONF_AIRTON_ID]) switches = switch.sub_switches(config, parent=hub) await switches( - CONF_DISPLAY, hub.set_display_switch, AirtonSetting.AIRTON_SETTING_DISPLAY + CONF_DISPLAY, + hub.set_display_switch, + apply=f"set_setting({AirtonSetting.AIRTON_SETTING_DISPLAY}, state)", ) await switches( CONF_SLEEP_MODE, hub.set_sleep_mode_switch, - AirtonSetting.AIRTON_SETTING_SLEEP_MODE, + apply=f"set_setting({AirtonSetting.AIRTON_SETTING_SLEEP_MODE}, state)", ) diff --git a/esphome/components/airton/switch/airton_switch.h b/esphome/components/airton/switch/airton_switch.h deleted file mode 100644 index d887bacad64..00000000000 --- a/esphome/components/airton/switch/airton_switch.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "esphome/components/switch/switch.h" -#include "../airton.h" - -namespace esphome::airton { - -// One of the unit's on/off settings; the parent sends the state and publishes it back -class AirtonSwitch : public switch_::Switch, public Parented { - public: - explicit AirtonSwitch(AirtonSetting setting) : setting_(setting) {} - - protected: - void write_state(bool state) override { this->parent_->set_setting(this->setting_, state); } - - AirtonSetting setting_; -}; - -} // namespace esphome::airton diff --git a/esphome/components/dfrobot_sen0609/select/__init__.py b/esphome/components/dfrobot_sen0609/select/__init__.py index a68ed8c8729..6f1ff32888f 100644 --- a/esphome/components/dfrobot_sen0609/select/__init__.py +++ b/esphome/components/dfrobot_sen0609/select/__init__.py @@ -5,15 +5,13 @@ import esphome.config_validation as cv from esphome.const import CONF_ID, ENTITY_CATEGORY_CONFIG, ICON_SCALE from esphome.types import ConfigType -from .. import CONF_DFROBOT_SEN0609_ID, DFRobotSEN0609Component, dfrobot_sen0609_ns - -OutPinLevelSelect = dfrobot_sen0609_ns.class_("OutPinLevelSelect", select.Select) +from .. import CONF_DFROBOT_SEN0609_ID, DFRobotSEN0609Component CONFIG_SCHEMA = { cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component), cv.Optional(CONF_OUT_PIN_LEVEL): select.select_schema( - OutPinLevelSelect, + select.ApplySelect, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_SCALE, ), @@ -25,5 +23,8 @@ async def to_code(config: ConfigType) -> None: selects = select.sub_selects(config, parent=hub) # The level the OUT pin has while a target is present; the index is the module's value await selects( - CONF_OUT_PIN_LEVEL, hub.set_out_pin_level_select, options=["low", "high"] + CONF_OUT_PIN_LEVEL, + hub.set_out_pin_level_select, + options=["low", "high"], + apply="control_out_pin_level(index)", ) diff --git a/esphome/components/dfrobot_sen0609/select/dfrobot_sen0609_select.h b/esphome/components/dfrobot_sen0609/select/dfrobot_sen0609_select.h deleted file mode 100644 index 0ea2fdd8974..00000000000 --- a/esphome/components/dfrobot_sen0609/select/dfrobot_sen0609_select.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/select/select.h" -#include "../dfrobot_sen0609.h" - -namespace esphome::dfrobot_sen0609 { - -class OutPinLevelSelect final : public select::Select, public Parented { - public: - // User provided, not "= default": `new(p) OutPinLevelSelect()` would zero-fill .bss that is already zero. - OutPinLevelSelect() {} - - protected: - void control(size_t index) override { this->parent_->control_out_pin_level(index); } -}; - -} // namespace esphome::dfrobot_sen0609 diff --git a/esphome/components/dfrobot_sen0609/switch/__init__.py b/esphome/components/dfrobot_sen0609/switch/__init__.py index 3baa9656f4c..c89a9703ef1 100644 --- a/esphome/components/dfrobot_sen0609/switch/__init__.py +++ b/esphome/components/dfrobot_sen0609/switch/__init__.py @@ -18,7 +18,6 @@ from .. import ( final_validate_mode, ) -DFRobotSEN0609Switch = dfrobot_sen0609_ns.class_("DFRobotSEN0609Switch", switch.Switch) SwitchSetting = dfrobot_sen0609_ns.enum("SwitchSetting") CONF_MICRO_MOTION = "micro_motion" @@ -29,7 +28,7 @@ CONF_RUN_LED = "run_led" def _led() -> cv.Schema: # The module forgets the LED state at power off, so it is restored from here return switch.switch_schema( - DFRobotSEN0609Switch, + switch.ApplySwitch, default_restore_mode="RESTORE_DEFAULT_ON", device_class=DEVICE_CLASS_SWITCH, entity_category=ENTITY_CATEGORY_CONFIG, @@ -41,7 +40,7 @@ CONFIG_SCHEMA = { cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component), cv.Optional(CONF_MICRO_MOTION): switch.switch_schema( - DFRobotSEN0609Switch, + switch.ApplySwitch, device_class=DEVICE_CLASS_SWITCH, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_MOTION_SENSOR, @@ -64,5 +63,7 @@ async def to_code(config: ConfigType) -> None: (CONF_OUT_LED, SwitchSetting.SWITCH_OUT_LED), ): await switches( - key, lambda s, setting=setting: hub.set_switch(setting, s), setting + key, + lambda s, setting=setting: hub.set_switch(setting, s), + apply=f"control_switch({setting}, state)", ) diff --git a/esphome/components/dfrobot_sen0609/switch/dfrobot_sen0609_switch.h b/esphome/components/dfrobot_sen0609/switch/dfrobot_sen0609_switch.h deleted file mode 100644 index dc2def0d809..00000000000 --- a/esphome/components/dfrobot_sen0609/switch/dfrobot_sen0609_switch.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "esphome/components/switch/switch.h" -#include "../dfrobot_sen0609.h" - -namespace esphome::dfrobot_sen0609 { - -class DFRobotSEN0609Switch final : public switch_::Switch, public Parented { - public: - explicit DFRobotSEN0609Switch(SwitchSetting setting) : setting_(setting) {} - - protected: - void write_state(bool state) override { this->parent_->control_switch(this->setting_, state); } - - SwitchSetting setting_; -}; - -} // namespace esphome::dfrobot_sen0609