mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
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:
parent
a507ed0af8
commit
dfe3219f3f
@ -18,6 +18,7 @@ from homeassistant.components.light import (
|
||||
FLASH_LONG, FLASH_SHORT, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP,
|
||||
SUPPORT_EFFECT, SUPPORT_FLASH, SUPPORT_COLOR, SUPPORT_TRANSITION,
|
||||
Light)
|
||||
from homeassistant.util import color
|
||||
|
||||
DEPENDENCIES = ['hue']
|
||||
SCAN_INTERVAL = timedelta(seconds=5)
|
||||
@ -235,19 +236,26 @@ class HueLight(Light):
|
||||
@property
|
||||
def hs_color(self):
|
||||
"""Return the hs color value."""
|
||||
# Don't return hue/sat if in color temperature mode
|
||||
if self._color_mode == "ct":
|
||||
# pylint: disable=redefined-outer-name
|
||||
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
|
||||
|
||||
if self.is_group:
|
||||
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,
|
||||
)
|
||||
return color.color_xy_to_hs(*source['xy'])
|
||||
|
||||
@property
|
||||
def color_temp(self):
|
||||
|
@ -11,6 +11,7 @@ import pytest
|
||||
|
||||
from homeassistant.components import hue
|
||||
import homeassistant.components.light.hue as hue_light
|
||||
from homeassistant.util import color
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -623,3 +624,59 @@ def test_available():
|
||||
)
|
||||
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user