Skip to content

Commit

Permalink
Allow Domoticz unsubsctibe using command DzIdx0 0 (#21019)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendst committed Apr 1, 2024
1 parent 781491e commit 1ebdbfd
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions tasmota/tasmota_xdrv_driver/xdrv_07_domoticz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ uint32_t DomoticzRelayIdx(uint32_t relay) {
return (relay < MAX_DOMOTICZ_IDX) ? Settings->domoticz_relay_idx[relay] : Domoticz->relay_idx[relay -MAX_DOMOTICZ_IDX];
}

void DomoticzSetRelayIdx(uint32_t relay, uint32_t idx) {
if (relay < MAX_DOMOTICZ_IDX) {
Settings->domoticz_relay_idx[relay] = idx;
} else {
Domoticz->relay_idx[relay -MAX_DOMOTICZ_IDX] = idx;
}
}

#ifdef USE_SONOFF_IFAN
void MqttPublishDomoticzFanState(void) {
if (Settings->flag.mqtt_enabled && DomoticzRelayIdx(1)) { // SetOption3 - Enable MQTT
Expand Down Expand Up @@ -220,14 +228,13 @@ void DomoticzMqttSubscribe(void) {
}
}

int32_t DomoticzIdx2Relay(uint32_t idx) {
if (0 == idx) {
return -1; // Idx not mine
}
uint32_t maxdev = (TasmotaGlobal.devices_present > MAX_RELAYS_SET) ? MAX_RELAYS_SET : TasmotaGlobal.devices_present;
for (uint32_t i = 0; i < maxdev; i++) {
if (idx == DomoticzRelayIdx(i)) {
return i;
int DomoticzIdx2Relay(uint32_t idx) {
if (idx > 0) {
uint32_t maxdev = (TasmotaGlobal.devices_present > MAX_RELAYS_SET) ? MAX_RELAYS_SET : TasmotaGlobal.devices_present;
for (uint32_t i = 0; i < maxdev; i++) {
if (idx == DomoticzRelayIdx(i)) {
return i;
}
}
}
return -1; // Idx not mine
Expand All @@ -242,6 +249,11 @@ bool DomoticzMqttData(void) {
*/
Domoticz->update_flag = true;

if (!Domoticz->subscribe) {
return false; // No Domoticz driver subscription so try user subscribes
}

// Default subscibed to domoticz/out/#
if (strncasecmp_P(XdrvMailbox.topic, PSTR(DOMOTICZ_OUT_TOPIC), strlen(DOMOTICZ_OUT_TOPIC)) != 0) {
return false; // Process unchanged data
}
Expand Down Expand Up @@ -520,14 +532,17 @@ void DomoticzInit(void) {
\*********************************************************************************************/

void CmndDomoticzIdx(void) {
// DzIdx0 0 - Reset all disabling subscription too
// DzIdx1 403 - Relate relay1 (=Power1) to Domoticz Idx 403 persistent
// DzIdx5 403 - Relate relay5 (=Power5) to Domoticz Idx 403 non-persistent (need a rule at boot to become persistent)
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= MAX_RELAYS_SET)) {
if ((XdrvMailbox.index >= 0) && (XdrvMailbox.index <= MAX_RELAYS_SET)) {
if (XdrvMailbox.payload >= 0) {
if (XdrvMailbox.index <= MAX_DOMOTICZ_IDX) {
Settings->domoticz_relay_idx[XdrvMailbox.index -1] = XdrvMailbox.payload;
if (0 == XdrvMailbox.index) {
for (uint32_t i = 0; i < MAX_RELAYS_SET; i++) {
DomoticzSetRelayIdx(i, 0);
}
} else {
Domoticz->relay_idx[XdrvMailbox.index -MAX_DOMOTICZ_IDX -1] = XdrvMailbox.payload;
DomoticzSetRelayIdx(XdrvMailbox.index -1, XdrvMailbox.payload);
}
DomoticzMqttSubscribe();
}
Expand Down

0 comments on commit 1ebdbfd

Please sign in to comment.