Skip to content

Commit cabc869

Browse files
authored
Add rf2 HASS autodiscovery (#791)
* Add Home Assistant auto discovery feature Add HA auto discovery to RF2. For each unique combination of unit, groupbit and address a switch will be added to Home Assistant called: "RF2_[unit]_[groupbit])_[address]. As RF2 433Mhz switches do not render their state, no state topic will be provided in the discovery. This will cause the switch to be in optimistic mode in HA with separate on and off icons. The two separate on/off icons allow for subsequent on commands to support the dimming feature of KAKU switches like ACM-300. Co-authored-by: hbraam <hbraam@users.noreply.github.com>
1 parent e7cecf6 commit cabc869

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

main/ZgatewayRF2.ino

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,50 @@ void setupRF2() {
7171
digitalWrite(RF_EMITTER_GPIO, LOW);
7272
}
7373

74+
# ifdef ZmqttDiscovery
75+
//Register for autodiscover in Home Assistant
76+
void RF2toMQTTdiscovery(JsonObject& data) {
77+
Log.trace(F("switchRF2Discovery" CR));
78+
String payloadonstr;
79+
String payloadoffstr;
80+
81+
int org_switchtype = data["switchType"]; // Store original switchvalue
82+
data.set("switchType", 1); // switchtype = 1 turns switch on.
83+
data.printTo(payloadonstr);
84+
data.set("switchType", 0); // switchtype = 0 turns switch off.
85+
data.printTo(payloadoffstr);
86+
data.set("switchType", org_switchtype); // Restore original switchvalue
87+
88+
String switchname;
89+
switchname = "RF2_" + String((int)data["unit"]) + "_" +
90+
String((int)data["groupbit"]) + "_" +
91+
String((unsigned long)data["address"]);
92+
93+
char* switchRF[8] = {"switch",
94+
(char*)switchname.c_str(),
95+
"",
96+
"",
97+
"",
98+
(char*)payloadonstr.c_str(),
99+
(char*)payloadoffstr.c_str(),
100+
""};
101+
// component type,name,availability topic,device class,value template,payload
102+
// on, payload off, unit of measurement
103+
104+
Log.trace(F("CreateDiscoverySwitch: %s" CR), switchRF[1]);
105+
106+
// As RF2 433Mhz switches do not render their state, no state topic should be
107+
// provided in the discovery. This will cause the switch to be in optimistic
108+
// mode in HA with separate on and off icons.
109+
// The two separate on/off icons allow for subsequent on commands to support
110+
// the dimming feature of KAKU switches like ACM-300.
111+
createDiscovery(switchRF[0], "", switchRF[1],
112+
(char*)getUniqueId(switchRF[1], "").c_str(), will_Topic,
113+
switchRF[3], switchRF[4], switchRF[5], switchRF[6],
114+
switchRF[7], 0, "", "", true, subjectMQTTtoRF2);
115+
}
116+
# endif
117+
74118
void RF2toMQTT() {
75119
if (rf2rd.hasNewData) {
76120
Log.trace(F("Creating RF2 buffer" CR));
@@ -86,6 +130,9 @@ void RF2toMQTT() {
86130
RF2data.set("period", (int)rf2rd.period);
87131
RF2data.set("address", (unsigned long)rf2rd.address);
88132
RF2data.set("switchType", (int)rf2rd.switchType);
133+
# ifdef ZmqttDiscovery //component creation for HA
134+
RF2toMQTTdiscovery(RF2data);
135+
# endif
89136

90137
pub(subjectRF2toMQTT, RF2data);
91138
}

main/ZmqttDiscovery.ino

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,18 @@ void createDiscovery(char* sensor_type,
7272
StaticJsonBuffer<JSON_MSG_CALC_BUFFER> jsonBuffer;
7373
JsonObject& sensor = jsonBuffer.createObject();
7474

75-
char state_topic[mqtt_topic_max_size];
76-
strcpy(state_topic, mqtt_topic);
77-
strcat(state_topic, st_topic);
78-
sensor.set("stat_t", state_topic); //state_topic
75+
// If a component cannot render it's state (f.i. KAKU relays) no state topic
76+
// should be added. Without a state topic HA will use optimistic mode for the
77+
// component by default. The Home Assistant UI for optimistic switches
78+
// (separate on and off icons) allows for multiple
79+
// subsequent on commands. This is required for dimming on KAKU relays like
80+
// the ACM-300.
81+
if (st_topic[0]) {
82+
char state_topic[mqtt_topic_max_size];
83+
strcpy(state_topic, mqtt_topic);
84+
strcat(state_topic, st_topic);
85+
sensor.set("stat_t", state_topic); // state_topic
86+
}
7987

8088
sensor.set("name", s_name); //name
8189
sensor.set("uniq_id", unique_id); //unique_id

0 commit comments

Comments
 (0)