From db0facf928c84c50730bf3788ddbc500e3424552 Mon Sep 17 00:00:00 2001 From: Lexi Beavil Date: Sat, 6 Apr 2024 17:16:07 +0100 Subject: [PATCH 1/2] Entities now update/init if _networking_enabled --- .../src/utility/ha_entity.cpp | 104 +++++++++--------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/lib/homeassistant-api/src/utility/ha_entity.cpp b/lib/homeassistant-api/src/utility/ha_entity.cpp index 09a9600..b0ef2cd 100644 --- a/lib/homeassistant-api/src/utility/ha_entity.cpp +++ b/lib/homeassistant-api/src/utility/ha_entity.cpp @@ -50,67 +50,69 @@ int HomeAssistant::getNumEntities() { } void HomeAssistant::createEntities() { -#if NETWORKING_ENABLED == 1 - _httpClient.begin(_host, _port, "/api/states"); - _httpClient.addHeader("Authorization", _token); - int status_code = _httpClient.GET(); - if (status_code != 200) + if (_networking_enabled) { - log_e("/api/states returned error code %d", status_code); - return; - } - log_d("Network request underway, code 200"); - JsonDocument entity_json; - WiFiClient http_stream = _httpClient.getStream(); - http_stream.find("["); - do - { - deserializeJson(entity_json, http_stream); - const String entity_id = entity_json["entity_id"]; - if (entity_id.startsWith("light.")) + _httpClient.begin(_host, _port, "/api/states"); + _httpClient.addHeader("Authorization", _token); + int status_code = _httpClient.GET(); + if (status_code != 200) { - log_d("Light(%s)", entity_id.c_str()); - if (!addEntity(new Light(entity_id))) - break; + log_e("/api/states returned error code %d", status_code); + return; } - else if (entity_id.startsWith("switch.")) + log_d("Network request underway, code 200"); + JsonDocument entity_json; + WiFiClient http_stream = _httpClient.getStream(); + http_stream.find("["); + do { - log_d("Switch(%s)", entity_id.c_str()); - if (!addEntity(new Switch(entity_id))) - break; - } - //! cleanup - } while (http_stream.findUntil(",", "]")); - _httpClient.end(); -#endif + deserializeJson(entity_json, http_stream); + const String entity_id = entity_json["entity_id"]; + if (entity_id.startsWith("light.")) + { + log_d("Light(%s)", entity_id.c_str()); + if (!addEntity(new Light(entity_id))) + break; + } + else if (entity_id.startsWith("switch.")) + { + log_d("Switch(%s)", entity_id.c_str()); + if (!addEntity(new Switch(entity_id))) + break; + } + //! cleanup + } while (http_stream.findUntil(",", "]")); + _httpClient.end(); + } } void HomeAssistant::updateAllStates() { -#if NETWORKING_ENABLED == 1 - _httpClient.begin(_host, _port, "/api/states"); - _httpClient.addHeader("Authorization", _token); - int status_code = _httpClient.GET(); - if (status_code != 200) + if (_networking_enabled) { - log_e("/api/states returned error code %d", status_code); - return; - } - log_d("Network request underway, code 200"); - JsonDocument entity_json; - WiFiClient http_stream = _httpClient.getStream(); - http_stream.find("["); - do - { - deserializeJson(entity_json, http_stream); - Entity *entity = getEntityByIdentifier(entity_json["entity_id"].as()); - if (entity != nullptr) + _httpClient.begin(_host, _port, "/api/states"); + _httpClient.addHeader("Authorization", _token); + int status_code = _httpClient.GET(); + if (status_code != 200) { - entity->updateStateFromJSON(entity_json); + log_e("/api/states returned error code %d", status_code); + return; } - //! cleanup - } while (http_stream.findUntil(",", "]")); - _httpClient.end(); -#endif + log_d("Network request underway, code 200"); + JsonDocument entity_json; + WiFiClient http_stream = _httpClient.getStream(); + http_stream.find("["); + do + { + deserializeJson(entity_json, http_stream); + Entity *entity = getEntityByIdentifier(entity_json["entity_id"].as()); + if (entity != nullptr) + { + entity->updateStateFromJSON(entity_json); + } + //! cleanup + } while (http_stream.findUntil(",", "]")); + _httpClient.end(); + } } Entity *HomeAssistant::getActiveEntity() { From b230fc65b31b50f78a8d59987020b4b6dfc9f732 Mon Sep 17 00:00:00 2001 From: Lexi Beavil Date: Sat, 6 Apr 2024 17:16:27 +0100 Subject: [PATCH 2/2] removed global NETWORKING_ENABLED as no longer needed --- platformio.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 3eb8f91..ed3c547 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,7 +22,6 @@ build_flags = -D LOGGING -D CORE_DEBUG_LEVEL=5 -frtti - -D NETWORKING_ENABLED=0 ; set to 1 to enable the networking build_src_filter = +<*> lib_deps =