[haier_tundra][tm1638][bl0940][micronova] Use Apply entities for hub forwarding buttons and switches (#21148)

This commit is contained in:
J. Nick Koston
2026-10-20 08:52:26 -04:00
committed by GitHub
parent b18a5b09e7
commit 8b951fa620
13 changed files with 35 additions and 221 deletions
+1
View File
@@ -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();
+9 -18
View File
@@ -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()"
)
@@ -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
@@ -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<BL0940> {
public:
void dump_config() override;
void press_action() override;
};
} // namespace esphome::bl0940
@@ -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()")
@@ -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
@@ -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<HaierTundra> {
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
+10 -12
View File
@@ -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})",
)
@@ -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
@@ -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
+9 -18
View File
@@ -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"
)
@@ -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
@@ -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