From 1c7fd88ec46ec39b7400504dda3a41e47bc7c264 Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Thu, 22 Aug 2019 13:46:42 +0100 Subject: [PATCH 1/3] Tuya: Rename tuya_show_dimmer to tuya_disable_dimmer to make the option clear. By default the option is set to 0 in which case the tuya serial will act as dimmer When its set to 1 tuya serial will disable dimmer functions. --- sonoff/settings.h | 2 +- sonoff/xdrv_01_webserver.ino | 2 +- sonoff/xdrv_16_tuyadimmer.ino | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sonoff/settings.h b/sonoff/settings.h index 99591d6bf..9b627fdf7 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -78,7 +78,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu uint32_t no_hold_retain : 1; // bit 12 (v6.4.1.19) - SetOption62 - Don't use retain flag on HOLD messages uint32_t no_power_feedback : 1; // bit 13 (v6.5.0.9) - SetOption63 - Don't scan relay power state at restart uint32_t use_underscore : 1; // bit 14 (v6.5.0.12) - SetOption64 - Enable "_" instead of "-" as sensor index separator - uint32_t tuya_show_dimmer : 1; // bit 15 (v6.5.0.15) - SetOption65 - Enable or Disable Dimmer slider control + uint32_t tuya_disable_dimmer : 1; // bit 15 (v6.5.0.15) - SetOption65 - Enable or Disable Tuya Serial Dimmer control uint32_t tuya_dimmer_range_255 : 1; // bit 16 (v6.6.0.1) - SetOption66 - Enable or Disable Dimmer range 255 slider control uint32_t buzzer_enable : 1; // bit 17 (v6.6.0.1) - SetOption67 - Enable buzzer when available uint32_t pwm_multi_channels : 1; // bit 18 (v6.6.0.3) - SetOption68 - Enable multi-channels PWM instead of Color PWM diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 351e8dce9..695043b15 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -942,7 +942,7 @@ void HandleRoot(void) if ((LST_COLDWARM == (light_type &7)) || (LST_RGBWC == (light_type &7))) { WSContentSend_P(HTTP_MSG_SLIDER1, LightGetColorTemp()); } - if (!Settings.flag3.tuya_show_dimmer) { + if (!Settings.flag3.tuya_disable_dimmer) { WSContentSend_P(HTTP_MSG_SLIDER2, Settings.light_dimmer); } } diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index b3b2ca0ea..4abdcdeac 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -149,7 +149,7 @@ void LightSerialDuty(uint8_t duty) if (duty < 25) { duty = 25; } // dimming acts odd below 25(10%) - this mirrors the threshold set on the faceplate itself } - if (Settings.flag3.tuya_show_dimmer == 0) { + if (Settings.flag3.tuya_disable_dimmer == 0) { if(Settings.flag3.tuya_dimmer_range_255 == 0) { duty = changeUIntScale(duty, 0, 255, 0, 100); } @@ -160,7 +160,7 @@ void LightSerialDuty(uint8_t duty) } } else { Tuya.ignore_dim = false; // reset flag - if (Settings.flag3.tuya_show_dimmer == 0) { + if (Settings.flag3.tuya_disable_dimmer == 0) { if(Settings.flag3.tuya_dimmer_range_255 == 0) { duty = changeUIntScale(duty, 0, 255, 0, 100); } @@ -216,7 +216,7 @@ void TuyaPacketProcess(void) else if (Tuya.buffer[5] == 8) { // dim packet AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: RX Dim State=%d"), Tuya.buffer[13]); - if (Settings.flag3.tuya_show_dimmer == 0) { + if (Settings.flag3.tuya_disable_dimmer == 0) { if (!Settings.param[P_TUYA_DIMMER_ID]) { AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Autoconfiguring Dimmer ID %d"), Tuya.buffer[6]); Settings.param[P_TUYA_DIMMER_ID] = Tuya.buffer[6]; From 9fb804b426476699830838eab1f1a374c5fb087e Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Tue, 20 Aug 2019 11:13:37 +0100 Subject: [PATCH 2/3] Fix: Tuya Switches are detected as dimmers. Tuya switches are detected as dimmers even after setting SetOption65 to 1. Currently SetOption65 just hides the dimmer from Web UI for Tuya switches with SetOption65 to 1 but they are advertised as dimmer to HASS. With this change we set light_type to LT_BASIC (on/off) instead of LT_SERIAL1 (dimmable) when option 65 is set. --- sonoff/support_command.ino | 3 +++ sonoff/xdrv_01_webserver.ino | 4 +--- sonoff/xdrv_16_tuyadimmer.ino | 7 ++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sonoff/support_command.ino b/sonoff/support_command.ino index 29543cd4d..9853ac974 100644 --- a/sonoff/support_command.ino +++ b/sonoff/support_command.ino @@ -621,6 +621,9 @@ void CmndSetoption(void) if (18 == pindex) { // SetOption68 for multi-channel PWM, requires a reboot restart_flag = 2; } + if (15 == pindex) { // SetOption65 for tuya_disable_dimmer requires a reboot + restart_flag = 2; + } } } else { // SetOption32 .. 49 diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 695043b15..b7541910e 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -942,9 +942,7 @@ void HandleRoot(void) if ((LST_COLDWARM == (light_type &7)) || (LST_RGBWC == (light_type &7))) { WSContentSend_P(HTTP_MSG_SLIDER1, LightGetColorTemp()); } - if (!Settings.flag3.tuya_disable_dimmer) { - WSContentSend_P(HTTP_MSG_SLIDER2, Settings.light_dimmer); - } + WSContentSend_P(HTTP_MSG_SLIDER2, Settings.light_dimmer); } #endif WSContentSend_P(HTTP_TABLE100); diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 4abdcdeac..6a916bd0f 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -291,7 +291,12 @@ bool TuyaModuleSelected(void) Settings.my_gp.io[3] = GPIO_TUYA_RX; restart_flag = 2; } - light_type = LT_SERIAL1; + if (Settings.flag3.tuya_disable_dimmer == 0) { + light_type = LT_SERIAL1; + } else { + light_type = LT_BASIC; + } + return true; } From 92a6f6672caececd3385c908b32bd3283b46260a Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Wed, 21 Aug 2019 15:31:31 +0100 Subject: [PATCH 3/3] Tuya Fix: Dimmer doesn't switch on from HASS Tuya serial dimmer doesn't switch on from HASS because when powered off, HASS sends dimmer command. Internally, dimmer update and power command are sent too quickly to serial out and switch doesn't turn on. Adding a delay fixes things. --- sonoff/xdrv_16_tuyadimmer.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 6a916bd0f..a74e418f9 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -139,6 +139,7 @@ bool TuyaSetPower(void) bool TuyaSetChannels(void) { LightSerialDuty(((uint8_t*)XdrvMailbox.data)[0]); + delay(20); // Hack when power is off and dimmer is set then both commands go too soon to Serial out. return true; }