mirror of
https://github.com/esphome/esphome.git
synced 2026-09-18 18:48:39 +00:00
[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:
@@ -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
|
||||
|
||||
|
||||
@@ -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_;
|
||||
|
||||
Reference in New Issue
Block a user