Hue: Convert XY to HS color if HS not present (#13465)

* Hue: Convert XY to HS color if HS not present

* Revert change to test

* Address comments

* Lint
This commit is contained in:
Paulus Schoutsen 2018-03-26 14:00:56 -07:00
parent a507ed0af8
commit dfe3219f3f
2 changed files with 76 additions and 11 deletions

View File

@ -18,6 +18,7 @@ from homeassistant.components.light import (
FLASH_LONG, FLASH_SHORT, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, FLASH_LONG, FLASH_SHORT, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP,
SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_COLOR, SUPPORT_TRANSITION, SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_COLOR, SUPPORT_TRANSITION,
Light) Light)
from homeassistant.util import color
DEPENDENCIES = ['hue'] DEPENDENCIES = ['hue']
SCAN_INTERVAL = timedelta(seconds=5) SCAN_INTERVAL = timedelta(seconds=5)
@ -235,19 +236,26 @@ class HueLight(Light):
@property @property
def hs_color(self): def hs_color(self):
"""Return the hs color value.""" """Return the hs color value."""
# Don't return hue/sat if in color temperature mode # pylint: disable=redefined-outer-name
if self._color_mode == "ct": mode = self._color_mode
if mode not in ('hs', 'xy'):
return
source = self.light.action if self.is_group else self.light.state
hue = source.get('hue')
sat = source.get('sat')
# Sometimes the state will not include valid hue/sat values.
# Reported as issue 13434
if hue is not None and sat is not None:
return hue / 65535 * 360, sat / 255 * 100
if 'xy' not in source:
return None return None
if self.is_group: return color.color_xy_to_hs(*source['xy'])
return (
self.light.action.get('hue') / 65535 * 360,
self.light.action.get('sat') / 255 * 100,
)
return (
self.light.state.get('hue') / 65535 * 360,
self.light.state.get('sat') / 255 * 100,
)
@property @property
def color_temp(self): def color_temp(self):

View File

@ -11,6 +11,7 @@ import pytest
from homeassistant.components import hue from homeassistant.components import hue
import homeassistant.components.light.hue as hue_light import homeassistant.components.light.hue as hue_light
from homeassistant.util import color
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -623,3 +624,59 @@ def test_available():
) )
assert light.available is True assert light.available is True
def test_hs_color():
"""Test hs_color property."""
light = hue_light.HueLight(
light=Mock(state={
'colormode': 'ct',
'hue': 1234,
'sat': 123,
}),
request_bridge_update=None,
bridge=Mock(),
is_group=False,
)
assert light.hs_color is None
light = hue_light.HueLight(
light=Mock(state={
'colormode': 'hs',
'hue': 1234,
'sat': 123,
}),
request_bridge_update=None,
bridge=Mock(),
is_group=False,
)
assert light.hs_color == (1234 / 65535 * 360, 123 / 255 * 100)
light = hue_light.HueLight(
light=Mock(state={
'colormode': 'xy',
'hue': 1234,
'sat': 123,
}),
request_bridge_update=None,
bridge=Mock(),
is_group=False,
)
assert light.hs_color == (1234 / 65535 * 360, 123 / 255 * 100)
light = hue_light.HueLight(
light=Mock(state={
'colormode': 'xy',
'hue': None,
'sat': 123,
'xy': [0.4, 0.5]
}),
request_bridge_update=None,
bridge=Mock(),
is_group=False,
)
assert light.hs_color == color.color_xy_to_hs(0.4, 0.5)