From db4b8b43c9463305bc0f7fc30c262093c62746e1 Mon Sep 17 00:00:00 2001 From: "rami.zayat9" Date: Mon, 21 Apr 2025 18:19:53 +0200 Subject: [PATCH 1/2] added support to include the UM pir mqtt device to associate with hass wled integration device by adding mac adresse & ip to discovery information --- usermods/PIR_sensor_switch/PIR_sensor_switch.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp b/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp index 6f09ce5be0..df7ca2e952 100644 --- a/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp +++ b/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp @@ -71,7 +71,7 @@ class PIRsensorSwitch : public Usermod bool m_offOnly = false; bool m_offMode = offMode; bool m_override = false; - + // Home Assistant bool HomeAssistantDiscovery = false; // is HA discovery turned on int16_t idx = -1; // Domoticz virtual switch idx @@ -317,7 +317,15 @@ void PIRsensorSwitch::publishHomeAssistantAutodiscovery() device[F("mf")] = F(WLED_BRAND); device[F("mdl")] = F(WLED_PRODUCT_NAME); device[F("sw")] = versionString; - + + String mqtt_avail_topic = mqttDeviceTopic; + mqtt_avail_topic += F("/status"); + doc[F("avty_t")] = mqtt_avail_topic.c_str(); + JsonArray connections = device[F("connections")].createNestedArray(); + connections.add(F("mac")); + connections.add(WiFi.macAddress()); + connections.add(F("ip")); + connections.add(WiFi.localIP().toString()); sprintf_P(buf, PSTR("homeassistant/binary_sensor/%s/config"), uid); DEBUG_PRINTLN(buf); size_t payload_size = serializeJson(doc, json_str); From 11719b0df04cba632771b05834a29bbde1b5e310 Mon Sep 17 00:00:00 2001 From: "rami.zayat9" Date: Wed, 23 Apr 2025 19:37:47 +0200 Subject: [PATCH 2/2] added support for hass discovery fix for attaching mqtt device to WLED hass integration via Mac adress matching for all hass-um --- usermods/Battery/Battery.cpp | 6 ++++++ usermods/PIR_sensor_switch/PIR_sensor_switch.cpp | 9 ++++----- usermods/multi_relay/multi_relay.cpp | 13 +++++++++++++ usermods/sht/sht.cpp | 9 ++++++++- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/usermods/Battery/Battery.cpp b/usermods/Battery/Battery.cpp index 5572f55024..244954520f 100644 --- a/usermods/Battery/Battery.cpp +++ b/usermods/Battery/Battery.cpp @@ -156,6 +156,12 @@ class UsermodBattery : public Usermod device[F("mf")] = F(WLED_BRAND); device[F("mdl")] = F(WLED_PRODUCT_NAME); device[F("sw")] = versionString; + // add device connection MAC and IP address ( for hass discivery rules to work ) + JsonArray connections = device[F("connections")].createNestedArray(); // array of connections defined by hass discovery rule + connections.add(F("mac")); + connections.add(WiFi.macAddress()); // MAC address of the device to match WLED hass integration MAC : this is reponsible for the device ID in Home Assistant to merge with the WLED device ID + connections.add(F("ip")); + connections.add(WiFi.localIP().toString()); // IP address of the device to match WLED hass integration sprintf_P(buf, PSTR("homeassistant/%s/%s/%s/config"), type, mqttClientID, uid); DEBUG_PRINTLN(buf); diff --git a/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp b/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp index df7ca2e952..2e74abfb2a 100644 --- a/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp +++ b/usermods/PIR_sensor_switch/PIR_sensor_switch.cpp @@ -71,7 +71,6 @@ class PIRsensorSwitch : public Usermod bool m_offOnly = false; bool m_offMode = offMode; bool m_override = false; - // Home Assistant bool HomeAssistantDiscovery = false; // is HA discovery turned on int16_t idx = -1; // Domoticz virtual switch idx @@ -320,12 +319,12 @@ void PIRsensorSwitch::publishHomeAssistantAutodiscovery() String mqtt_avail_topic = mqttDeviceTopic; mqtt_avail_topic += F("/status"); - doc[F("avty_t")] = mqtt_avail_topic.c_str(); - JsonArray connections = device[F("connections")].createNestedArray(); + doc[F("avty_t")] = mqtt_avail_topic.c_str(); // availability topic to indicate sensor device is online + JsonArray connections = device[F("connections")].createNestedArray(); // array of connections defined by hass discovery rule connections.add(F("mac")); - connections.add(WiFi.macAddress()); + connections.add(WiFi.macAddress()); // MAC address of the device to match WLED hass integration MAC : this is reponsible for the device ID in Home Assistant to merge with the WLED device ID connections.add(F("ip")); - connections.add(WiFi.localIP().toString()); + connections.add(WiFi.localIP().toString()); // IP address of the device to match WLED hass integration sprintf_P(buf, PSTR("homeassistant/binary_sensor/%s/config"), uid); DEBUG_PRINTLN(buf); size_t payload_size = serializeJson(doc, json_str); diff --git a/usermods/multi_relay/multi_relay.cpp b/usermods/multi_relay/multi_relay.cpp index f8b1f566a5..736ac86f4e 100644 --- a/usermods/multi_relay/multi_relay.cpp +++ b/usermods/multi_relay/multi_relay.cpp @@ -485,6 +485,19 @@ void MultiRelay::publishHomeAssistantAutodiscovery() { json[F("avty_t")] = buf; json[F("pl_avail")] = F("online"); json[F("pl_not_avail")] = F("offline"); + // add device properties, connection MAC and IP address ( for hass discivery rules to work ) + JsonObject device = json.createNestedObject(F("device")); + device[F("name")] = serverDescription; + device[F("ids")] = String(F("wled-")) + mqttClientID; + device[F("mf")] = F(WLED_BRAND); + device[F("mdl")] = F(WLED_PRODUCT_NAME); + device[F("sw")] = versionString; + JsonArray connections = device[F("connections")].createNestedArray(); // array of connections defined by hass discovery rule + connections.add(F("mac")); + connections.add(WiFi.macAddress()); // MAC address of the device to match WLED hass integration MAC : this is reponsible for the device ID in Home Assistant to merge with the WLED device ID + connections.add(F("ip")); + connections.add(WiFi.localIP().toString()); // IP address of the device to match WLED hass integration + //TODO: dev payload_size = serializeJson(json, json_str); } else { diff --git a/usermods/sht/sht.cpp b/usermods/sht/sht.cpp index e1eb9e885f..393f7189e0 100644 --- a/usermods/sht/sht.cpp +++ b/usermods/sht/sht.cpp @@ -143,8 +143,15 @@ void ShtUsermod::appendDeviceToMqttDiscoveryMessage(JsonDocument& root) { device[F("ids")] = escapedMac.c_str(); device[F("name")] = serverDescription; device[F("sw")] = versionString; - device[F("mdl")] = ESP.getChipModel(); + // device[F("mdl")] = ESP.getChipModel(); // getChipModel() is not supported ! compile error device[F("mf")] = F("espressif"); + device[F("mdl")] = F(WLED_PRODUCT_NAME); + device[F("mf")] = F(WLED_BRAND); + JsonArray connections = device[F("connections")].createNestedArray(); // array of connections defined by hass discovery rule + connections.add(F("mac")); + connections.add(WiFi.macAddress()); // MAC address of the device to match WLED hass integration MAC : this is reponsible for the device ID in Home Assistant to merge with the WLED device ID + connections.add(F("ip")); + connections.add(WiFi.localIP().toString()); // IP address of the device to match WLED hass integration } /**