Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HA entity updating #36

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions lib/homeassistant-api/src/utility/ha_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>());
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<String>());
if (entity != nullptr)
{
entity->updateStateFromJSON(entity_json);
}
//! cleanup
} while (http_stream.findUntil(",", "]"));
_httpClient.end();
}
}
Entity *HomeAssistant::getActiveEntity()
{
Expand Down
1 change: 0 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down