diff --git a/esphome/components/dfrobot_sen0609/dfrobot_sen0609.cpp b/esphome/components/dfrobot_sen0609/dfrobot_sen0609.cpp index 5f42d05a827..f5e157ece25 100644 --- a/esphome/components/dfrobot_sen0609/dfrobot_sen0609.cpp +++ b/esphome/components/dfrobot_sen0609/dfrobot_sen0609.cpp @@ -147,9 +147,9 @@ void DFRobotSEN0609Component::dump_config() { LOG_BINARY_SENSOR(" ", "HasTarget", this->has_target_binary_sensor_); #endif #ifdef USE_SENSOR - LOG_SENSOR(" ", "TargetDistance", this->target_distance_sensor_); - LOG_SENSOR(" ", "TargetSpeed", this->target_speed_sensor_); - LOG_SENSOR(" ", "TargetEnergy", this->target_energy_sensor_); + LOG_SENSOR(" ", "TargetDistance", this->target_distance_sensor_.get_sensor()); + LOG_SENSOR(" ", "TargetSpeed", this->target_speed_sensor_.get_sensor()); + LOG_SENSOR(" ", "TargetEnergy", this->target_energy_sensor_.get_sensor()); #endif #ifdef USE_TEXT_SENSOR LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_); @@ -353,17 +353,18 @@ void DFRobotSEN0609Component::log_bad_frame_(const LogString *problem, const cha } #ifdef USE_SENSOR -static void publish_deduplicated(sensor::Sensor *sens, Deduplicator &dedup, float value) { - if (sens == nullptr) - return; - if (std::isnan(value) ? dedup.next_unknown() : dedup.next(value)) - sens->publish_state(value); +static void publish_deduplicated(sensor::SensorWithDedup &sens, float value) { + if (std::isnan(value)) { + sens.publish_state_unknown(); + } else { + sens.publish_state_if_not_dup(value); + } } void DFRobotSEN0609Component::publish_target_(float distance, float speed, float energy) { - publish_deduplicated(this->target_distance_sensor_, this->distance_dedup_, distance); - publish_deduplicated(this->target_speed_sensor_, this->speed_dedup_, speed); - publish_deduplicated(this->target_energy_sensor_, this->energy_dedup_, energy); + publish_deduplicated(this->target_distance_sensor_, distance); + publish_deduplicated(this->target_speed_sensor_, speed); + publish_deduplicated(this->target_energy_sensor_, energy); } #endif diff --git a/esphome/components/dfrobot_sen0609/dfrobot_sen0609.h b/esphome/components/dfrobot_sen0609/dfrobot_sen0609.h index ca70fa14943..4b117ee52f9 100644 --- a/esphome/components/dfrobot_sen0609/dfrobot_sen0609.h +++ b/esphome/components/dfrobot_sen0609/dfrobot_sen0609.h @@ -7,7 +7,7 @@ #include "esphome/components/binary_sensor/binary_sensor.h" #endif #ifdef USE_SENSOR -#include "esphome/components/sensor/sensor.h" +#include "esphome/components/sensor/sensor_with_dedup.h" #endif #ifdef USE_TEXT_SENSOR #include "esphome/components/text_sensor/text_sensor.h" @@ -102,9 +102,9 @@ class DFRobotSEN0609Component final : public Component, public uart::UARTDevice SUB_BINARY_SENSOR(has_target) #endif #ifdef USE_SENSOR - SUB_SENSOR(target_distance) - SUB_SENSOR(target_speed) - SUB_SENSOR(target_energy) + SUB_SENSOR_WITH_DEDUP(target_distance, float) + SUB_SENSOR_WITH_DEDUP(target_speed, float) + SUB_SENSOR_WITH_DEDUP(target_energy, float) #endif #ifdef USE_TEXT_SENSOR SUB_TEXT_SENSOR(version) @@ -174,9 +174,6 @@ class DFRobotSEN0609Component final : public Component, public uart::UARTDevice #endif #ifdef USE_SENSOR void publish_target_(float distance, float speed, float energy); - Deduplicator distance_dedup_; - Deduplicator speed_dedup_; - Deduplicator energy_dedup_; #endif #ifdef USE_NUMBER diff --git a/esphome/components/ld2410/ld2410.h b/esphome/components/ld2410/ld2410.h index 6c7601df3d2..af0db761ee5 100644 --- a/esphome/components/ld2410/ld2410.h +++ b/esphome/components/ld2410/ld2410.h @@ -128,8 +128,8 @@ class LD2410Component final : public Component, public uart::UARTDevice { std::array gate_still_threshold_numbers_{}; #endif #ifdef USE_SENSOR - std::array, TOTAL_GATES> gate_move_sensors_{}; - std::array, TOTAL_GATES> gate_still_sensors_{}; + std::array, TOTAL_GATES> gate_move_sensors_{}; + std::array, TOTAL_GATES> gate_still_sensors_{}; #endif }; diff --git a/esphome/components/ld2412/ld2412.h b/esphome/components/ld2412/ld2412.h index c427d024f39..401ea61e34a 100644 --- a/esphome/components/ld2412/ld2412.h +++ b/esphome/components/ld2412/ld2412.h @@ -148,8 +148,8 @@ class LD2412Component final : public Component, public uart::UARTDevice { GateThresholds gate_still_thresholds_; #endif #ifdef USE_SENSOR - std::array, TOTAL_GATES> gate_move_sensors_{}; - std::array, TOTAL_GATES> gate_still_sensors_{}; + std::array, TOTAL_GATES> gate_move_sensors_{}; + std::array, TOTAL_GATES> gate_still_sensors_{}; #endif }; diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h index f85a26ba675..d620bf971c7 100644 --- a/esphome/components/ld2450/ld2450.h +++ b/esphome/components/ld2450/ld2450.h @@ -193,15 +193,15 @@ class LD2450Component : public Component, public uart::UARTDevice { ZoneOfNumbers zone_numbers_[MAX_ZONES]; #endif #ifdef USE_SENSOR - std::array, MAX_TARGETS> move_x_sensors_{}; - std::array, MAX_TARGETS> move_y_sensors_{}; - std::array, MAX_TARGETS> move_speed_sensors_{}; - std::array, MAX_TARGETS> move_angle_sensors_{}; - std::array, MAX_TARGETS> move_distance_sensors_{}; - std::array, MAX_TARGETS> move_resolution_sensors_{}; - std::array, MAX_ZONES> zone_target_count_sensors_{}; - std::array, MAX_ZONES> zone_still_target_count_sensors_{}; - std::array, MAX_ZONES> zone_moving_target_count_sensors_{}; + std::array, MAX_TARGETS> move_x_sensors_{}; + std::array, MAX_TARGETS> move_y_sensors_{}; + std::array, MAX_TARGETS> move_speed_sensors_{}; + std::array, MAX_TARGETS> move_angle_sensors_{}; + std::array, MAX_TARGETS> move_distance_sensors_{}; + std::array, MAX_TARGETS> move_resolution_sensors_{}; + std::array, MAX_ZONES> zone_target_count_sensors_{}; + std::array, MAX_ZONES> zone_still_target_count_sensors_{}; + std::array, MAX_ZONES> zone_moving_target_count_sensors_{}; #endif #ifdef USE_TEXT_SENSOR std::array direction_text_sensors_{}; diff --git a/esphome/components/ld2460/ld2460.h b/esphome/components/ld2460/ld2460.h index aa7679f39a1..4ac3298b4f5 100644 --- a/esphome/components/ld2460/ld2460.h +++ b/esphome/components/ld2460/ld2460.h @@ -48,7 +48,7 @@ class LD2460Component : public Component, public uart::UARTDevice { #ifdef USE_SENSOR // INT16_MIN marks a slot with no published coordinates, so the next report always publishes struct TargetSensors { - ld24xx::SensorWithDedup x, y, distance, angle; + sensor::SensorWithDedup x, y, distance, angle; int16_t last_x{INT16_MIN}; int16_t last_y{INT16_MIN}; }; diff --git a/esphome/components/ld24xx/ld24xx.h b/esphome/components/ld24xx/ld24xx.h index deac04e86f9..6ea9533c0a3 100644 --- a/esphome/components/ld24xx/ld24xx.h +++ b/esphome/components/ld24xx/ld24xx.h @@ -7,21 +7,9 @@ #include #ifdef USE_SENSOR -#include "esphome/components/sensor/sensor.h" - -#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \ - protected: \ - ld24xx::SensorWithDedup name##_sensor_{}; \ -\ - public: \ - void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_.set_sensor(sensor); } +#include "esphome/components/sensor/sensor_with_dedup.h" #endif -#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \ - if ((sensor).has_sensor()) { \ - LOG_SENSOR(tag, name, (sensor).get_sensor()); \ - } - #define SAFE_PUBLISH_SENSOR(sensor, value) (sensor).publish_state_if_not_dup(value) #define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) (sensor).publish_state_unknown() @@ -60,34 +48,4 @@ inline void format_version_str(const uint8_t *version, std::span buffe version[2]); } -#ifdef USE_SENSOR -/// Sensor with deduplication — sensor may be null, null check is internal. -/// Stored inline, no heap allocation. Does nothing when no sensor is set. -template class SensorWithDedup { - public: - void set_sensor(sensor::Sensor *sens) { - this->sens_ = sens; - this->dedup_ = {}; - } - - void publish_state_if_not_dup(T state) { - if (this->sens_ != nullptr && this->dedup_.next(state)) { - this->sens_->publish_state(static_cast(state)); - } - } - - void publish_state_unknown() { - if (this->sens_ != nullptr && this->dedup_.next_unknown()) { - this->sens_->publish_state(NAN); - } - } - - bool has_sensor() const { return this->sens_ != nullptr; } - sensor::Sensor *get_sensor() const { return this->sens_; } - - protected: - sensor::Sensor *sens_{nullptr}; - Deduplicator dedup_; -}; -#endif } // namespace esphome::ld24xx