From 1887f11ec932738b215440d9c17f586c4247e314 Mon Sep 17 00:00:00 2001 From: Daniel de Jong <64607512+daniel-jong@users.noreply.github.com> Date: Thu, 17 Sep 2020 22:48:52 +0200 Subject: [PATCH] Do not default Pilight lights to max brightness (#39549) Fix pilight lights would always turned on at max brightness instead of just turning on. Some 433mhz dimmers (like the KAKU series) remember their last brightness setting. Fix pilight lights would not respect configured dimlevel_min --- homeassistant/components/pilight/light.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/pilight/light.py b/homeassistant/components/pilight/light.py index 12d175817d7..11eeb02293e 100644 --- a/homeassistant/components/pilight/light.py +++ b/homeassistant/components/pilight/light.py @@ -61,7 +61,20 @@ class PilightLight(PilightBaseDevice, LightEntity): def turn_on(self, **kwargs): """Turn the switch on by calling pilight.send service with on code.""" - self._brightness = kwargs.get(ATTR_BRIGHTNESS, 255) - dimlevel = int(self._brightness / (255 / self._dimlevel_max)) + # Update brightness only if provided as an argument. + # This will allow the switch to keep its previous brightness level. + dimlevel = None + + if ATTR_BRIGHTNESS in kwargs: + self._brightness = kwargs[ATTR_BRIGHTNESS] + + # Calculate pilight brightness (as a range of 0 to 15) + # By creating a percentage + percentage = self._brightness / 255 + # Then calculate the dimmer range (aka amount of available brightness steps). + dimrange = self._dimlevel_max - self._dimlevel_min + # Finally calculate the pilight brightness. + # We add dimlevel_min back in to ensure the minimum is always reached. + dimlevel = int(percentage * dimrange + self._dimlevel_min) self.set_state(turn_on=True, dimlevel=dimlevel)