From f08e7ff5df96f0e7c796aab01baf444201ae75e8 Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Wed, 14 Aug 2019 13:53:46 +0100 Subject: [PATCH] tuya-dimmer: Fix dimmer skipping power/dimmer commands This tuya dimmer mcu sends data for multiple dimmer ids which breaks `tuya_ignore_dim` logic and sometimes when `power on` is sent its blocked due to multiple serial commands being sent at the same time. This patch makes sure we send dimmer commands only when we need to. Bug is explained in https://github.com/arendst/Sonoff-Tasmota/issues/6215#issuecomment-521191828 --- sonoff/xdrv_16_tuyadimmer.ino | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/sonoff/xdrv_16_tuyadimmer.ino b/sonoff/xdrv_16_tuyadimmer.ino index 362d85a3a..db63d6cec 100644 --- a/sonoff/xdrv_16_tuyadimmer.ino +++ b/sonoff/xdrv_16_tuyadimmer.ino @@ -149,8 +149,10 @@ void LightSerialDuty(uint8_t duty) if(Settings.flag3.tuya_dimmer_range_255 == 0) { duty = changeUIntScale(duty, 0, 255, 0, 100); } - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim value=%d (id=%d)"), duty, Settings.param[P_TUYA_DIMMER_ID]); - TuyaSendValue(Settings.param[P_TUYA_DIMMER_ID], duty); + if (tuya_new_dim != duty) { + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Send dim value=%d (id=%d)"), duty, Settings.param[P_TUYA_DIMMER_ID]); + TuyaSendValue(Settings.param[P_TUYA_DIMMER_ID], duty); + } } } else { tuya_ignore_dim = false; // reset flag @@ -215,16 +217,18 @@ void TuyaPacketProcess(void) AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TYA: Autoconfiguring Dimmer ID %d"), tuya_buffer[6]); Settings.param[P_TUYA_DIMMER_ID] = tuya_buffer[6]; } - if(Settings.flag3.tuya_dimmer_range_255 == 0) { - tuya_new_dim = (uint8_t) tuya_buffer[13]; - } else { - tuya_new_dim = changeUIntScale((uint8_t) tuya_buffer[13], 0, 255, 0, 100); - } - if ((power || Settings.flag3.tuya_apply_o20) && (tuya_new_dim > 0) && (abs(tuya_new_dim - Settings.light_dimmer) > 1)) { - tuya_ignore_dim = true; + if (Settings.param[P_TUYA_DIMMER_ID] == tuya_buffer[6]) { + if(Settings.flag3.tuya_dimmer_range_255 == 0) { + tuya_new_dim = (uint8_t) tuya_buffer[13]; + } else { + tuya_new_dim = changeUIntScale((uint8_t) tuya_buffer[13], 0, 255, 0, 100); + } + if ((power || Settings.flag3.tuya_apply_o20) && (tuya_new_dim > 0) && (abs(tuya_new_dim - Settings.light_dimmer) > 1)) { + tuya_ignore_dim = true; - snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), tuya_new_dim ); - ExecuteCommand(scmnd, SRC_SWITCH); + snprintf_P(scmnd, sizeof(scmnd), PSTR(D_CMND_DIMMER " %d"), tuya_new_dim ); + ExecuteCommand(scmnd, SRC_SWITCH); + } } } }