[ld2450] Use integer dedup for direction text sensor updates

Replace per-frame string comparison with Deduplicator<uint8_t> 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.
This commit is contained in:
J. Nick Koston
2026-02-28 13:11:18 -10:00
parent 757e8d90e6
commit 128259870e
2 changed files with 7 additions and 5 deletions
+5 -4
View File
@@ -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
+2 -1
View File
@@ -194,7 +194,8 @@ class LD2450Component : public Component, public uart::UARTDevice {
std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_moving_target_count_sensors_{};
#endif
#ifdef USE_TEXT_SENSOR
std::array<text_sensor::TextSensor *, 3> direction_text_sensors_{};
std::array<text_sensor::TextSensor *, MAX_TARGETS> direction_text_sensors_{};
std::array<Deduplicator<uint8_t>, MAX_TARGETS> direction_dedup_{};
#endif
LazyCallbackManager<void()> data_callback_;