Correct HA discovery topic & minor adjutments.
Publish on MQTT connect.
This commit is contained in:
parent
535f285287
commit
e88d34ea19
@ -164,32 +164,37 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create an MQTT Binary Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
|
// Create an MQTT Binary Sensor for Home Assistant Discovery purposes, this includes a pointer to the topic that is published to in the Loop.
|
||||||
void _createMqttBinarySensor(const String &name, const String &topic, const String &deviceClass)
|
void publishHomeAssistantAutodiscovery()
|
||||||
{
|
{
|
||||||
StaticJsonDocument<600> doc;
|
if (WLED_MQTT_CONNECTED) {
|
||||||
|
StaticJsonDocument<600> doc;
|
||||||
|
char uid[24], json_str[1024], buf[128];
|
||||||
|
|
||||||
doc[F("name")] = String(serverDescription) + " " + name;
|
sprintf_P(buf, PSTR("%s Motion"), serverDescription); //max length: 33 + 7 = 40
|
||||||
doc[F("state_topic")] = topic;
|
doc[F("name")] = buf;
|
||||||
doc[F("payload_on")] = "on";
|
sprintf_P(buf, PSTR("%s/motion"), mqttDeviceTopic); //max length: 33 + 7 = 40
|
||||||
doc[F("payload_off")] = "off";
|
doc[F("stat_t")] = buf;
|
||||||
doc[F("unique_id")] = String(mqttClientID) + name;
|
doc[F("pl_on")] = "on";
|
||||||
if (deviceClass != "")
|
doc[F("pl_off")] = "off";
|
||||||
doc[F("device_class")] = deviceClass;
|
sprintf_P(uid, PSTR("%s_motion"), escapedMac.c_str());
|
||||||
doc[F("expire_after")] = 1800;
|
doc[F("uniq_id")] = uid;
|
||||||
|
doc[F("dev_cla")] = F("motion");
|
||||||
|
doc[F("exp_aft")] = 1800;
|
||||||
|
|
||||||
JsonObject device = doc.createNestedObject(F("device")); // attach the sensor to the same device
|
JsonObject device = doc.createNestedObject(F("device")); // attach the sensor to the same device
|
||||||
device[F("name")] = serverDescription;
|
device[F("name")] = serverDescription;
|
||||||
device[F("identifiers")] = String(F("wled-sensor-")) + mqttClientID;
|
device[F("ids")] = String(F("wled-sensor-")) + mqttClientID;
|
||||||
device[F("manufacturer")] = "WLED";
|
device[F("mf")] = "WLED";
|
||||||
device[F("model")] = F("FOSS");
|
device[F("mdl")] = F("FOSS");
|
||||||
device[F("sw_version")] = versionString;
|
device[F("sw")] = versionString;
|
||||||
|
|
||||||
String temp;
|
sprintf_P(buf, PSTR("homeassistant/binary_sensor/%s/config"), uid);
|
||||||
serializeJson(doc, temp);
|
DEBUG_PRINTLN(buf);
|
||||||
DEBUG_PRINTLN(topic);
|
size_t payload_size = serializeJson(doc, json_str);
|
||||||
DEBUG_PRINTLN(temp);
|
DEBUG_PRINTLN(json_str);
|
||||||
|
|
||||||
mqtt->publish(topic.c_str(), 0, true, temp.c_str()); // do we really need to retain?
|
mqtt->publish(buf, 0, true, json_str, payload_size); // do we really need to retain?
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -266,10 +271,14 @@ public:
|
|||||||
*/
|
*/
|
||||||
void connected()
|
void connected()
|
||||||
{
|
{
|
||||||
if (WLED_MQTT_CONNECTED) {
|
}
|
||||||
if (HomeAssistantDiscovery) {
|
|
||||||
_createMqttBinarySensor(String(F("Motion")), mqttDeviceTopic + String(F("/motion")), F("motion"));
|
/**
|
||||||
}
|
* onMqttConnect() is called when MQTT connection is established
|
||||||
|
*/
|
||||||
|
void onMqttConnect(bool sessionPresent) {
|
||||||
|
if (HomeAssistantDiscovery) {
|
||||||
|
publishHomeAssistantAutodiscovery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user