Browse Source

Print each HA config entry separately

sensors
Max Prokhorov 5 years ago
parent
commit
31a0772e37
2 changed files with 53 additions and 17 deletions
  1. +49
    -16
      code/espurna/homeassistant.ino
  2. +4
    -1
      code/html/custom.js

+ 49
- 16
code/espurna/homeassistant.ino View File

@ -9,10 +9,13 @@ Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
#if HOMEASSISTANT_SUPPORT
#include <ArduinoJson.h>
#include <queue>
bool _haEnabled = false;
bool _haSendFlag = false;
std::queue<uint32_t> _ha_send_config;
// -----------------------------------------------------------------------------
// UTILS
// -----------------------------------------------------------------------------
@ -147,9 +150,7 @@ void _haSendSwitches() {
// -----------------------------------------------------------------------------
String _haGetConfig() {
String output;
void _haDumpConfig(std::function<void(String&)> printer) {
#if (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE) || (defined(ITEAD_SLAMPHER))
String type = String("light");
@ -163,8 +164,12 @@ String _haGetConfig() {
JsonObject& config = jsonBuffer.createObject();
_haSendSwitch(i, config);
output += "\n" + type + ":\n";
String output;
output.reserve(config.measureLength() + 32);
output += "\n\n" + type + ":\n";
bool first = true;
for (auto kv : config) {
if (first) {
output += " - ";
@ -172,11 +177,17 @@ String _haGetConfig() {
} else {
output += " ";
}
output += kv.key + String(": ") + kv.value.as<String>() + String("\n");
output += kv.key;
output += ": ";
output += kv.value.as<String>();
output += "\n";
}
output += " ";
jsonBuffer.clear();
printer(output);
}
#if SENSOR_SUPPORT
@ -187,8 +198,12 @@ String _haGetConfig() {
JsonObject& config = jsonBuffer.createObject();
_haSendMagnitude(i, config);
output += "\nsensor:\n";
String output;
output.reserve(config.measureLength() + 32);
output += "\n\nsensor:\n";
bool first = true;
for (auto kv : config) {
if (first) {
output += " - ";
@ -198,18 +213,21 @@ String _haGetConfig() {
}
String value = kv.value.as<String>();
value.replace("%", "'%'");
output += kv.key + String(": ") + value + String("\n");
output += kv.key;
output += ": ";
output += value;
output += "\n";
}
output += "\n";
output += " ";
jsonBuffer.clear();
printer(output);
}
#endif
return output;
}
void _haSend() {
@ -253,11 +271,7 @@ void _haWebSocketOnSend(JsonObject& root) {
void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
if (strcmp(action, "haconfig") == 0) {
String output = _haGetConfig();
output.replace(" ", "&nbsp;");
output.replace("\n", "<br />");
output = String("{\"haConfig\": \"") + output + String("\"}");
wsSend(client_id, output.c_str());
_ha_send_config.push(client_id);
}
}
@ -268,7 +282,10 @@ void _haWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& d
void _haInitCommands() {
terminalRegisterCommand(F("HA.CONFIG"), [](Embedis* e) {
DEBUG_MSG(_haGetConfig().c_str());
_haDumpConfig([](String& data) {
DEBUG_MSG(data.c_str());
});
DEBUG_MSG("\n");
terminalOK();
});
@ -296,6 +313,21 @@ void _haInitCommands() {
// -----------------------------------------------------------------------------
void _haLoop() {
if (_ha_send_config.empty()) return;
uint32_t client_id = _ha_send_config.front();
_ha_send_config.pop();
_haDumpConfig([client_id](String& output) {
output.replace(" ", "&nbsp;");
output.replace("\n", "<br />");
output = String("{\"haConfig\": \"") + output + String("\"}");
wsSend(client_id, output.c_str());
yield();
});
}
void haSetup() {
_haConfigure();
@ -317,6 +349,7 @@ void haSetup() {
// Main callbacks
espurnaRegisterReload(_haConfigure);
espurnaRegisterLoop(_haLoop);
}


+ 4
- 1
code/html/custom.js View File

@ -658,6 +658,7 @@ function doScan() {
function doHAConfig() {
$("#haConfig").html("");
$("#haConfig").show();
sendAction("haconfig", {});
return false;
}
@ -1366,7 +1367,9 @@ function processData(data) {
// -----------------------------------------------------------------------------
if ("haConfig" === key) {
$("#haConfig").show();
websock.send("{}");
$("#haConfig").append(value);
return;
}
// -----------------------------------------------------------------------------


Loading…
Cancel
Save