Skip to content

Commit f1be1ab

Browse files
committed
fix discovery prefix
1 parent 30df4a4 commit f1be1ab

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

MqttManager/MqttDiscoveryComponent.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#include "MqttDiscoveryComponent.h"
22

3-
MqttDiscoveryComponent::MqttDiscoveryComponent(String component, String name)
3+
4+
MqttDiscoveryComponent::MqttDiscoveryComponent(String component, String name, String discovery_prefix)
45
{
56
this->component = component;
67
this->entity_id = name;
8+
this->discovery_prefix = discovery_prefix;
79

810
this->entity_id.replace(" ", "_");
911

1012
this->setConfigurtionVariable("name", name);
11-
this->setConfigurtionVariable("state_topic", discovery_prefix + "/" + component + "/" + entity_id + "/" + state_suffix);
13+
this->setConfigurtionVariable("state_topic", this->discovery_prefix + "/" + this->component + "/" + this->entity_id + "/" + this->state_suffix);
1214

1315
if (component == "light" || component == "switch")
1416
{
15-
this->setConfigurtionVariable("command_topic", discovery_prefix + "/" + component + "/" + entity_id + "/" + command_suffix);
17+
this->setConfigurtionVariable("command_topic", this->discovery_prefix + "/" + this->component + "/" + this->entity_id + "/" + this->command_suffix);
1618
}
1719
else
1820
{

MqttManager/MqttDiscoveryComponent.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef MQTTDISCOVERYCOMPONENT_H
22
#define MQTTDISCOVERYCOMPONENT_H
33

4-
#include <string>
54
#include <map>
65

76
#include "ArduinoJson.h"
@@ -13,10 +12,10 @@ class MqttDiscoveryComponent
1312
std::map<String, String> m_fields;
1413

1514
public:
16-
MqttDiscoveryComponent(String component, String name);
15+
MqttDiscoveryComponent(String component, String name, String discovery_prefix="homeassistant");
1716
void setConfigurtionVariable(String configKey, String configValue);
1817

19-
String discovery_prefix{"homeassistant"};
18+
String discovery_prefix;
2019
String component;
2120
String entity_id;
2221
String discovery_suffix{"config"};

MqttManager/MqttManager.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,23 @@ void MqttManager::setDeviceData(String deviceName, String hardware, String devic
6262

6363
if (m_mqttDiscoveryEnabled)
6464
{
65-
m_deviceStatusSensor = new MqttDiscoveryComponent("binary_sensor", m_deviceName + " Status");
66-
m_deviceStatusSensor->discovery_prefix = discovery_prefix;
65+
m_deviceStatusSensor = new MqttDiscoveryComponent("binary_sensor", m_deviceName + " Status", discovery_prefix);
6766
m_deviceStatusSensor->setConfigurtionVariable("device_class", "connectivity");
6867
m_discoveryComponents.push_back(m_deviceStatusSensor);
6968

70-
m_deviceIpSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " IP");
71-
m_deviceIpSensor->discovery_prefix = discovery_prefix;
69+
m_deviceIpSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " IP", discovery_prefix);
7270
m_discoveryComponents.push_back(m_deviceIpSensor);
7371

74-
m_deviceMacSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " MAC");
75-
m_deviceMacSensor->discovery_prefix = discovery_prefix;
72+
m_deviceMacSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " MAC", discovery_prefix);
7673
m_discoveryComponents.push_back(m_deviceMacSensor);
7774

78-
m_deviceHardwareSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Hardware");
79-
m_deviceHardwareSensor->discovery_prefix = discovery_prefix;
75+
m_deviceHardwareSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Hardware", discovery_prefix);
8076
m_discoveryComponents.push_back(m_deviceHardwareSensor);
8177

82-
m_deviceFirmwareSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Firmware");
83-
m_deviceFirmwareSensor->discovery_prefix = discovery_prefix;
78+
m_deviceFirmwareSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Firmware", discovery_prefix);
8479
m_discoveryComponents.push_back(m_deviceFirmwareSensor);
8580

86-
m_deviceFirmwareVersionSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Firmware Version");
87-
m_deviceFirmwareVersionSensor->discovery_prefix = discovery_prefix;
81+
m_deviceFirmwareVersionSensor = new MqttDiscoveryComponent("sensor", m_deviceName + " Firmware Version", discovery_prefix);
8882
m_discoveryComponents.push_back(m_deviceFirmwareVersionSensor);
8983

9084
this->setLastWillMQTT(m_deviceStatusSensor->getStateTopic(), m_lastWillPayload);

0 commit comments

Comments
 (0)