optimized HASS value update for text_sensor

This commit is contained in:
j0ta29 2024-01-10 13:21:05 +00:00
parent 6094830625
commit cf33d4b824
2 changed files with 7 additions and 3 deletions

View file

@ -272,9 +272,12 @@ void DatapointComponent::subscribe_hass(std::string entity_id, std::function<voi
entity_id, optional<std::string>(), [this, entity_id](const std::string &state) { entity_id, optional<std::string>(), [this, entity_id](const std::string &state) {
ESP_LOGD(TAG, "received schedule plan from HASS entity '%s': %s", entity_id.c_str(), state.c_str()); ESP_LOGD(TAG, "received schedule plan from HASS entity '%s': %s", entity_id.c_str(), state.c_str());
for (auto &subscription : hass_subscriptions_) { for (auto &subscription : hass_subscriptions_) {
if (subscription.entity_id == entity_id) { if (subscription.last_state != state) {
for (auto callback : subscription.callbacks) { if (subscription.entity_id == entity_id) {
callback(state); subscription.last_state = state;
for (auto callback : subscription.callbacks) {
callback(state);
}
} }
} }
} }

View file

@ -13,6 +13,7 @@ class Optolink;
struct HassSubscription { struct HassSubscription {
std::string entity_id; std::string entity_id;
std::string last_state;
std::vector<std::function<void(std::string)>> callbacks; std::vector<std::function<void(std::string)>> callbacks;
}; };