-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from omerfaruk-aran/feature/handle-value-changes
Handle UI Update Issues - Improved Value Change Handling in Samsung AC Integration
- Loading branch information
Showing
15 changed files
with
212 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#pragma once | ||
#include <iostream> | ||
|
||
#include <string> | ||
|
||
namespace esphome | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#ifndef DEVICE_STATE_TRACKER_H | ||
#define DEVICE_STATE_TRACKER_H | ||
|
||
#include "esphome/core/log.h" | ||
#include "esphome/core/helpers.h" | ||
#include <map> | ||
#include <string> | ||
|
||
namespace esphome { | ||
|
||
template <typename T> | ||
class DeviceStateTracker { | ||
public: | ||
DeviceStateTracker(unsigned long timeout_period) | ||
: TIMEOUT_PERIOD(timeout_period) {} | ||
|
||
void update(const std::string &address, const T ¤t_value) { | ||
unsigned long now = millis(); | ||
|
||
if (pending_changes_.find(address) != pending_changes_.end()) { | ||
if (current_value == pending_changes_[address]) { | ||
pending_changes_.erase(address); | ||
} else { | ||
ESP_LOGI("device_state_tracker", "Stale value received for device: %s, ignoring.", address.c_str()); | ||
return; | ||
} | ||
} | ||
|
||
if (last_values_.find(address) == last_values_.end() || last_values_[address] != current_value) { | ||
pending_changes_[address] = current_value; | ||
last_values_[address] = current_value; | ||
last_update_time_[address] = now; | ||
|
||
ESP_LOGI("device_state_tracker", "Value changed for device: %s", address.c_str()); | ||
} else { | ||
ESP_LOGD("device_state_tracker", "No change in value for device: %s", address.c_str()); | ||
|
||
if (now - last_update_time_[address] > TIMEOUT_PERIOD) { | ||
if (pending_changes_.find(address) != pending_changes_.end()) { | ||
ESP_LOGW("device_state_tracker", "Timeout for device: %s, forcing update.", address.c_str()); | ||
pending_changes_.erase(address); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private: | ||
std::map<std::string, T> last_values_; | ||
std::map<std::string, unsigned long> last_update_time_; | ||
std::map<std::string, T> pending_changes_; | ||
const unsigned long TIMEOUT_PERIOD; | ||
}; | ||
|
||
} // namespace esphome | ||
|
||
#endif // DEVICE_STATE_TRACKER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
#include <iostream> | ||
#include "protocol.h" | ||
|
||
namespace esphome | ||
|
Oops, something went wrong.