From 55a44b0a1c394db8c5359133783d31d32cf3dab9 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Wed, 23 Aug 2017 07:19:33 +0200 Subject: [PATCH] Yeelight fix updates on hsv mode (#9093) * cast strings to integers for hsv_to_rgb conversion, fixes #6473 * remove type_checking, flake8 does not like that. * use hsv_to_rgb to convert to correct rgb value --- homeassistant/components/light/yeelight.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/light/yeelight.py b/homeassistant/components/light/yeelight.py index e286bf330a1..1f7ee2ba5f9 100644 --- a/homeassistant/components/light/yeelight.py +++ b/homeassistant/components/light/yeelight.py @@ -6,6 +6,7 @@ https://home-assistant.io/components/light.yeelight/ """ import logging import colorsys +from typing import Tuple import voluptuous as vol @@ -89,6 +90,14 @@ YEELIGHT_EFFECT_LIST = [ EFFECT_STOP] +# Travis-CI runs too old astroid https://github.com/PyCQA/pylint/issues/1212 +# pylint: disable=invalid-sequence-index +def hsv_to_rgb(hsv: Tuple[float, float, float]) -> Tuple[int, int, int]: + """Convert HSV tuple (degrees, %, %) to RGB (values 0-255).""" + red, green, blue = colorsys.hsv_to_rgb(hsv[0]/360, hsv[1]/100, hsv[2]/100) + return int(red * 255), int(green * 255), int(blue * 255) + + def _cmd(func): """Define a wrapper to catch exceptions from the bulb.""" def _wrap(self, *args, **kwargs): @@ -192,10 +201,10 @@ class YeelightLight(Light): if color_mode == 2: # color temperature return color_temperature_to_rgb(self.color_temp) if color_mode == 3: # hsv - hue = self._properties.get('hue') - sat = self._properties.get('sat') - val = self._properties.get('bright') - return colorsys.hsv_to_rgb(hue, sat, val) + hue = int(self._properties.get('hue')) + sat = int(self._properties.get('sat')) + val = int(self._properties.get('bright')) + return hsv_to_rgb((hue, sat, val)) rgb = int(rgb) blue = rgb & 0xff @@ -214,7 +223,7 @@ class YeelightLight(Light): return self._bulb.last_properties @property - def _bulb(self) -> object: + def _bulb(self) -> 'yeelight.Bulb': import yeelight if self._bulb_device is None: try: