From e7b061ce5a688d48aba3d1736d48973d4dadd5e8 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 11 Jan 2020 15:39:56 +0100 Subject: [PATCH] Fix Tuya PowerOn state Fix Tuya PowerOn state (#7412) --- tasmota/support_tasmota.ino | 54 +++++++++++++++++++++++++++++++++++++ tasmota/tasmota.ino | 49 +-------------------------------- tasmota/xdrv_16_tuyamcu.ino | 2 +- 3 files changed, 56 insertions(+), 49 deletions(-) diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index a4ce37d36..d502e64c3 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -276,6 +276,60 @@ void SetAllPower(uint32_t state, uint32_t source) } } +void SetPowerOnState(void) +{ + if (MOTOR == my_module_type) { + Settings.poweronstate = POWER_ALL_ON; // Needs always on else in limbo! + } + if (POWER_ALL_ALWAYS_ON == Settings.poweronstate) { + SetDevicePower(1, SRC_RESTART); + } else { + if ((ResetReason() == REASON_DEFAULT_RST) || (ResetReason() == REASON_EXT_SYS_RST)) { + switch (Settings.poweronstate) { + case POWER_ALL_OFF: + case POWER_ALL_OFF_PULSETIME_ON: + power = 0; + SetDevicePower(power, SRC_RESTART); + break; + case POWER_ALL_ON: // All on + power = (1 << devices_present) -1; + SetDevicePower(power, SRC_RESTART); + break; + case POWER_ALL_SAVED_TOGGLE: + power = (Settings.power & ((1 << devices_present) -1)) ^ POWER_MASK; + if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart + SetDevicePower(power, SRC_RESTART); + } + break; + case POWER_ALL_SAVED: + power = Settings.power & ((1 << devices_present) -1); + if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart + SetDevicePower(power, SRC_RESTART); + } + break; + } + } else { + power = Settings.power & ((1 << devices_present) -1); + if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart + SetDevicePower(power, SRC_RESTART); + } + } + } + + // Issue #526 and #909 + for (uint32_t i = 0; i < devices_present; i++) { + if (!Settings.flag3.no_power_feedback) { // SetOption63 - Don't scan relay power state at restart - #5594 and #5663 + if ((i < MAX_RELAYS) && (pin[GPIO_REL1 +i] < 99)) { + bitWrite(power, i, digitalRead(pin[GPIO_REL1 +i]) ^ bitRead(rel_inverted, i)); + } + } + if ((i < MAX_PULSETIMERS) && (bitRead(power, i) || (POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate))) { + SetPulseTimer(i, Settings.pulse_timer[i]); + } + } + blink_powersave = power; +} + void SetLedPowerIdx(uint32_t led, uint32_t state) { if ((99 == pin[GPIO_LEDLNK]) && (0 == led)) { // Legacy - LED1 is link led only if LED2 is present diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index fe90b1a33..97f2ff797 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -276,54 +276,7 @@ void setup(void) WifiConnect(); - if (MOTOR == my_module_type) { Settings.poweronstate = POWER_ALL_ON; } // Needs always on else in limbo! - if (POWER_ALL_ALWAYS_ON == Settings.poweronstate) { - SetDevicePower(1, SRC_RESTART); - } else { - if ((ResetReason() == REASON_DEFAULT_RST) || (ResetReason() == REASON_EXT_SYS_RST)) { - switch (Settings.poweronstate) { - case POWER_ALL_OFF: - case POWER_ALL_OFF_PULSETIME_ON: - power = 0; - SetDevicePower(power, SRC_RESTART); - break; - case POWER_ALL_ON: // All on - power = (1 << devices_present) -1; - SetDevicePower(power, SRC_RESTART); - break; - case POWER_ALL_SAVED_TOGGLE: - power = (Settings.power & ((1 << devices_present) -1)) ^ POWER_MASK; - if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart - SetDevicePower(power, SRC_RESTART); - } - break; - case POWER_ALL_SAVED: - power = Settings.power & ((1 << devices_present) -1); - if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart - SetDevicePower(power, SRC_RESTART); - } - break; - } - } else { - power = Settings.power & ((1 << devices_present) -1); - if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart - SetDevicePower(power, SRC_RESTART); - } - } - } - - // Issue #526 and #909 - for (uint32_t i = 0; i < devices_present; i++) { - if (!Settings.flag3.no_power_feedback) { // SetOption63 - Don't scan relay power state at restart - #5594 and #5663 - if ((i < MAX_RELAYS) && (pin[GPIO_REL1 +i] < 99)) { - bitWrite(power, i, digitalRead(pin[GPIO_REL1 +i]) ^ bitRead(rel_inverted, i)); - } - } - if ((i < MAX_PULSETIMERS) && (bitRead(power, i) || (POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate))) { - SetPulseTimer(i, Settings.pulse_timer[i]); - } - } - blink_powersave = power; + SetPowerOnState(); AddLog_P2(LOG_LEVEL_INFO, PSTR(D_PROJECT " %s %s " D_VERSION " %s%s-" ARDUINO_ESP8266_RELEASE), PROJECT, SettingsText(SET_FRIENDLYNAME1), my_version, my_image); #ifdef FIRMWARE_MINIMAL diff --git a/tasmota/xdrv_16_tuyamcu.ino b/tasmota/xdrv_16_tuyamcu.ino index a5f1df326..18a899589 100644 --- a/tasmota/xdrv_16_tuyamcu.ino +++ b/tasmota/xdrv_16_tuyamcu.ino @@ -794,7 +794,7 @@ bool Xdrv16(uint8_t function) case FUNC_MODULE_INIT: result = TuyaModuleSelected(); break; - case FUNC_INIT: + case FUNC_PRE_INIT: TuyaInit(); break; case FUNC_SET_DEVICE_POWER: