move hass config to main
This commit is contained in:
17
src/hass.cpp
17
src/hass.cpp
@@ -6,9 +6,8 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string>
|
||||
|
||||
HomeAssistant::HomeAssistant(const std::string& addr, const std::string& ent) {
|
||||
address = addr;
|
||||
entity = ent;
|
||||
HomeAssistant::HomeAssistant(const Config& config){
|
||||
m_config = config;
|
||||
}
|
||||
|
||||
HomeAssistant::~HomeAssistant() {
|
||||
@@ -29,17 +28,17 @@ void HomeAssistant::event_handler(struct lws* wsi, const enum lws_callback_reaso
|
||||
if (ha_type == "auth_required") {
|
||||
json.clear();
|
||||
json["type"] = "auth";
|
||||
json["access_token"] = HASS_TOKEN;
|
||||
json["access_token"] = m_config.token;
|
||||
tx = json.dump();
|
||||
break;
|
||||
}
|
||||
|
||||
if (ha_type == "auth_ok") {
|
||||
applog(LOG::INF, "subscribing to entity \"{}\"", entity);
|
||||
applog(LOG::INF, "subscribing to entity \"{}\"", m_config.entity);
|
||||
json.clear();
|
||||
json["id"] = id++;
|
||||
json["type"] = "subscribe_entities";
|
||||
json["entity_ids"][0] = entity.c_str();
|
||||
json["entity_ids"][0] = m_config.entity.c_str();
|
||||
tx = json.dump();
|
||||
break;
|
||||
}
|
||||
@@ -53,9 +52,9 @@ void HomeAssistant::event_handler(struct lws* wsi, const enum lws_callback_reaso
|
||||
if (ha_type == "event") {
|
||||
json = json["event"];
|
||||
if (json.contains("a"))
|
||||
state = std::stof(json["a"][entity]["s"].get<std::string>());
|
||||
state = std::stof(json["a"][m_config.entity]["s"].get<std::string>());
|
||||
if (json.contains("c"))
|
||||
state = std::stof(json["c"][entity]["+"]["s"].get<std::string>());
|
||||
state = std::stof(json["c"][m_config.entity]["+"]["s"].get<std::string>());
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -134,7 +133,7 @@ int HomeAssistant::connect() {
|
||||
|
||||
i.context = context;
|
||||
i.port = 443;
|
||||
i.address = address.c_str();
|
||||
i.address = m_config.address.c_str();
|
||||
i.path = "/api/websocket";
|
||||
i.host = i.address;
|
||||
i.origin = i.address;
|
||||
|
||||
Reference in New Issue
Block a user