fixed api dependency

This commit is contained in:
j0ta29 2024-02-18 14:51:50 +00:00
parent cca0adc3bd
commit e7a50f18f2

View file

@ -2,7 +2,9 @@
#include "datapoint_component.h" #include "datapoint_component.h"
#include "optolink.h" #include "optolink.h"
#ifdef USE_API
#include "esphome/components/api/api_server.h" #include "esphome/components/api/api_server.h"
#endif
namespace esphome { namespace esphome {
namespace optolink { namespace optolink {
@ -268,20 +270,24 @@ void DatapointComponent::subscribe_hass(std::string entity_id, std::function<voi
subscription.callbacks.push_back(f); subscription.callbacks.push_back(f);
hass_subscriptions_.push_back(subscription); hass_subscriptions_.push_back(subscription);
api::global_api_server->subscribe_home_assistant_state( #ifdef USE_API
entity_id, optional<std::string>(), [this, entity_id](const std::string &state) { if (api::global_api_server != nullptr) {
ESP_LOGD(TAG, "received schedule plan from HASS entity '%s': %s", entity_id.c_str(), state.c_str()); api::global_api_server->subscribe_home_assistant_state(
for (auto &subscription : hass_subscriptions_) { entity_id, optional<std::string>(), [this, entity_id](const std::string &state) {
if (subscription.last_state != state) { ESP_LOGD(TAG, "received schedule plan from HASS entity '%s': %s", entity_id.c_str(), state.c_str());
if (subscription.entity_id == entity_id) { for (auto &subscription : hass_subscriptions_) {
subscription.last_state = state; 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);
}
} }
} }
} }
} });
}); }
#endif
} }
void conv2_100_F::encode(uint8_t *out, DPValue in) { void conv2_100_F::encode(uint8_t *out, DPValue in) {