From a2a2a7f4c0bc791d0707917a24aa7a8f9a3d61d4 Mon Sep 17 00:00:00 2001 From: Lars Englund Date: Wed, 6 Oct 2021 16:05:12 +0200 Subject: [PATCH] Fixed problem with dimmer not turning off Fixes this issue https://github.com/JeffResc/Sonoff-D1-Dimmer/issues/9 by actually turning the light off when brightness reaches zero. Also removes some unnecessary serial writes by updating the lastBinary and lastBrightness variables. --- d1_dimmer_no_rf/d1_dimmer_no_rf.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/d1_dimmer_no_rf/d1_dimmer_no_rf.h b/d1_dimmer_no_rf/d1_dimmer_no_rf.h index c2928c7..9dcddad 100644 --- a/d1_dimmer_no_rf/d1_dimmer_no_rf.h +++ b/d1_dimmer_no_rf/d1_dimmer_no_rf.h @@ -62,6 +62,9 @@ class Sonoff_D1_Dimmer : public Component, public LightOutput // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 uint8_t buffer[17] = {0xAA, 0x55, 0x01, 0x04, 0x00, 0x0A, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00}; + lastBinary = binary; + lastBrightness = brightness; + buffer[6] = binary; buffer[7] = brightness; @@ -87,8 +90,13 @@ class Sonoff_D1_Dimmer : public Component, public LightOutput // Convert ESPHome's brightness (0-1) to the device's internal brightness (0-100) const int calculatedBrightness = round(brightness * 100); + + if (calculatedBrightness == 0) + { + binary = 0; + } ESP_LOGD("custom", "Interpreting brightness %f as %d", brightness, calculatedBrightness); control_dimmer(binary, calculatedBrightness); } -}; \ No newline at end of file +};