Allow Domoticz unsubsctibe using command `DzIdx0 0` (#21019)

This commit is contained in:
Theo Arends 2024-04-01 13:56:40 +02:00
parent 781491efca
commit 1ebdbfd6a4

View File

@ -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();
}