From 8b951fa620e83992aa0c2f7776fb69744b77dd6a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 20 Oct 2026 02:52:26 -1000 Subject: [PATCH] [haier_tundra][tm1638][bl0940][micronova] Use Apply entities for hub forwarding buttons and switches (#21148) --- esphome/components/bl0940/bl0940.cpp | 1 + esphome/components/bl0940/button/__init__.py | 27 +++++++------------ .../button/calibration_reset_button.cpp | 18 ------------- .../bl0940/button/calibration_reset_button.h | 17 ------------ .../haier_tundra/button/__init__.py | 23 +++++----------- .../button/haier_tundra_button.cpp | 18 ------------- .../haier_tundra/button/haier_tundra_button.h | 25 ----------------- .../components/micronova/button/__init__.py | 22 +++++++-------- .../micronova/button/micronova_button.cpp | 16 ----------- .../micronova/button/micronova_button.h | 23 ---------------- esphome/components/tm1638/switch/__init__.py | 27 +++++++------------ .../tm1638/switch/tm1638_switch_led.cpp | 18 ------------- .../tm1638/switch/tm1638_switch_led.h | 21 --------------- 13 files changed, 35 insertions(+), 221 deletions(-) delete mode 100644 esphome/components/bl0940/button/calibration_reset_button.cpp delete mode 100644 esphome/components/bl0940/button/calibration_reset_button.h delete mode 100644 esphome/components/haier_tundra/button/haier_tundra_button.cpp delete mode 100644 esphome/components/haier_tundra/button/haier_tundra_button.h delete mode 100644 esphome/components/micronova/button/micronova_button.cpp delete mode 100644 esphome/components/micronova/button/micronova_button.h delete mode 100644 esphome/components/tm1638/switch/tm1638_switch_led.cpp delete mode 100644 esphome/components/tm1638/switch/tm1638_switch_led.h diff --git a/esphome/components/bl0940/bl0940.cpp b/esphome/components/bl0940/bl0940.cpp index ac1386df03a..82153fa5b43 100644 --- a/esphome/components/bl0940/bl0940.cpp +++ b/esphome/components/bl0940/bl0940.cpp @@ -122,6 +122,7 @@ float BL0940::calculate_energy_reference_() { float BL0940::calculate_calibration_value_(float state) { return (100 + state) / 100; } void BL0940::reset_calibration() { + ESP_LOGI(TAG, "Resetting calibration defaults..."); #ifdef USE_NUMBER if (this->current_calibration_number_ != nullptr && this->current_cal_ != 1) { this->current_calibration_number_->make_call().set_value(0).perform(); diff --git a/esphome/components/bl0940/button/__init__.py b/esphome/components/bl0940/button/__init__.py index e87a647392f..690fd306163 100644 --- a/esphome/components/bl0940/button/__init__.py +++ b/esphome/components/bl0940/button/__init__.py @@ -1,28 +1,19 @@ -import esphome.codegen as cg from esphome.components import button import esphome.config_validation as cv from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_RESTART from esphome.types import ConfigType -from .. import CONF_BL0940_ID, bl0940_ns +from .. import CONF_BL0940_ID from ..sensor import BL0940 -CalibrationResetButton = bl0940_ns.class_( - "CalibrationResetButton", button.Button, cg.Component -) - -CONFIG_SCHEMA = cv.All( - button.button_schema( - CalibrationResetButton, - entity_category=ENTITY_CATEGORY_CONFIG, - icon=ICON_RESTART, - ) - .extend({cv.GenerateID(CONF_BL0940_ID): cv.use_id(BL0940)}) - .extend(cv.COMPONENT_SCHEMA) -) +CONFIG_SCHEMA = button.button_schema( + button.ApplyButton, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_RESTART, +).extend({cv.GenerateID(CONF_BL0940_ID): cv.use_id(BL0940)}) async def to_code(config: ConfigType) -> None: - var = await button.new_button(config) - await cg.register_component(var, config) - await cg.register_parented(var, config[CONF_BL0940_ID]) + await button.sub_buttons(config, parent=config[CONF_BL0940_ID]).create( + config, None, apply="reset_calibration()" + ) diff --git a/esphome/components/bl0940/button/calibration_reset_button.cpp b/esphome/components/bl0940/button/calibration_reset_button.cpp deleted file mode 100644 index 5598818f546..00000000000 --- a/esphome/components/bl0940/button/calibration_reset_button.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "calibration_reset_button.h" -#include "../bl0940.h" -#include "esphome/core/hal.h" -#include "esphome/core/log.h" -#include "esphome/core/application.h" - -namespace esphome::bl0940 { - -ESPHOME_LOG_TAG(TAG, "bl0940.button.calibration_reset"); - -void CalibrationResetButton::dump_config() { LOG_BUTTON("", "Calibration Reset Button", this); } - -void CalibrationResetButton::press_action() { - ESP_LOGI(TAG, "Resetting calibration defaults..."); - this->parent_->reset_calibration(); -} - -} // namespace esphome::bl0940 diff --git a/esphome/components/bl0940/button/calibration_reset_button.h b/esphome/components/bl0940/button/calibration_reset_button.h deleted file mode 100644 index f5a4f50886e..00000000000 --- a/esphome/components/bl0940/button/calibration_reset_button.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/components/button/button.h" - -namespace esphome::bl0940 { - -class BL0940; // Forward declaration of BL0940 class - -class CalibrationResetButton final : public button::Button, public Component, public Parented { - public: - void dump_config() override; - - void press_action() override; -}; - -} // namespace esphome::bl0940 diff --git a/esphome/components/haier_tundra/button/__init__.py b/esphome/components/haier_tundra/button/__init__.py index 2e919b80099..358e88b2e1b 100644 --- a/esphome/components/haier_tundra/button/__init__.py +++ b/esphome/components/haier_tundra/button/__init__.py @@ -1,18 +1,12 @@ -import esphome.codegen as cg from esphome.components import button import esphome.config_validation as cv from esphome.const import CONF_LIGHT from esphome.types import ConfigType -from ..climate import HaierTundra, haier_tundra_ns +from ..climate import HaierTundra CODEOWNERS = ["@fauxpark"] -HaierTundraButton = haier_tundra_ns.class_( - "HaierTundraButton", button.Button, cg.Component -) -ButtonKind = haier_tundra_ns.enum("ButtonKind", is_class=True) - ICON_LED_OUTLINE = "mdi:led-outline" ICON_SPRAY_BOTTLE = "mdi:spray-bottle" @@ -23,21 +17,16 @@ CONFIG_SCHEMA = cv.Schema( { cv.Required(CONF_HAIER_TUNDRA_ID): cv.use_id(HaierTundra), cv.Optional(CONF_LIGHT): button.button_schema( - HaierTundraButton, icon=ICON_LED_OUTLINE + button.ApplyButton, icon=ICON_LED_OUTLINE ), cv.Optional(CONF_SELF_CLEAN): button.button_schema( - HaierTundraButton, icon=ICON_SPRAY_BOTTLE + button.ApplyButton, icon=ICON_SPRAY_BOTTLE ), } ) async def to_code(config: ConfigType) -> None: - hub = await cg.get_variable(config[CONF_HAIER_TUNDRA_ID]) - for key, kind in ( - (CONF_LIGHT, ButtonKind.BUTTON_KIND_LIGHT), - (CONF_SELF_CLEAN, ButtonKind.BUTTON_KIND_SELF_CLEAN), - ): - if (conf := config.get(key)) is not None: - var = await button.new_button(conf, hub, kind) - await cg.register_component(var, conf) + buttons = button.sub_buttons(config, parent=config[CONF_HAIER_TUNDRA_ID]) + await buttons(CONF_LIGHT, None, apply="toggle_light()") + await buttons(CONF_SELF_CLEAN, None, apply="start_self_clean()") diff --git a/esphome/components/haier_tundra/button/haier_tundra_button.cpp b/esphome/components/haier_tundra/button/haier_tundra_button.cpp deleted file mode 100644 index 39246530bc3..00000000000 --- a/esphome/components/haier_tundra/button/haier_tundra_button.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "haier_tundra_button.h" -#include "esphome/core/log.h" - -namespace esphome::haier_tundra { - -ESPHOME_LOG_TAG(TAG, "haier_tundra.button"); - -void HaierTundraButton::dump_config() { LOG_BUTTON("", "Haier Tundra Button", this); } - -void HaierTundraButton::press_action() { - if (this->kind_ == ButtonKind::BUTTON_KIND_LIGHT) { - this->parent_->toggle_light(); - } else { - this->parent_->start_self_clean(); - } -} - -} // namespace esphome::haier_tundra diff --git a/esphome/components/haier_tundra/button/haier_tundra_button.h b/esphome/components/haier_tundra/button/haier_tundra_button.h deleted file mode 100644 index c4888a4616a..00000000000 --- a/esphome/components/haier_tundra/button/haier_tundra_button.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/components/button/button.h" -#include "esphome/components/haier_tundra/haier_tundra.h" - -namespace esphome::haier_tundra { - -enum class ButtonKind : uint8_t { - BUTTON_KIND_LIGHT, - BUTTON_KIND_SELF_CLEAN, -}; - -class HaierTundraButton final : public button::Button, public Component, public Parented { - public: - HaierTundraButton(HaierTundra *parent, ButtonKind kind) : Parented(parent), kind_(kind) {} - void dump_config() override; - - protected: - void press_action() override; - - ButtonKind kind_; -}; - -} // namespace esphome::haier_tundra diff --git a/esphome/components/micronova/button/__init__.py b/esphome/components/micronova/button/__init__.py index 68b5b9aca69..e203ef37aa9 100644 --- a/esphome/components/micronova/button/__init__.py +++ b/esphome/components/micronova/button/__init__.py @@ -1,4 +1,3 @@ -import esphome.codegen as cg from esphome.components import button import esphome.config_validation as cv from esphome.types import ConfigType @@ -9,12 +8,9 @@ from .. import ( CONF_MICRONOVA_ID, MICRONOVA_ADDRESS_SCHEMA, MicroNova, - micronova_ns, register_micronova_writer, ) -MicroNovaButton = micronova_ns.class_("MicroNovaButton", button.Button, cg.Component) - CONF_CUSTOM_BUTTON = "custom_button" CONF_MEMORY_DATA = "memory_data" @@ -22,7 +18,7 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_MICRONOVA_ID): cv.use_id(MicroNova), cv.Optional(CONF_CUSTOM_BUTTON): button.button_schema( - MicroNovaButton, + button.ApplyButton, ) .extend( MICRONOVA_ADDRESS_SCHEMA( @@ -35,11 +31,13 @@ CONFIG_SCHEMA = cv.Schema( async def to_code(config: ConfigType) -> None: - mv = await cg.get_variable(config[CONF_MICRONOVA_ID]) - - if custom_button_config := config.get(CONF_CUSTOM_BUTTON): + if conf := config.get(CONF_CUSTOM_BUTTON): register_micronova_writer() - bt = await button.new_button(custom_button_config, mv) - cg.add(bt.set_memory_location(custom_button_config[CONF_MEMORY_LOCATION])) - cg.add(bt.set_memory_address(custom_button_config[CONF_MEMORY_ADDRESS])) - cg.add(bt.set_memory_data(custom_button_config[CONF_MEMORY_DATA])) + location = conf[CONF_MEMORY_LOCATION] + address = conf[CONF_MEMORY_ADDRESS] + data = conf[CONF_MEMORY_DATA] + await button.sub_buttons(config, parent=config[CONF_MICRONOVA_ID]).create( + conf, + None, + apply=f"queue_write_command(0x{location:02X}, 0x{address:02X}, 0x{data:02X})", + ) diff --git a/esphome/components/micronova/button/micronova_button.cpp b/esphome/components/micronova/button/micronova_button.cpp deleted file mode 100644 index 159dee2d20e..00000000000 --- a/esphome/components/micronova/button/micronova_button.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "micronova_button.h" - -namespace esphome::micronova { - -ESPHOME_LOG_TAG(TAG, "micronova.button"); - -void MicroNovaButton::dump_config() { - LOG_BUTTON("", "Micronova button", this); - this->dump_base_config(); -} - -void MicroNovaButton::press_action() { - this->micronova_->queue_write_command(this->memory_location_, this->memory_address_, this->memory_data_); -} - -} // namespace esphome::micronova diff --git a/esphome/components/micronova/button/micronova_button.h b/esphome/components/micronova/button/micronova_button.h deleted file mode 100644 index 9f8f66ee02d..00000000000 --- a/esphome/components/micronova/button/micronova_button.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include "esphome/components/micronova/micronova.h" -#include "esphome/core/component.h" -#include "esphome/components/button/button.h" - -namespace esphome::micronova { - -class MicroNovaButton final : public Component, public button::Button, public MicroNovaBaseListener { - public: - MicroNovaButton(MicroNova *m) : MicroNovaBaseListener(m) {} - void dump_config() override; - - void set_memory_data(uint8_t f) { this->memory_data_ = f; } - uint8_t get_memory_data() { return this->memory_data_; } - - protected: - void press_action() override; - - uint8_t memory_data_ = 0; -}; - -} // namespace esphome::micronova diff --git a/esphome/components/tm1638/switch/__init__.py b/esphome/components/tm1638/switch/__init__.py index eee55dc118e..3b0fc05cb9c 100644 --- a/esphome/components/tm1638/switch/__init__.py +++ b/esphome/components/tm1638/switch/__init__.py @@ -1,28 +1,19 @@ -import esphome.codegen as cg from esphome.components import switch import esphome.config_validation as cv from esphome.const import CONF_LED from esphome.types import ConfigType -from ..display import CONF_TM1638_ID, TM1638Component, tm1638_ns +from ..display import CONF_TM1638_ID, TM1638Component -TM1638SwitchLed = tm1638_ns.class_("TM1638SwitchLed", switch.Switch, cg.Component) - - -CONFIG_SCHEMA = ( - switch.switch_schema(TM1638SwitchLed) - .extend( - { - cv.GenerateID(CONF_TM1638_ID): cv.use_id(TM1638Component), - cv.Required(CONF_LED): cv.int_range(min=0, max=7), - } - ) - .extend(cv.COMPONENT_SCHEMA) +CONFIG_SCHEMA = switch.switch_schema(switch.ApplySwitch).extend( + { + cv.GenerateID(CONF_TM1638_ID): cv.use_id(TM1638Component), + cv.Required(CONF_LED): cv.int_range(min=0, max=7), + } ) async def to_code(config: ConfigType) -> None: - var = await switch.new_switch(config) - await cg.register_component(var, config) - cg.add(var.set_lednum(config[CONF_LED])) - await cg.add_variable(var.set_tm1638, config[CONF_TM1638_ID]) + await switch.sub_switches(config, parent=config[CONF_TM1638_ID]).create( + config, None, apply=f"set_led({config[CONF_LED]}, state)", publish="after" + ) diff --git a/esphome/components/tm1638/switch/tm1638_switch_led.cpp b/esphome/components/tm1638/switch/tm1638_switch_led.cpp deleted file mode 100644 index f5573a35921..00000000000 --- a/esphome/components/tm1638/switch/tm1638_switch_led.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "tm1638_switch_led.h" -#include "esphome/core/log.h" - -namespace esphome::tm1638 { - -ESPHOME_LOG_TAG(TAG, "tm1638.led"); - -void TM1638SwitchLed::write_state(bool state) { - tm1638_->set_led(led_, state); - publish_state(state); -} - -void TM1638SwitchLed::dump_config() { - LOG_SWITCH("", "TM1638 LED", this); - ESP_LOGCONFIG(TAG, " LED: %d", led_); -} - -} // namespace esphome::tm1638 diff --git a/esphome/components/tm1638/switch/tm1638_switch_led.h b/esphome/components/tm1638/switch/tm1638_switch_led.h deleted file mode 100644 index 8df4678d62d..00000000000 --- a/esphome/components/tm1638/switch/tm1638_switch_led.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include "esphome/core/component.h" -#include "esphome/components/switch/switch.h" -#include "../tm1638.h" - -namespace esphome::tm1638 { - -class TM1638SwitchLed final : public switch_::Switch, public Component { - public: - void dump_config() override; - - void set_tm1638(TM1638Component *tm1638) { tm1638_ = tm1638; } - void set_lednum(int led) { led_ = led; } - - protected: - void write_state(bool state) override; - TM1638Component *tm1638_; - int led_; -}; -} // namespace esphome::tm1638