publish ha command

This commit is contained in:
proddy
2020-10-20 21:07:19 +02:00
parent 1d1e7f38b6
commit 73008bfe5f
26 changed files with 122 additions and 89 deletions

View File

@@ -121,12 +121,17 @@ void EMSESPShell::add_console_commands() {
commands->add_command(ShellContext::MAIN,
CommandFlags::ADMIN,
flash_string_vector{F_(publish)},
[&](Shell & shell, const std::vector<std::string> & arguments __attribute__((unused))) {
shell.printfln(F("Publishing all data to MQTT"));
EMSESP::publish_all();
flash_string_vector{F_(ha_optional)},
[&](Shell & shell, const std::vector<std::string> & arguments) {
if (arguments.empty()) {
EMSESP::publish_all();
shell.printfln(F("Published all data to MQTT"));
} else {
EMSESP::publish_all(true);
shell.printfln(F("Published all data to MQTT, including HA configs"));
}
});
commands->add_command(ShellContext::MAIN,
CommandFlags::USER,
flash_string_vector{F_(show)},

View File

@@ -80,9 +80,13 @@ Boiler::Boiler(uint8_t device_type, int8_t device_id, uint8_t product_id, const
// create the config topics for Home Assistant MQTT Discovery
// for each of the main elements
bool Boiler::register_mqtt_ha_config() {
void Boiler::register_mqtt_ha_config(bool force) {
if ((mqtt_ha_config_ && !force)) {
return;
}
if (!Mqtt::connected()) {
return false;
return;
}
// Create the Master device
@@ -169,7 +173,7 @@ bool Boiler::register_mqtt_ha_config() {
Mqtt::register_mqtt_ha_sensor(nullptr, F_(mqtt_suffix_ww), F_(wWStarts), this->device_type(), "wWStarts", nullptr, nullptr);
Mqtt::register_mqtt_ha_sensor(nullptr, F_(mqtt_suffix_ww), F_(wWWorkM), this->device_type(), "wWWorkM", nullptr, nullptr);
return true;
mqtt_ha_config_ = true; // done
}
// send stuff to the Web UI
@@ -611,10 +615,10 @@ bool Boiler::export_values_main(JsonObject & output) {
}
// publish values via MQTT
void Boiler::publish_values(JsonObject & data) {
void Boiler::publish_values(JsonObject & data, bool force) {
// handle HA first
if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!mqtt_ha_config_)) {
mqtt_ha_config_ = register_mqtt_ha_config();
if (Mqtt::mqtt_format() == Mqtt::Format::HA) {
register_mqtt_ha_config(force);
}
DynamicJsonDocument doc_main(EMSESP_MAX_JSON_SIZE_LARGE);

View File

@@ -39,14 +39,14 @@ class Boiler : public EMSdevice {
Boiler(uint8_t device_type, int8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();
private:
static uuid::log::Logger logger_;
bool register_mqtt_ha_config();
void register_mqtt_ha_config(bool force);
void check_active();
bool export_values_main(JsonObject & doc);
bool export_values_ww(JsonObject & doc);

View File

@@ -37,7 +37,7 @@ void Connect::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Connect::publish_values(JsonObject & data) {
void Connect::publish_values(JsonObject & data, bool force) {
}
// check to see if values have been updated

View File

@@ -36,7 +36,7 @@ class Connect : public EMSdevice {
Connect(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();

View File

@@ -37,7 +37,7 @@ void Controller::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Controller::publish_values(JsonObject & data) {
void Controller::publish_values(JsonObject & data, bool force) {
}
// check to see if values have been updated

View File

@@ -36,7 +36,7 @@ class Controller : public EMSdevice {
Controller(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();

View File

@@ -37,7 +37,7 @@ void Gateway::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Gateway::publish_values(JsonObject & data) {
void Gateway::publish_values(JsonObject & data, bool force) {
}
// check to see if values have been updated

View File

@@ -36,7 +36,7 @@ class Gateway : public EMSdevice {
Gateway(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();

View File

@@ -37,7 +37,7 @@ void Generic::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Generic::publish_values(JsonObject & data) {
void Generic::publish_values(JsonObject & data, bool force) {
}
// check to see if values have been updated

View File

@@ -36,7 +36,7 @@ class Generic : public EMSdevice {
Generic(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();

View File

@@ -42,7 +42,7 @@ void Heatpump::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Heatpump::publish_values(JsonObject & data) {
void Heatpump::publish_values(JsonObject & data, bool force) {
}
// check to see if values have been updated

View File

@@ -36,7 +36,7 @@ class Heatpump : public EMSdevice {
Heatpump(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();

View File

@@ -138,7 +138,12 @@ bool Mixing::command_info(const char * value, const int8_t id, JsonObject & outp
// publish values via MQTT
// topic is mixing_data<id>
void Mixing::publish_values(JsonObject & data) {
void Mixing::publish_values(JsonObject & data, bool force) {
// handle HA first
if (Mqtt::mqtt_format() == Mqtt::Format::HA) {
register_mqtt_ha_config(force);
}
if (Mqtt::mqtt_format() == Mqtt::Format::SINGLE) {
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
JsonObject output = doc.to<JsonObject>();
@@ -150,18 +155,21 @@ void Mixing::publish_values(JsonObject & data) {
Mqtt::publish(topic, doc.as<JsonObject>());
}
} else {
if (export_values(Mqtt::mqtt_format(), data)) {
// if we're using Home Assistant and haven't created the MQTT Discovery topics, do it now
if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!mqtt_ha_config_)) {
register_mqtt_ha_config();
mqtt_ha_config_ = true;
}
}
// format is HA or Nested. This is bundled together and sent in emsesp.cpp
(void)export_values(Mqtt::mqtt_format(), data);
}
}
// publish config topic for HA MQTT Discovery
void Mixing::register_mqtt_ha_config() {
void Mixing::register_mqtt_ha_config(bool force) {
if ((mqtt_ha_config_ && !force)) {
return;
}
if (!Mqtt::connected()) {
return;
}
// Create the Master device
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
@@ -208,6 +216,8 @@ void Mixing::register_mqtt_ha_config() {
Mqtt::register_mqtt_ha_sensor(wwc_name, nullptr, F_(pumpStatus), this->device_type(), "pumpStatus", nullptr, nullptr);
Mqtt::register_mqtt_ha_sensor(wwc_name, nullptr, F_(tempStatus), this->device_type(), "tempStatus", nullptr, nullptr);
}
mqtt_ha_config_ = true; // done
}
// creates JSON doc from values

View File

@@ -37,7 +37,7 @@ class Mixing : public EMSdevice {
Mixing(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();
@@ -45,7 +45,7 @@ class Mixing : public EMSdevice {
static uuid::log::Logger logger_;
bool export_values(uint8_t mqtt_format, JsonObject & doc);
void register_mqtt_ha_config();
void register_mqtt_ha_config(bool force);
bool command_info(const char * value, const int8_t id, JsonObject & output);
void process_MMPLUSStatusMessage_HC(std::shared_ptr<const Telegram> telegram);

View File

@@ -130,7 +130,12 @@ void Solar::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Solar::publish_values(JsonObject & data) {
void Solar::publish_values(JsonObject & data, bool force) {
// handle HA first
if (Mqtt::mqtt_format() == Mqtt::Format::HA) {
register_mqtt_ha_config(force);
}
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
JsonObject output = doc.to<JsonObject>();
if (export_values(output)) {
@@ -138,17 +143,20 @@ void Solar::publish_values(JsonObject & data) {
Mqtt::publish(F("ww_data"), doc.as<JsonObject>());
} else {
Mqtt::publish(F("solar_data"), doc.as<JsonObject>());
// if we're using Home Assistant and haven't created the MQTT Discovery topics, do it now
if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!mqtt_ha_config_)) {
register_mqtt_ha_config();
mqtt_ha_config_ = true;
}
}
}
}
// publish config topic for HA MQTT Discovery
void Solar::register_mqtt_ha_config() {
void Solar::register_mqtt_ha_config(bool force) {
if ((mqtt_ha_config_ && !force)) {
return;
}
if (!Mqtt::connected()) {
return;
}
// Create the Master device
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
doc["name"] = F("EMS-ESP");
@@ -183,6 +191,9 @@ void Solar::register_mqtt_ha_config() {
Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(valveStatus), this->device_type(), "valveStatus", nullptr, nullptr);
Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(tankHeated), this->device_type(), "tankHeated", nullptr, nullptr);
Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(collectorShutdown), this->device_type(), "collectorShutdown", nullptr, nullptr);
mqtt_ha_config_ = true; // done
}
// creates JSON doc from values

View File

@@ -37,7 +37,7 @@ class Solar : public EMSdevice {
Solar(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();
@@ -46,7 +46,7 @@ class Solar : public EMSdevice {
bool export_values(JsonObject & doc);
bool command_info(const char * value, const int8_t id, JsonObject & output);
void register_mqtt_ha_config();
void register_mqtt_ha_config(bool force);
int16_t collectorTemp_ = EMS_VALUE_SHORT_NOTSET; // TS1: Temperature sensor for collector array 1
int16_t tankBottomTemp_ = EMS_VALUE_SHORT_NOTSET; // TS2: Temperature sensor 1 cylinder, bottom (solar thermal system)

View File

@@ -37,7 +37,7 @@ void Switch::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Switch::publish_values(JsonObject & data) {
void Switch::publish_values(JsonObject & data, bool force) {
}
// check to see if values have been updated

View File

@@ -36,7 +36,7 @@ class Switch : public EMSdevice {
Switch(uint8_t device_type, uint8_t device_id, uint8_t product_id, const std::string & version, const std::string & name, uint8_t flags, uint8_t brand);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();

View File

@@ -315,10 +315,11 @@ void Thermostat::show_values(uuid::console::Shell & shell) {
}
// publish values via MQTT
void Thermostat::publish_values(JsonObject & data) {
void Thermostat::publish_values(JsonObject & data, bool force) {
if (EMSESP::actual_master_thermostat() != this->device_id()) {
return;
}
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
JsonObject output = doc.to<JsonObject>();
bool has_data = false;
@@ -336,6 +337,10 @@ void Thermostat::publish_values(JsonObject & data) {
// if we're in HA or CUSTOM, send out the complete topic with all the data
if (Mqtt::mqtt_format() != Mqtt::Format::SINGLE && has_data) {
// see if we have already registered this with HA MQTT Discovery, if not send the config
if (Mqtt::mqtt_format() == Mqtt::Format::HA) {
ha_config(force);
}
Mqtt::publish(F("thermostat_data"), output);
}
}
@@ -452,13 +457,6 @@ bool Thermostat::export_values_main(JsonObject & rootThermostat) {
rootThermostat["wwcircmode"] = Helpers::render_enum(s, {"off", "on", "auto"}, wwCircMode_);
}
if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!ha_registered())) {
// see if we have already registered this with HA MQTT Discovery, if not send the config
if (register_mqtt_ha_config()) {
ha_registered(true);
}
}
return (rootThermostat.size());
}
@@ -625,11 +623,6 @@ bool Thermostat::export_values_hc(uint8_t mqtt_format, JsonObject & rootThermost
strlcat(topic, Helpers::itoa(s, hc->hc_num()), 30); // append hc to topic
Mqtt::publish(topic, rootThermostat);
rootThermostat.clear(); // clear object
} else if ((mqtt_format == Mqtt::Format::HA) && (!hc->ha_registered())) {
// see if we have already registered this with HA MQTT Discovery, if not send the config
if (register_mqtt_ha_config(hc->hc_num())) {
hc->ha_registered(true);
}
}
}
}
@@ -637,6 +630,26 @@ bool Thermostat::export_values_hc(uint8_t mqtt_format, JsonObject & rootThermost
return (has_data);
}
// set up HA MQTT Discovery
void Thermostat::ha_config(bool force) {
if (!Mqtt::connected()) {
return;
}
if (force || !ha_registered()) {
register_mqtt_ha_config();
ha_registered(true);
}
// check to see which heating circuits need publishing
for (const auto & hc : heating_circuits_) {
if (force || (hc->is_active() && !hc->ha_registered())) {
register_mqtt_ha_config(hc->hc_num());
hc->ha_registered(true);
}
}
}
// returns the heating circuit object based on the hc number
// of nullptr if it doesn't exist yet
std::shared_ptr<Thermostat::HeatingCircuit> Thermostat::heating_circuit(const uint8_t hc_num) {
@@ -739,11 +752,7 @@ std::shared_ptr<Thermostat::HeatingCircuit> Thermostat::heating_circuit(std::sha
// publish config topic for HA MQTT Discovery
// homeassistant/climate/ems-esp/thermostat/config
bool Thermostat::register_mqtt_ha_config() {
if (!Mqtt::connected()) {
return false;
}
void Thermostat::register_mqtt_ha_config() {
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
doc["uniq_id"] = F("thermostat");
doc["ic"] = F("mdi:home-thermometer-outline");
@@ -783,17 +792,11 @@ bool Thermostat::register_mqtt_ha_config() {
Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(wwtemplow), this->device_type(), "wwtemplow", F_(degrees), F_(icontemperature));
Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(wwcircmode), this->device_type(), "wwcircmode", nullptr, nullptr);
}
return true;
}
// publish config topic for HA MQTT Discovery
// e.g. homeassistant/climate/ems-esp/thermostat_hc1/config
bool Thermostat::register_mqtt_ha_config(uint8_t hc_num) {
if (!Mqtt::connected()) {
return false;
}
void Thermostat::register_mqtt_ha_config(uint8_t hc_num) {
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
char str1[40];
@@ -919,8 +922,6 @@ bool Thermostat::register_mqtt_ha_config(uint8_t hc_num) {
default:
break;
}
return true;
}
// for HA specifically when receiving over MQTT in the thermostat topic

View File

@@ -100,7 +100,7 @@ class Thermostat : public EMSdevice {
static std::string mode_tostring(uint8_t mode);
virtual void show_values(uuid::console::Shell & shell);
virtual void publish_values(JsonObject & data);
virtual void publish_values(JsonObject & data, bool force);
virtual void device_info_web(JsonArray & root);
virtual bool updated_values();
@@ -237,8 +237,9 @@ class Thermostat : public EMSdevice {
std::shared_ptr<Thermostat::HeatingCircuit> heating_circuit(std::shared_ptr<const Telegram> telegram);
std::shared_ptr<Thermostat::HeatingCircuit> heating_circuit(const uint8_t hc_num);
bool register_mqtt_ha_config();
bool register_mqtt_ha_config(uint8_t hc_num);
void register_mqtt_ha_config();
void register_mqtt_ha_config(uint8_t hc_num);
void ha_config(bool force = false);
bool thermostat_ha_cmd(const char * message, uint8_t hc_num);
bool command_info(const char * value, const int8_t id, JsonObject & output);

View File

@@ -140,10 +140,10 @@ class EMSdevice {
void register_mqtt_cmd(const __FlashStringHelper * cmd, cmdfunction_p f);
// virtual functions overrules by derived classes
virtual void show_values(uuid::console::Shell & shell) = 0;
virtual void publish_values(JsonObject & data) = 0;
virtual bool updated_values() = 0;
virtual void device_info_web(JsonArray & root) = 0;
virtual void show_values(uuid::console::Shell & shell) = 0;
virtual void publish_values(JsonObject & data, bool force = false) = 0;
virtual bool updated_values() = 0;
virtual void device_info_web(JsonArray & root) = 0;
std::string telegram_type_name(std::shared_ptr<const Telegram> telegram);

View File

@@ -288,12 +288,12 @@ void EMSESP::show_sensor_values(uuid::console::Shell & shell) {
}
// MQTT publish everything, immediately
void EMSESP::publish_all() {
void EMSESP::publish_all(bool force) {
if (Mqtt::connected()) {
publish_device_values(EMSdevice::DeviceType::BOILER);
publish_device_values(EMSdevice::DeviceType::THERMOSTAT);
publish_device_values(EMSdevice::DeviceType::SOLAR);
publish_device_values(EMSdevice::DeviceType::MIXING);
publish_device_values(EMSdevice::DeviceType::BOILER, force);
publish_device_values(EMSdevice::DeviceType::THERMOSTAT, force);
publish_device_values(EMSdevice::DeviceType::SOLAR, force);
publish_device_values(EMSdevice::DeviceType::MIXING, force);
publish_other_values();
publish_sensor_values(true);
system_.send_heartbeat();
@@ -302,13 +302,13 @@ void EMSESP::publish_all() {
// create json doc for the devices values and add to MQTT publish queue
// special case for Mixing units, since we want to bundle all devices together into one payload
void EMSESP::publish_device_values(uint8_t device_type) {
void EMSESP::publish_device_values(uint8_t device_type, bool force) {
if (device_type == EMSdevice::DeviceType::MIXING && Mqtt::mqtt_format() != Mqtt::Format::SINGLE) {
DynamicJsonDocument doc(EMSESP_MAX_JSON_SIZE_LARGE);
JsonObject output = doc.to<JsonObject>();
for (const auto & emsdevice : emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_type)) {
emsdevice->publish_values(output);
emsdevice->publish_values(output, force);
}
}
doc.shrinkToFit();
@@ -319,7 +319,7 @@ void EMSESP::publish_device_values(uint8_t device_type) {
for (const auto & emsdevice : emsdevices) {
if (emsdevice && (emsdevice->device_type() == device_type)) {
JsonObject dummy;
emsdevice->publish_values(dummy);
emsdevice->publish_values(dummy, force);
}
}
}

View File

@@ -61,10 +61,10 @@ class EMSESP {
static void start();
static void loop();
static void publish_device_values(uint8_t device_type);
static void publish_device_values(uint8_t device_type, bool force = false);
static void publish_other_values();
static void publish_sensor_values(const bool force = false);
static void publish_all();
static void publish_all(bool force = false);
#ifdef EMSESP_STANDALONE
static void run_test(uuid::console::Shell & shell, const std::string & command); // only for testing

View File

@@ -107,6 +107,7 @@ MAKE_PSTR(wifi_ssid_fmt, "WiFi SSID = %s")
MAKE_PSTR(wifi_password_fmt, "WiFi Password = %S")
MAKE_PSTR(mqtt_heartbeat_fmt, "MQTT Heartbeat is %s")
MAKE_PSTR(cmd_optional, "[cmd]")
MAKE_PSTR(ha_optional, "[ha]")
MAKE_PSTR(deep_optional, "[deep]")
MAKE_PSTR(tx_mode_fmt, "Tx mode = %d")
MAKE_PSTR(bus_id_fmt, "Bus ID = %02X")

View File

@@ -1 +1 @@
#define EMSESP_APP_VERSION "2.1.0b9"
#define EMSESP_APP_VERSION "2.1.0b10"