From e7a50f18f28596258df82498a5b727657800cad6 Mon Sep 17 00:00:00 2001 From: j0ta29 Date: Sun, 18 Feb 2024 14:51:50 +0000 Subject: [PATCH] fixed api dependency --- .../optolink/datapoint_component.cpp | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/esphome/components/optolink/datapoint_component.cpp b/esphome/components/optolink/datapoint_component.cpp index 3b552e1c01..6f00e3abe0 100644 --- a/esphome/components/optolink/datapoint_component.cpp +++ b/esphome/components/optolink/datapoint_component.cpp @@ -2,7 +2,9 @@ #include "datapoint_component.h" #include "optolink.h" +#ifdef USE_API #include "esphome/components/api/api_server.h" +#endif namespace esphome { namespace optolink { @@ -268,20 +270,24 @@ void DatapointComponent::subscribe_hass(std::string entity_id, std::functionsubscribe_home_assistant_state( - entity_id, optional(), [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()); - for (auto &subscription : hass_subscriptions_) { - if (subscription.last_state != state) { - if (subscription.entity_id == entity_id) { - subscription.last_state = state; - for (auto callback : subscription.callbacks) { - callback(state); +#ifdef USE_API + if (api::global_api_server != nullptr) { + api::global_api_server->subscribe_home_assistant_state( + entity_id, optional(), [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()); + for (auto &subscription : hass_subscriptions_) { + if (subscription.last_state != state) { + if (subscription.entity_id == entity_id) { + subscription.last_state = state; + for (auto callback : subscription.callbacks) { + callback(state); + } } } } - } - }); + }); + } +#endif } void conv2_100_F::encode(uint8_t *out, DPValue in) {