From d65620c4d1d4a66d1dc1ecdaf4de041dbb0f6558 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 16 Oct 2026 13:46:07 -1000 Subject: [PATCH] [ld2410] Use Apply numbers --- esphome/components/ld2410/ld2410.cpp | 11 +++- esphome/components/ld2410/ld2410.h | 2 +- esphome/components/ld2410/number/__init__.py | 60 +++++++++++-------- .../ld2410/number/gate_threshold_number.cpp | 12 ---- .../ld2410/number/gate_threshold_number.h | 17 ------ .../ld2410/number/light_threshold_number.cpp | 10 ---- .../ld2410/number/light_threshold_number.h | 16 ----- .../number/max_distance_timeout_number.cpp | 10 ---- .../number/max_distance_timeout_number.h | 16 ----- 9 files changed, 46 insertions(+), 108 deletions(-) delete mode 100644 esphome/components/ld2410/number/gate_threshold_number.cpp delete mode 100644 esphome/components/ld2410/number/gate_threshold_number.h delete mode 100644 esphome/components/ld2410/number/light_threshold_number.cpp delete mode 100644 esphome/components/ld2410/number/light_threshold_number.h delete mode 100644 esphome/components/ld2410/number/max_distance_timeout_number.cpp delete mode 100644 esphome/components/ld2410/number/max_distance_timeout_number.h diff --git a/esphome/components/ld2410/ld2410.cpp b/esphome/components/ld2410/ld2410.cpp index 22bd20d1e5a..0213bf8ea56 100644 --- a/esphome/components/ld2410/ld2410.cpp +++ b/esphome/components/ld2410/ld2410.cpp @@ -727,7 +727,16 @@ void LD2410Component::set_max_distances_timeout() { this->set_config_mode_(false); } -void LD2410Component::set_gate_threshold(uint8_t gate) { +void LD2410Component::set_gate_threshold(number::Number *n) { + // Found by pointer: up to 18 compares per write beat storing the gate in each number + uint8_t gate = 0; + while (gate < TOTAL_GATES && this->gate_move_threshold_numbers_[gate] != n && + this->gate_still_threshold_numbers_[gate] != n) + gate++; + if (gate == TOTAL_GATES) { + ESP_LOGW(TAG, "Threshold number is not a known gate"); + return; + } number::Number *motionsens = this->gate_move_threshold_numbers_[gate]; number::Number *stillsens = this->gate_still_threshold_numbers_[gate]; diff --git a/esphome/components/ld2410/ld2410.h b/esphome/components/ld2410/ld2410.h index 6c7601df3d2..bd1da42c293 100644 --- a/esphome/components/ld2410/ld2410.h +++ b/esphome/components/ld2410/ld2410.h @@ -87,7 +87,7 @@ class LD2410Component final : public Component, public uart::UARTDevice { void set_gate_still_threshold_number(uint8_t gate, number::Number *n); void set_gate_move_threshold_number(uint8_t gate, number::Number *n); void set_max_distances_timeout(); - void set_gate_threshold(uint8_t gate); + void set_gate_threshold(number::Number *n); #endif #ifdef USE_SENSOR void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s); diff --git a/esphome/components/ld2410/number/__init__.py b/esphome/components/ld2410/number/__init__.py index b1891a5935a..4c32fcdc548 100644 --- a/esphome/components/ld2410/number/__init__.py +++ b/esphome/components/ld2410/number/__init__.py @@ -1,3 +1,5 @@ +from functools import partial + import esphome.codegen as cg from esphome.components import number import esphome.config_validation as cv @@ -18,11 +20,7 @@ from esphome.const import ( ) from esphome.types import ConfigType -from .. import CONF_LD2410_ID, LD2410Component, ld2410_ns - -GateThresholdNumber = ld2410_ns.class_("GateThresholdNumber", number.Number) -LightThresholdNumber = ld2410_ns.class_("LightThresholdNumber", number.Number) -MaxDistanceTimeoutNumber = ld2410_ns.class_("MaxDistanceTimeoutNumber", number.Number) +from .. import CONF_LD2410_ID, LD2410Component CONF_MAX_MOVE_DISTANCE_GATE = "max_move_distance_gate" CONF_MAX_STILL_DISTANCE_GATE = "max_still_distance_gate" @@ -35,25 +33,25 @@ CONFIG_SCHEMA = cv.Schema( cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase), cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component), cv.Inclusive(CONF_TIMEOUT, TIMEOUT_GROUP): number.number_schema( - MaxDistanceTimeoutNumber, + number.ApplyNumber, unit_of_measurement=UNIT_SECOND, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_TIMELAPSE, ), cv.Inclusive(CONF_MAX_MOVE_DISTANCE_GATE, TIMEOUT_GROUP): number.number_schema( - MaxDistanceTimeoutNumber, + number.ApplyNumber, device_class=DEVICE_CLASS_DISTANCE, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_MOTION_SENSOR, ), cv.Inclusive(CONF_MAX_STILL_DISTANCE_GATE, TIMEOUT_GROUP): number.number_schema( - MaxDistanceTimeoutNumber, + number.ApplyNumber, device_class=DEVICE_CLASS_DISTANCE, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_MOTION_SENSOR, ), cv.Optional(CONF_LIGHT_THRESHOLD): number.number_schema( - LightThresholdNumber, + number.ApplyNumber, device_class=DEVICE_CLASS_ILLUMINANCE, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_LIGHTBULB, @@ -66,14 +64,14 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( cv.Optional(f"g{x}"): cv.Schema( { cv.Required(CONF_MOVE_THRESHOLD): number.number_schema( - GateThresholdNumber, + number.ApplyNumber, device_class=DEVICE_CLASS_SIGNAL_STRENGTH, unit_of_measurement=UNIT_PERCENT, entity_category=ENTITY_CATEGORY_CONFIG, icon=ICON_MOTION_SENSOR, ), cv.Required(CONF_STILL_THRESHOLD): number.number_schema( - GateThresholdNumber, + number.ApplyNumber, device_class=DEVICE_CLASS_SIGNAL_STRENGTH, unit_of_measurement=UNIT_PERCENT, entity_category=ENTITY_CATEGORY_CONFIG, @@ -89,8 +87,14 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend( async def to_code(config: ConfigType) -> None: hub = await cg.get_variable(config[CONF_LD2410_ID]) numbers = number.sub_numbers(config, parent=hub) + timeouts = {"apply": "set_max_distances_timeout()", "publish": "before"} await numbers( - CONF_TIMEOUT, hub.set_timeout_number, min_value=0, max_value=65535, step=1 + CONF_TIMEOUT, + hub.set_timeout_number, + min_value=0, + max_value=65535, + step=1, + **timeouts, ) await numbers( CONF_MAX_MOVE_DISTANCE_GATE, @@ -98,6 +102,7 @@ async def to_code(config: ConfigType) -> None: min_value=2, max_value=8, step=1, + **timeouts, ) await numbers( CONF_MAX_STILL_DISTANCE_GATE, @@ -105,6 +110,7 @@ async def to_code(config: ConfigType) -> None: min_value=2, max_value=8, step=1, + **timeouts, ) await numbers( CONF_LIGHT_THRESHOLD, @@ -112,21 +118,25 @@ async def to_code(config: ConfigType) -> None: min_value=0, max_value=255, step=1, + apply="set_light_out_control()", + publish="before", ) + threshold = { + "min_value": 0, + "max_value": 100, + "step": 1, + "apply": "set_gate_threshold(entity)", + "publish": "before", + } for x in range(9): if gate_conf := config.get(f"g{x}"): - move_config = gate_conf[CONF_MOVE_THRESHOLD] - n = cg.new_Pvariable(move_config[CONF_ID], x) - await number.register_number( - n, move_config, min_value=0, max_value=100, step=1 + await numbers.create( + gate_conf[CONF_MOVE_THRESHOLD], + partial(hub.set_gate_move_threshold_number, x), + **threshold, ) - await cg.register_parented(n, hub) - cg.add(hub.set_gate_move_threshold_number(x, n)) - - still_config = gate_conf[CONF_STILL_THRESHOLD] - n = cg.new_Pvariable(still_config[CONF_ID], x) - await number.register_number( - n, still_config, min_value=0, max_value=100, step=1 + await numbers.create( + gate_conf[CONF_STILL_THRESHOLD], + partial(hub.set_gate_still_threshold_number, x), + **threshold, ) - await cg.register_parented(n, hub) - cg.add(hub.set_gate_still_threshold_number(x, n)) diff --git a/esphome/components/ld2410/number/gate_threshold_number.cpp b/esphome/components/ld2410/number/gate_threshold_number.cpp deleted file mode 100644 index 65e864a4d7d..00000000000 --- a/esphome/components/ld2410/number/gate_threshold_number.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "gate_threshold_number.h" - -namespace esphome::ld2410 { - -GateThresholdNumber::GateThresholdNumber(uint8_t gate) : gate_(gate) {} - -void GateThresholdNumber::control(float value) { - this->publish_state(value); - this->parent_->set_gate_threshold(this->gate_); -} - -} // namespace esphome::ld2410 diff --git a/esphome/components/ld2410/number/gate_threshold_number.h b/esphome/components/ld2410/number/gate_threshold_number.h deleted file mode 100644 index 68359a10d4f..00000000000 --- a/esphome/components/ld2410/number/gate_threshold_number.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include "esphome/components/number/number.h" -#include "../ld2410.h" - -namespace esphome::ld2410 { - -class GateThresholdNumber final : public number::Number, public Parented { - public: - GateThresholdNumber(uint8_t gate); - - protected: - uint8_t gate_; - void control(float value) override; -}; - -} // namespace esphome::ld2410 diff --git a/esphome/components/ld2410/number/light_threshold_number.cpp b/esphome/components/ld2410/number/light_threshold_number.cpp deleted file mode 100644 index 55f69ff1fc9..00000000000 --- a/esphome/components/ld2410/number/light_threshold_number.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "light_threshold_number.h" - -namespace esphome::ld2410 { - -void LightThresholdNumber::control(float value) { - this->publish_state(value); - this->parent_->set_light_out_control(); -} - -} // namespace esphome::ld2410 diff --git a/esphome/components/ld2410/number/light_threshold_number.h b/esphome/components/ld2410/number/light_threshold_number.h deleted file mode 100644 index 6e1a5ca4a4b..00000000000 --- a/esphome/components/ld2410/number/light_threshold_number.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "esphome/components/number/number.h" -#include "../ld2410.h" - -namespace esphome::ld2410 { - -class LightThresholdNumber final : public number::Number, public Parented { - public: - LightThresholdNumber() = default; - - protected: - void control(float value) override; -}; - -} // namespace esphome::ld2410 diff --git a/esphome/components/ld2410/number/max_distance_timeout_number.cpp b/esphome/components/ld2410/number/max_distance_timeout_number.cpp deleted file mode 100644 index b8667f5dbc1..00000000000 --- a/esphome/components/ld2410/number/max_distance_timeout_number.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "max_distance_timeout_number.h" - -namespace esphome::ld2410 { - -void MaxDistanceTimeoutNumber::control(float value) { - this->publish_state(value); - this->parent_->set_max_distances_timeout(); -} - -} // namespace esphome::ld2410 diff --git a/esphome/components/ld2410/number/max_distance_timeout_number.h b/esphome/components/ld2410/number/max_distance_timeout_number.h deleted file mode 100644 index 29b19c2022b..00000000000 --- a/esphome/components/ld2410/number/max_distance_timeout_number.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "esphome/components/number/number.h" -#include "../ld2410.h" - -namespace esphome::ld2410 { - -class MaxDistanceTimeoutNumber final : public number::Number, public Parented { - public: - MaxDistanceTimeoutNumber() = default; - - protected: - void control(float value) override; -}; - -} // namespace esphome::ld2410