Re-add domoticz erase settings on request

This commit is contained in:
Theo Arends 2025-07-15 17:39:07 +02:00
parent c0f11f0373
commit 64c8719f70

View File

@ -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 =
"<tr><td colspan='3'><b>" D_DOMOTICZ_SENSOR_IDX " %d</b> %s</td><td>";
const char HTTP_FORM_DOMOTICZ_TIMER[] PROGMEM =
"<tr><td colspan='3'><b>" D_DOMOTICZ_UPDATE_TIMER "</b> (" STR(DOMOTICZ_UPDATE_TIMER) ")</td><td><input id='ut' placeholder='" STR(DOMOTICZ_UPDATE_TIMER) "' value='%d'></td></tr>";
"<tr><td colspan='3'><b>" D_DOMOTICZ_UPDATE_TIMER "</b> (" STR(DOMOTICZ_UPDATE_TIMER) ")</td>"
"<td><input id='ut' placeholder='" STR(DOMOTICZ_UPDATE_TIMER) "' value='%d'></td></tr>";
void HandleDomoticzConfiguration(void) {
if (!HttpCheckPriviledgedAccess()) { return; }
@ -854,8 +878,6 @@ void HandleDomoticzConfiguration(void) {
return;
}
char stemp[40];
WSContentStart_P(PSTR(D_CONFIGURE_DOMOTICZ));
WSContentSendStyle();
WSContentSend_P(HTTP_FORM_DOMOTICZ, (Domoticz->switches)? D_DOMOTICZ_SWITCH :"", (Domoticz->keys)? D_DOMOTICZ_KEY :"");
@ -871,12 +893,11 @@ void HandleDomoticzConfiguration(void) {
WSContentSend_P(PSTR("</td><td>"));
WSContentSend_P(HTTP_FORM_DOMOTICZ_INPUT, 'r', i, Domoticz->Settings.relay_idx[i]);
WSContentSend_P(PSTR("</td></tr>"));
#ifdef USE_SONOFF_IFAN
if (IsModuleIfan() && (1 == i)) { break; }
#endif // USE_SONOFF_IFAN
}
char stemp[40];
for (uint32_t i = 0; i < DZ_MAX_SENSORS; i++) {
WSContentSend_P(HTTP_FORM_DOMOTICZ_SENSOR, i +1, GetTextIndexed(stemp, sizeof(stemp), i, kDomoticzSensors));
WSContentSend_P(HTTP_FORM_DOMOTICZ_INPUT, 'l', i, Domoticz->Settings.sensor_idx[i]);