From dd59a827ff439bf7ac7a556e4981b4ef6e3062cf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 16 Oct 2026 10:41:21 -1000 Subject: [PATCH] [ld2450] Use Apply entities for the forwarding buttons, switches, numbers and selects --- esphome/components/ld2450/button/__init__.py | 17 +++++++------- .../ld2450/button/factory_reset_button.cpp | 7 ------ .../ld2450/button/factory_reset_button.h | 17 -------------- .../ld2450/button/restart_button.cpp | 7 ------ .../components/ld2450/button/restart_button.h | 17 -------------- esphome/components/ld2450/number/__init__.py | 5 ++-- .../ld2450/number/presence_timeout_number.cpp | 10 -------- .../ld2450/number/presence_timeout_number.h | 17 -------------- esphome/components/ld2450/select/__init__.py | 12 ++++++---- .../ld2450/select/baud_rate_select.cpp | 10 -------- .../ld2450/select/baud_rate_select.h | 17 -------------- .../ld2450/select/zone_type_select.cpp | 10 -------- .../ld2450/select/zone_type_select.h | 17 -------------- esphome/components/ld2450/switch/__init__.py | 23 ++++++++++++------- .../ld2450/switch/bluetooth_switch.cpp | 10 -------- .../ld2450/switch/bluetooth_switch.h | 17 -------------- .../ld2450/switch/multi_target_switch.cpp | 10 -------- .../ld2450/switch/multi_target_switch.h | 17 -------------- 18 files changed, 34 insertions(+), 206 deletions(-) delete mode 100644 esphome/components/ld2450/button/factory_reset_button.cpp delete mode 100644 esphome/components/ld2450/button/factory_reset_button.h delete mode 100644 esphome/components/ld2450/button/restart_button.cpp delete mode 100644 esphome/components/ld2450/button/restart_button.h delete mode 100644 esphome/components/ld2450/number/presence_timeout_number.cpp delete mode 100644 esphome/components/ld2450/number/presence_timeout_number.h delete mode 100644 esphome/components/ld2450/select/baud_rate_select.cpp delete mode 100644 esphome/components/ld2450/select/baud_rate_select.h delete mode 100644 esphome/components/ld2450/select/zone_type_select.cpp delete mode 100644 esphome/components/ld2450/select/zone_type_select.h delete mode 100644 esphome/components/ld2450/switch/bluetooth_switch.cpp delete mode 100644 esphome/components/ld2450/switch/bluetooth_switch.h delete mode 100644 esphome/components/ld2450/switch/multi_target_switch.cpp delete mode 100644 esphome/components/ld2450/switch/multi_target_switch.h diff --git a/esphome/components/ld2450/button/__init__.py b/esphome/components/ld2450/button/__init__.py index d23f1bebf8f..5166073a620 100644 --- a/esphome/components/ld2450/button/__init__.py +++ b/esphome/components/ld2450/button/__init__.py @@ -13,22 +13,19 @@ from esphome.const import ( ) from esphome.types import ConfigType -from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns - -FactoryResetButton = ld2450_ns.class_("FactoryResetButton", button.Button) -RestartButton = ld2450_ns.class_("RestartButton", button.Button) +from .. import CONF_LD2450_ID, LD2450Component CONFIG_SCHEMA = { cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.Optional(CONF_FACTORY_RESET): button.button_schema( - FactoryResetButton, + button.ApplyButton, device_class=DEVICE_CLASS_RESTART, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_RESTART_ALERT, ), cv.Optional(CONF_RESTART): button.button_schema( - RestartButton, + button.ApplyButton, device_class=DEVICE_CLASS_RESTART, entity_category=ENTITY_CATEGORY_DIAGNOSTIC, icon=ICON_RESTART, @@ -39,5 +36,9 @@ CONFIG_SCHEMA = { async def to_code(config: ConfigType) -> None: hub = await cg.get_variable(config[CONF_LD2450_ID]) buttons = button.sub_buttons(config, parent=hub) - await buttons(CONF_FACTORY_RESET, hub.set_factory_reset_button) - await buttons(CONF_RESTART, hub.set_restart_button) + await buttons( + CONF_FACTORY_RESET, hub.set_factory_reset_button, apply="factory_reset()" + ) + await buttons( + CONF_RESTART, hub.set_restart_button, apply="restart_and_read_all_info()" + ) diff --git a/esphome/components/ld2450/button/factory_reset_button.cpp b/esphome/components/ld2450/button/factory_reset_button.cpp deleted file mode 100644 index 7a8eb5b0dd7..00000000000 --- a/esphome/components/ld2450/button/factory_reset_button.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "factory_reset_button.h" - -namespace esphome::ld2450 { - -void FactoryResetButton::press_action() { this->parent_->factory_reset(); } - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/button/factory_reset_button.h b/esphome/components/ld2450/button/factory_reset_button.h deleted file mode 100644 index 71dc19a6cd3..00000000000 --- a/esphome/components/ld2450/button/factory_reset_button.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/button/button.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class FactoryResetButton : public button::Button, public Parented { - public: - // User provided, not "= default": `new(p) FactoryResetButton()` would zero-fill .bss that is already zero. - FactoryResetButton() {} - - protected: - void press_action() override; -}; - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/button/restart_button.cpp b/esphome/components/ld2450/button/restart_button.cpp deleted file mode 100644 index 572df80325f..00000000000 --- a/esphome/components/ld2450/button/restart_button.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "restart_button.h" - -namespace esphome::ld2450 { - -void RestartButton::press_action() { this->parent_->restart_and_read_all_info(); } - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/button/restart_button.h b/esphome/components/ld2450/button/restart_button.h deleted file mode 100644 index 87b1a2bbd1e..00000000000 --- a/esphome/components/ld2450/button/restart_button.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/button/button.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class RestartButton : public button::Button, public Parented { - public: - // User provided, not "= default": `new(p) RestartButton()` would zero-fill .bss that is already zero. - RestartButton() {} - - protected: - void press_action() override; -}; - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/number/__init__.py b/esphome/components/ld2450/number/__init__.py index 237c7c4c231..23aa1a33251 100644 --- a/esphome/components/ld2450/number/__init__.py +++ b/esphome/components/ld2450/number/__init__.py @@ -24,7 +24,6 @@ ICON_ARROW_TOP_LEFT = "mdi:arrow-top-left" ICON_ARROW_TOP_LEFT_BOLD_BOX_OUTLINE = "mdi:arrow-top-left-bold-box-outline" MAX_ZONES = 3 -PresenceTimeoutNumber = ld2450_ns.class_("PresenceTimeoutNumber", number.Number) ZoneCoordinateNumber = ld2450_ns.class_("ZoneCoordinateNumber", number.Number) CONFIG_SCHEMA = cv.Schema( @@ -32,7 +31,7 @@ CONFIG_SCHEMA = cv.Schema( cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.Required(CONF_PRESENCE_TIMEOUT): number.number_schema( - PresenceTimeoutNumber, + number.ApplyNumber, unit_of_measurement=UNIT_SECOND, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_TIMELAPSE, @@ -88,6 +87,8 @@ async def to_code(config: ConfigType) -> None: min_value=0, max_value=3600, step=1, + apply="set_presence_timeout()", + publish="before", ) for zone_num in range(MAX_ZONES): if zone_conf := config.get(f"zone_{zone_num + 1}"): diff --git a/esphome/components/ld2450/number/presence_timeout_number.cpp b/esphome/components/ld2450/number/presence_timeout_number.cpp deleted file mode 100644 index 19a1ada0d7b..00000000000 --- a/esphome/components/ld2450/number/presence_timeout_number.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "presence_timeout_number.h" - -namespace esphome::ld2450 { - -void PresenceTimeoutNumber::control(float value) { - this->publish_state(value); - this->parent_->set_presence_timeout(); -} - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/number/presence_timeout_number.h b/esphome/components/ld2450/number/presence_timeout_number.h deleted file mode 100644 index 8c44fa39dc0..00000000000 --- a/esphome/components/ld2450/number/presence_timeout_number.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/number/number.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class PresenceTimeoutNumber : public number::Number, public Parented { - public: - // User provided, not "= default": `new(p) PresenceTimeoutNumber()` would zero-fill .bss that is already zero. - PresenceTimeoutNumber() {} - - protected: - void control(float value) override; -}; - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/select/__init__.py b/esphome/components/ld2450/select/__init__.py index 779dfc2a35c..f62bf5d4597 100644 --- a/esphome/components/ld2450/select/__init__.py +++ b/esphome/components/ld2450/select/__init__.py @@ -9,23 +9,21 @@ from esphome.const import ( ) from esphome.types import ConfigType -from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns +from .. import CONF_LD2450_ID, LD2450Component CONF_ZONE_TYPE = "zone_type" -BaudRateSelect = ld2450_ns.class_("BaudRateSelect", select.Select) -ZoneTypeSelect = ld2450_ns.class_("ZoneTypeSelect", select.Select) CONFIG_SCHEMA = { cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.Optional(CONF_BAUD_RATE): select.select_schema( - BaudRateSelect, + select.ApplySelect, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_THERMOMETER, ), cv.Optional(CONF_ZONE_TYPE): select.select_schema( - ZoneTypeSelect, + select.ApplySelect, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_THERMOMETER, ), @@ -48,6 +46,8 @@ async def to_code(config: ConfigType) -> None: "256000", "460800", ], + apply="set_baud_rate(entity->option_at(index))", + publish="before", ) await selects( CONF_ZONE_TYPE, @@ -57,4 +57,6 @@ async def to_code(config: ConfigType) -> None: "Detection", "Filter", ], + apply="set_zone_type(entity->option_at(index))", + publish="before", ) diff --git a/esphome/components/ld2450/select/baud_rate_select.cpp b/esphome/components/ld2450/select/baud_rate_select.cpp deleted file mode 100644 index 7b18b280065..00000000000 --- a/esphome/components/ld2450/select/baud_rate_select.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "baud_rate_select.h" - -namespace esphome::ld2450 { - -void BaudRateSelect::control(size_t index) { - this->publish_state(index); - this->parent_->set_baud_rate(this->option_at(index)); -} - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/select/baud_rate_select.h b/esphome/components/ld2450/select/baud_rate_select.h deleted file mode 100644 index af4c477dff2..00000000000 --- a/esphome/components/ld2450/select/baud_rate_select.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/select/select.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class BaudRateSelect : public select::Select, public Parented { - public: - // User provided, not "= default": `new(p) BaudRateSelect()` would zero-fill .bss that is already zero. - BaudRateSelect() {} - - protected: - void control(size_t index) override; -}; - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/select/zone_type_select.cpp b/esphome/components/ld2450/select/zone_type_select.cpp deleted file mode 100644 index 39642b99adc..00000000000 --- a/esphome/components/ld2450/select/zone_type_select.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "zone_type_select.h" - -namespace esphome::ld2450 { - -void ZoneTypeSelect::control(size_t index) { - this->publish_state(index); - this->parent_->set_zone_type(this->option_at(index)); -} - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/select/zone_type_select.h b/esphome/components/ld2450/select/zone_type_select.h deleted file mode 100644 index cf79c2324dc..00000000000 --- a/esphome/components/ld2450/select/zone_type_select.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/select/select.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class ZoneTypeSelect : public select::Select, public Parented { - public: - // User provided, not "= default": `new(p) ZoneTypeSelect()` would zero-fill .bss that is already zero. - ZoneTypeSelect() {} - - protected: - void control(size_t index) override; -}; - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/switch/__init__.py b/esphome/components/ld2450/switch/__init__.py index bb920f6c76e..60c4aae73fe 100644 --- a/esphome/components/ld2450/switch/__init__.py +++ b/esphome/components/ld2450/switch/__init__.py @@ -11,10 +11,7 @@ from esphome.const import ( ) from esphome.types import ConfigType -from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns - -BluetoothSwitch = ld2450_ns.class_("BluetoothSwitch", switch.Switch) -MultiTargetSwitch = ld2450_ns.class_("MultiTargetSwitch", switch.Switch) +from .. import CONF_LD2450_ID, LD2450Component CONF_MULTI_TARGET = "multi_target" @@ -22,13 +19,13 @@ CONFIG_SCHEMA = { cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), cv.Optional(CONF_BLUETOOTH): switch.switch_schema( - BluetoothSwitch, + switch.ApplySwitch, device_class=DEVICE_CLASS_SWITCH, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_BLUETOOTH, ), cv.Optional(CONF_MULTI_TARGET): switch.switch_schema( - MultiTargetSwitch, + switch.ApplySwitch, device_class=DEVICE_CLASS_SWITCH, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_PULSE, @@ -39,5 +36,15 @@ CONFIG_SCHEMA = { async def to_code(config: ConfigType) -> None: hub = await cg.get_variable(config[CONF_LD2450_ID]) switches = switch.sub_switches(config, parent=hub) - await switches(CONF_BLUETOOTH, hub.set_bluetooth_switch) - await switches(CONF_MULTI_TARGET, hub.set_multi_target_switch) + await switches( + CONF_BLUETOOTH, + hub.set_bluetooth_switch, + apply="set_bluetooth(state)", + publish="before", + ) + await switches( + CONF_MULTI_TARGET, + hub.set_multi_target_switch, + apply="set_multi_target(state)", + publish="before", + ) diff --git a/esphome/components/ld2450/switch/bluetooth_switch.cpp b/esphome/components/ld2450/switch/bluetooth_switch.cpp deleted file mode 100644 index 0e19a3e6c66..00000000000 --- a/esphome/components/ld2450/switch/bluetooth_switch.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "bluetooth_switch.h" - -namespace esphome::ld2450 { - -void BluetoothSwitch::write_state(bool state) { - this->publish_state(state); - this->parent_->set_bluetooth(state); -} - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/switch/bluetooth_switch.h b/esphome/components/ld2450/switch/bluetooth_switch.h deleted file mode 100644 index 8b118a7b8c1..00000000000 --- a/esphome/components/ld2450/switch/bluetooth_switch.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/switch/switch.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class BluetoothSwitch : public switch_::Switch, public Parented { - public: - // User provided, not "= default": `new(p) BluetoothSwitch()` would zero-fill .bss that is already zero. - BluetoothSwitch() {} - - protected: - void write_state(bool state) override; -}; - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/switch/multi_target_switch.cpp b/esphome/components/ld2450/switch/multi_target_switch.cpp deleted file mode 100644 index 0b1cb04a685..00000000000 --- a/esphome/components/ld2450/switch/multi_target_switch.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "multi_target_switch.h" - -namespace esphome::ld2450 { - -void MultiTargetSwitch::write_state(bool state) { - this->publish_state(state); - this->parent_->set_multi_target(state); -} - -} // namespace esphome::ld2450 diff --git a/esphome/components/ld2450/switch/multi_target_switch.h b/esphome/components/ld2450/switch/multi_target_switch.h deleted file mode 100644 index d711a2d2d29..00000000000 --- a/esphome/components/ld2450/switch/multi_target_switch.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/switch/switch.h" -#include "../ld2450.h" - -namespace esphome::ld2450 { - -class MultiTargetSwitch : public switch_::Switch, public Parented { - public: - // User provided, not "= default": `new(p) MultiTargetSwitch()` would zero-fill .bss that is already zero. - MultiTargetSwitch() {} - - protected: - void write_state(bool state) override; -}; - -} // namespace esphome::ld2450