From b036891464e3f3c3de3dd4888ff9e42eee975910 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 16 Oct 2026 13:12:40 -1000 Subject: [PATCH] Make ld24xx use the shared SensorWithDedup so there is one definition --- esphome/components/ld24xx/ld24xx.h | 40 +------------------ esphome/components/sensor/sensor_with_dedup.h | 2 - 2 files changed, 2 insertions(+), 40 deletions(-) diff --git a/esphome/components/ld24xx/ld24xx.h b/esphome/components/ld24xx/ld24xx.h index 20fbf52c843..b7dddb92fad 100644 --- a/esphome/components/ld24xx/ld24xx.h +++ b/esphome/components/ld24xx/ld24xx.h @@ -7,16 +7,7 @@ #include #ifdef USE_SENSOR -#include "esphome/components/sensor/sensor.h" - -#ifndef SUB_SENSOR_WITH_DEDUP -#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); } -#endif +#include "esphome/components/sensor/sensor_with_dedup.h" #endif #define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \ @@ -63,33 +54,6 @@ inline void format_version_str(const uint8_t *version, std::span buffe } #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_; -}; +template using SensorWithDedup = sensor::SensorWithDedup; #endif } // namespace esphome::ld24xx diff --git a/esphome/components/sensor/sensor_with_dedup.h b/esphome/components/sensor/sensor_with_dedup.h index 026f2c2d2dd..6fab40f1e68 100644 --- a/esphome/components/sensor/sensor_with_dedup.h +++ b/esphome/components/sensor/sensor_with_dedup.h @@ -8,14 +8,12 @@ namespace esphome::sensor { /// Declare an optional child sensor that skips publishing a value equal to the last one. -#ifndef SUB_SENSOR_WITH_DEDUP #define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \ protected: \ sensor::SensorWithDedup name##_sensor_{}; \ \ public: \ void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_.set_sensor(sensor); } -#endif #define LOG_SENSOR_WITH_DEDUP(prefix, type, s) LOG_SENSOR(prefix, type, (s).get_sensor())