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)