From 64c8719f7096c43a5d5804a6a18d53fe0ec10b46 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 15 Jul 2025 17:39:07 +0200 Subject: [PATCH] Re-add domoticz erase settings on request --- .../xdrv_07_ufs_domoticz.ino | 47 ++++++++++++++----- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_07_ufs_domoticz.ino b/tasmota/tasmota_xdrv_driver/xdrv_07_ufs_domoticz.ino index 4593f4060..f6b17c79b 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_07_ufs_domoticz.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_07_ufs_domoticz.ino @@ -194,7 +194,21 @@ void DomoticzDeleteData(void) { void DomoticzSettingsLoad(bool erase) { // Called from FUNC_PRE_INIT (erase = 0) once at restart // Called from FUNC_RESET_SETTINGS (erase = 1) after command reset 4, 5, or 6 -// memset(&Domoticz->Settings, 0x00, sizeof(DzSettings_t)); + +// memset(&Domoticz->Settings, 0x00, sizeof(DzSettings_t)); // Won't work as we need to keep our pointers + Domoticz->Settings.update_timer = 0; + for (uint32_t i = 0; i < TasmotaGlobal.devices_present; i++) { + Domoticz->Settings.relay_idx[i] = 0; + if (i < Domoticz->keys) { + Domoticz->Settings.key_idx[i] = 0; + } + if (i < Domoticz->switches) { + Domoticz->Settings.switch_idx[i] = 0; + } + } + for (uint32_t i = 0; i < DZ_MAX_SENSORS; i++) { + Domoticz->Settings.sensor_idx[i] = 0; + } #ifndef CONFIG_IDF_TARGET_ESP32P4 // Init any other parameter in struct DzSettings @@ -299,7 +313,10 @@ void MqttPublishDomoticzFanState(void) { int fan_speed = GetFanspeed(); snprintf_P(svalue, sizeof(svalue), PSTR("%d"), fan_speed * 10); - Response_P(DOMOTICZ_MESSAGE, (int)DomoticzRelayIdx(1), (0 == fan_speed) ? 0 : 2, svalue, DomoticzBatteryQuality(), DomoticzRssiQuality()); + Response_P(DOMOTICZ_MESSAGE, (int)DomoticzRelayIdx(1), + (0 == fan_speed) ? 0 : 2, + svalue, + DomoticzBatteryQuality(), DomoticzRssiQuality()); MqttPublish(domoticz_in_topic); Domoticz->fan_debounce = millis() + 1000; // 1 second @@ -336,7 +353,10 @@ void MqttPublishDomoticzPowerState(uint8_t device) { char svalue[8]; // Dimmer value snprintf_P(svalue, sizeof(svalue), PSTR("%d"), Settings->light_dimmer); - Response_P(DOMOTICZ_MESSAGE, (int)DomoticzRelayIdx(device -1), (TasmotaGlobal.power & (1 << (device -1))) ? 1 : 0, (TasmotaGlobal.light_type) ? svalue : "", DomoticzBatteryQuality(), DomoticzRssiQuality()); + Response_P(DOMOTICZ_MESSAGE, (int)DomoticzRelayIdx(device -1), + (TasmotaGlobal.power & (1 << (device -1))) ? 1 : 0, + (TasmotaGlobal.light_type) ? svalue : "", + DomoticzBatteryQuality(), DomoticzRssiQuality()); MqttPublish(domoticz_in_topic); #ifdef USE_SONOFF_IFAN } @@ -475,7 +495,8 @@ bool DomoticzMqttData(void) { AddLog(LOG_LEVEL_DEBUG_MORE, PSTR(D_LOG_DOMOTICZ "%s, idx %d, nvalue %d"), XdrvMailbox.topic, DomoticzRelayIdx(relay_index), nvalue); bool iscolordimmer = (strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Color Switch")) == 0); - bool isShutter = (strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Light/Switch")) == 0) && (strncmp_P(domoticz.getStr(PSTR("switchType")),PSTR("Blinds"), 6) == 0); + bool isShutter = (strcmp_P(domoticz.getStr(PSTR("dtype")), PSTR("Light/Switch")) == 0) && + (strncmp_P(domoticz.getStr(PSTR("switchType")), PSTR("Blinds"), 6) == 0); #ifdef USE_SONOFF_IFAN if (IsModuleIfan() && (1 == relay_index)) { // Idx 2 is fanspeed @@ -563,7 +584,9 @@ bool DomoticzMqttData(void) { void DomoticzSendSwitch(uint32_t type, uint32_t index, uint32_t state) { char stemp[16]; // "switchlight" or "switchscene" Response_P(PSTR("{\"command\":\"%s\",\"idx\":%d,\"switchcmd\":\"%s\"}"), - GetTextIndexed(stemp, sizeof(stemp), type, kDomoticzCommand), index, (state) ? (POWER_TOGGLE == state) ? "Toggle" : "On" : "Off"); // Domoticz case sensitive + GetTextIndexed(stemp, sizeof(stemp), type, kDomoticzCommand), + index, + (state) ? (POWER_TOGGLE == state) ? "Toggle" : "On" : "Off"); // Domoticz case sensitive MqttPublish(domoticz_in_topic); } @@ -706,17 +729,17 @@ void DomoticzInit(void) { Domoticz = (Domoticz_t*)calloc(1, sizeof(Domoticz_t)); // Need calloc to reset registers to 0/false if (nullptr == Domoticz) { return; } - Domoticz->Settings.relay_idx = (uintdz_t*)calloc(TasmotaGlobal.devices_present, sizeof(uintdz_t)); // Need calloc to reset registers to 0/false + Domoticz->Settings.relay_idx = (uintdz_t*)calloc(TasmotaGlobal.devices_present, sizeof(uintdz_t)); for (uint32_t i = 0; i < TasmotaGlobal.devices_present; i++) { if (ButtonUsed(i)) { Domoticz->keys++; } if (SwitchUsed(i)) { Domoticz->switches++; } } if (Domoticz->keys) { - Domoticz->Settings.key_idx = (uintdz_t*)calloc(Domoticz->keys, sizeof(uintdz_t)); // Need calloc to reset registers to 0/false + Domoticz->Settings.key_idx = (uintdz_t*)calloc(Domoticz->keys, sizeof(uintdz_t)); if (nullptr == Domoticz->Settings.key_idx) { return; } } if (Domoticz->switches) { - Domoticz->Settings.switch_idx = (uintdz_t*)calloc(Domoticz->switches, sizeof(uintdz_t)); // Need calloc to reset registers to 0/false + Domoticz->Settings.switch_idx = (uintdz_t*)calloc(Domoticz->switches, sizeof(uintdz_t)); if (nullptr == Domoticz->Settings.switch_idx) { return; } } @@ -841,7 +864,8 @@ const char HTTP_FORM_DOMOTICZ_INPUT[] PROGMEM = const char HTTP_FORM_DOMOTICZ_SENSOR[] PROGMEM = "