From a3e626757eefcfa80716b972ed4fd0dcc4f467e8 Mon Sep 17 00:00:00 2001 From: Keith Burzinski Date: Wed, 23 Jul 2025 21:22:54 -0500 Subject: [PATCH] [helpers] Add "unknown" value handling to ``Deduplicator`` (#9855) --- esphome/core/helpers.h | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 260479c9e1..f67f13b71f 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -578,21 +578,28 @@ template class CallbackManager { /// Helper class to deduplicate items in a series of values. template class Deduplicator { public: - /// Feeds the next item in the series to the deduplicator and returns whether this is a duplicate. + /// Feeds the next item in the series to the deduplicator and returns false if this is a duplicate. bool next(T value) { - if (this->has_value_) { - if (this->last_value_ == value) - return false; + if (this->has_value_ && !this->value_unknown_ && this->last_value_ == value) { + return false; } this->has_value_ = true; + this->value_unknown_ = false; this->last_value_ = value; return true; } - /// Returns whether this deduplicator has processed any items so far. + /// Returns true if the deduplicator's value was previously known. + bool next_unknown() { + bool ret = !this->value_unknown_; + this->value_unknown_ = true; + return ret; + } + /// Returns true if this deduplicator has processed any items. bool has_value() const { return this->has_value_; } protected: bool has_value_{false}; + bool value_unknown_{false}; T last_value_{}; };