From 1ebdbfd6a4ce388df2e3f4bb6ac5c5b4d67b0e98 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 1 Apr 2024 13:56:40 +0200 Subject: [PATCH] Allow Domoticz unsubsctibe using command ``DzIdx0 0`` (#21019) --- .../tasmota_xdrv_driver/xdrv_07_domoticz.ino | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_07_domoticz.ino b/tasmota/tasmota_xdrv_driver/xdrv_07_domoticz.ino index 0037aa15d..fc0360b1a 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_07_domoticz.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_07_domoticz.ino @@ -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 @@ -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 @@ -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 } @@ -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(); }