From c5c848e758ce7f0d30d7a821f4c308fef0753662 Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Mon, 5 Feb 2018 23:01:49 +0100 Subject: [PATCH 1/2] Mapping ([1,100] <-> [1,255]) of the brightness improved. --- homeassistant/components/light/xiaomi_miio.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/light/xiaomi_miio.py b/homeassistant/components/light/xiaomi_miio.py index afaafe6c971e19..f918173da7c85e 100644 --- a/homeassistant/components/light/xiaomi_miio.py +++ b/homeassistant/components/light/xiaomi_miio.py @@ -7,6 +7,7 @@ import asyncio from functools import partial import logging +from math import ceil import voluptuous as vol @@ -204,11 +205,11 @@ def async_turn_on(self, **kwargs): """Turn the light on.""" if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] - percent_brightness = int(100 * brightness / 255) + percent_brightness = int(ceil(100 * brightness / 255.0)) _LOGGER.debug( "Setting brightness: %s %s%%", - self.brightness, percent_brightness) + brightness, percent_brightness) result = yield from self._try_command( "Setting brightness failed: %s", @@ -235,7 +236,7 @@ def async_update(self): _LOGGER.debug("Got new state: %s", state) self._state = state.is_on - self._brightness = int(255 * 0.01 * state.brightness) + self._brightness = int(ceil((255/100.0) * state.brightness)) except DeviceException as ex: _LOGGER.error("Got exception while fetching the state: %s", ex) @@ -306,11 +307,11 @@ def async_turn_on(self, **kwargs): if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] - percent_brightness = int(100 * brightness / 255) + percent_brightness = int(ceil(100 * brightness / 255.0)) _LOGGER.debug( "Setting brightness: %s %s%%", - self.brightness, percent_brightness) + brightness, percent_brightness) result = yield from self._try_command( "Setting brightness failed: %s", @@ -331,7 +332,7 @@ def async_update(self): _LOGGER.debug("Got new state: %s", state) self._state = state.is_on - self._brightness = int(255 * 0.01 * state.brightness) + self._brightness = int(ceil((255/100.0) * state.brightness)) self._color_temp = self.translate( state.color_temperature, CCT_MIN, CCT_MAX, From 5c6c49b96ea4b2e60730e83b4224e1d6109bc70e Mon Sep 17 00:00:00 2001 From: Sebastian Muszynski Date: Tue, 6 Feb 2018 08:55:42 +0100 Subject: [PATCH 2/2] The cast to int isn't needed for python3. --- homeassistant/components/light/xiaomi_miio.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/light/xiaomi_miio.py b/homeassistant/components/light/xiaomi_miio.py index f918173da7c85e..a3c5fa9f62ea21 100644 --- a/homeassistant/components/light/xiaomi_miio.py +++ b/homeassistant/components/light/xiaomi_miio.py @@ -205,7 +205,7 @@ def async_turn_on(self, **kwargs): """Turn the light on.""" if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] - percent_brightness = int(ceil(100 * brightness / 255.0)) + percent_brightness = ceil(100 * brightness / 255.0) _LOGGER.debug( "Setting brightness: %s %s%%", @@ -236,7 +236,7 @@ def async_update(self): _LOGGER.debug("Got new state: %s", state) self._state = state.is_on - self._brightness = int(ceil((255/100.0) * state.brightness)) + self._brightness = ceil((255/100.0) * state.brightness) except DeviceException as ex: _LOGGER.error("Got exception while fetching the state: %s", ex) @@ -307,7 +307,7 @@ def async_turn_on(self, **kwargs): if ATTR_BRIGHTNESS in kwargs: brightness = kwargs[ATTR_BRIGHTNESS] - percent_brightness = int(ceil(100 * brightness / 255.0)) + percent_brightness = ceil(100 * brightness / 255.0) _LOGGER.debug( "Setting brightness: %s %s%%", @@ -332,7 +332,7 @@ def async_update(self): _LOGGER.debug("Got new state: %s", state) self._state = state.is_on - self._brightness = int(ceil((255/100.0) * state.brightness)) + self._brightness = ceil((255/100.0) * state.brightness) self._color_temp = self.translate( state.color_temperature, CCT_MIN, CCT_MAX,