mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
use hostname for HA MQTT configs - #549
This commit is contained in:
@@ -86,7 +86,11 @@ void Boiler::register_mqtt_ha_config() {
|
|||||||
doc["name"] = F("Service Code");
|
doc["name"] = F("Service Code");
|
||||||
doc["uniq_id"] = F("boiler");
|
doc["uniq_id"] = F("boiler");
|
||||||
doc["ic"] = F("mdi:home-thermometer-outline");
|
doc["ic"] = F("mdi:home-thermometer-outline");
|
||||||
doc["stat_t"] = F("ems-esp/boiler_data");
|
|
||||||
|
char stat_t[50];
|
||||||
|
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/boiler_data"), System::hostname().c_str());
|
||||||
|
doc["stat_t"] = stat_t;
|
||||||
|
|
||||||
doc["val_tpl"] = F("{{value_json.serviceCode}}");
|
doc["val_tpl"] = F("{{value_json.serviceCode}}");
|
||||||
JsonObject dev = doc.createNestedObject("dev");
|
JsonObject dev = doc.createNestedObject("dev");
|
||||||
dev["name"] = F("EMS-ESP Boiler");
|
dev["name"] = F("EMS-ESP Boiler");
|
||||||
|
|||||||
@@ -143,13 +143,17 @@ void Solar::publish_values() {
|
|||||||
void Solar::register_mqtt_ha_config() {
|
void Solar::register_mqtt_ha_config() {
|
||||||
// Create the Master device
|
// Create the Master device
|
||||||
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
|
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
|
||||||
doc["name"] = "EMS-ESP";
|
doc["name"] = F("EMS-ESP");
|
||||||
doc["uniq_id"] = "solar";
|
doc["uniq_id"] = F("solar");
|
||||||
doc["ic"] = "mdi:home-thermometer-outline";
|
doc["ic"] = F("mdi:home-thermometer-outline");
|
||||||
doc["stat_t"] = "ems-esp/solar_data";
|
|
||||||
doc["val_tpl"] = "{{value_json.solarPump}}";
|
char stat_t[50];
|
||||||
|
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/solar_data"), System::hostname().c_str());
|
||||||
|
doc["stat_t"] = stat_t;
|
||||||
|
|
||||||
|
doc["val_tpl"] = F("{{value_json.solarPump}}");
|
||||||
JsonObject dev = doc.createNestedObject("dev");
|
JsonObject dev = doc.createNestedObject("dev");
|
||||||
dev["name"] = "EMS-ESP Solar";
|
dev["name"] = F("EMS-ESP Solar");
|
||||||
dev["sw"] = EMSESP_APP_VERSION;
|
dev["sw"] = EMSESP_APP_VERSION;
|
||||||
dev["mf"] = this->brand_to_string();
|
dev["mf"] = this->brand_to_string();
|
||||||
dev["mdl"] = this->name();
|
dev["mdl"] = this->name();
|
||||||
|
|||||||
@@ -722,13 +722,13 @@ void Thermostat::register_mqtt_ha_config(uint8_t hc_num) {
|
|||||||
snprintf_P(str2, sizeof(str2), PSTR("thermostat_hc%d"), hc_num);
|
snprintf_P(str2, sizeof(str2), PSTR("thermostat_hc%d"), hc_num);
|
||||||
doc["uniq_id"] = str2;
|
doc["uniq_id"] = str2;
|
||||||
|
|
||||||
|
doc["~"] = System::hostname(); // ems-esp
|
||||||
|
|
||||||
char str3[40];
|
char str3[40];
|
||||||
snprintf_P(str3, sizeof(str3), PSTR("~/%s"), str2);
|
snprintf_P(str3, sizeof(str3), PSTR("~/%s"), str2);
|
||||||
|
|
||||||
doc["uniq_id"] = str2;
|
|
||||||
doc["~"] = F("ems-esp");
|
|
||||||
doc["mode_cmd_t"] = str3;
|
doc["mode_cmd_t"] = str3;
|
||||||
doc["temp_cmd_t"] = str3;
|
doc["temp_cmd_t"] = str3;
|
||||||
|
|
||||||
doc["mode_stat_t"] = F("~/thermostat_data");
|
doc["mode_stat_t"] = F("~/thermostat_data");
|
||||||
doc["temp_stat_t"] = F("~/thermostat_data");
|
doc["temp_stat_t"] = F("~/thermostat_data");
|
||||||
doc["curr_temp_t"] = F("~/thermostat_data");
|
doc["curr_temp_t"] = F("~/thermostat_data");
|
||||||
|
|||||||
14
src/mqtt.cpp
14
src/mqtt.cpp
@@ -483,11 +483,13 @@ void Mqtt::on_connect() {
|
|||||||
// all the values from the heartbeat payload will be added as attributes to the entity state
|
// all the values from the heartbeat payload will be added as attributes to the entity state
|
||||||
void Mqtt::ha_status() {
|
void Mqtt::ha_status() {
|
||||||
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
|
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
|
||||||
|
|
||||||
doc["name"] = F("EMS-ESP status");
|
doc["name"] = F("EMS-ESP status");
|
||||||
doc["uniq_id"] = F("status");
|
doc["uniq_id"] = F("status");
|
||||||
doc["avty_t"] = F("ems-esp/status");
|
doc["~"] = System::hostname(); // ems-esp
|
||||||
doc["json_attr_t"] = F("ems-esp/heartbeat");
|
doc["avty_t"] = F("~/status");
|
||||||
doc["stat_t"] = F("ems-esp/heartbeat");
|
doc["json_attr_t"] = F("~/heartbeat");
|
||||||
|
doc["stat_t"] = F("~/heartbeat");
|
||||||
doc["val_tpl"] = F("{{value_json['status']}}");
|
doc["val_tpl"] = F("{{value_json['status']}}");
|
||||||
doc["ic"] = F("mdi:home-thermometer-outline");
|
doc["ic"] = F("mdi:home-thermometer-outline");
|
||||||
|
|
||||||
@@ -767,8 +769,8 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
|||||||
snprintf_P(topic, sizeof(topic), PSTR("homeassistant/sensor/ems-esp/%s/config"), uniq.c_str());
|
snprintf_P(topic, sizeof(topic), PSTR("homeassistant/sensor/ems-esp/%s/config"), uniq.c_str());
|
||||||
|
|
||||||
// state topic
|
// state topic
|
||||||
char state_t[50];
|
char stat_t[50];
|
||||||
snprintf_P(state_t, sizeof(state_t), PSTR("%s/%s_data"), hostname_.c_str(), device_name.c_str());
|
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/%s_data"), hostname_.c_str(), device_name.c_str());
|
||||||
|
|
||||||
// state template
|
// state template
|
||||||
char tpl[50];
|
char tpl[50];
|
||||||
@@ -784,7 +786,7 @@ void Mqtt::register_mqtt_ha_sensor(const char * prefix,
|
|||||||
if (uom != nullptr) {
|
if (uom != nullptr) {
|
||||||
doc["unit_of_meas"] = uom;
|
doc["unit_of_meas"] = uom;
|
||||||
}
|
}
|
||||||
doc["stat_t"] = state_t;
|
doc["stat_t"] = stat_t;
|
||||||
doc["val_tpl"] = tpl;
|
doc["val_tpl"] = tpl;
|
||||||
if (icon != nullptr) {
|
if (icon != nullptr) {
|
||||||
doc["ic"] = icon;
|
doc["ic"] = icon;
|
||||||
|
|||||||
@@ -342,7 +342,11 @@ void Sensor::publish_values() {
|
|||||||
if (!(registered_ha_[i])) {
|
if (!(registered_ha_[i])) {
|
||||||
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> config;
|
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> config;
|
||||||
config["dev_cla"] = F("temperature");
|
config["dev_cla"] = F("temperature");
|
||||||
config["stat_t"] = F("ems-esp/sensor_data");
|
|
||||||
|
char stat_t[50];
|
||||||
|
snprintf_P(stat_t, sizeof(stat_t), PSTR("%s/sensor_data"), System::hostname().c_str());
|
||||||
|
config["stat_t"] = stat_t;
|
||||||
|
|
||||||
config["unit_of_meas"] = F("°C");
|
config["unit_of_meas"] = F("°C");
|
||||||
|
|
||||||
char str[50];
|
char str[50];
|
||||||
|
|||||||
Reference in New Issue
Block a user