From 128259870ee1d033caffaf22613005824135a27b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 28 Feb 2026 13:11:18 -1000 Subject: [PATCH] [ld2450] Use integer dedup for direction text sensor updates Replace per-frame string comparison with Deduplicator on the direction enum value. The previous code called get_state() and compared std::string against const char* on every frame for each target (~75 times/sec). The new approach does a single uint8_t comparison and only looks up the direction string and publishes when the direction actually changes. Also use MAX_TARGETS instead of hardcoded 3 for direction_text_sensors_ array size for consistency with all other sensor arrays. --- esphome/components/ld2450/ld2450.cpp | 9 +++++---- esphome/components/ld2450/ld2450.h | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index d30c1647695..4c1cc12cce5 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -528,10 +528,11 @@ void LD2450Component::handle_periodic_data_() { } else { direction = DIRECTION_STATIONARY; } - text_sensor::TextSensor *tsd = this->direction_text_sensors_[index]; - const auto *dir_str = find_str(ld2450::DIRECTION_BY_UINT, direction); - if (tsd != nullptr && (!tsd->has_state() || tsd->get_state() != dir_str)) { - tsd->publish_state(dir_str); + if (this->direction_dedup_[index].next(direction)) { + text_sensor::TextSensor *tsd = this->direction_text_sensors_[index]; + if (tsd != nullptr) { + tsd->publish_state(find_str(ld2450::DIRECTION_BY_UINT, direction)); + } } #endif diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h index 30f96c0a9c1..518b320b0af 100644 --- a/esphome/components/ld2450/ld2450.h +++ b/esphome/components/ld2450/ld2450.h @@ -194,7 +194,8 @@ class LD2450Component : public Component, public uart::UARTDevice { std::array *, MAX_ZONES> zone_moving_target_count_sensors_{}; #endif #ifdef USE_TEXT_SENSOR - std::array direction_text_sensors_{}; + std::array direction_text_sensors_{}; + std::array, MAX_TARGETS> direction_dedup_{}; #endif LazyCallbackManager data_callback_;