Skip to content
24 changes: 21 additions & 3 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,31 @@ def async_handle_light_service(service):
if color_name is not None:
params[ATTR_RGB_COLOR] = color_util.color_name_to_rgb(color_name)

xy_color = params.get(ATTR_XY_COLOR, None)
brightness = params.get(ATTR_BRIGHTNESS, None)

# Convert xy_color/brightness for lights that do not support xy
params_rgb = params.copy()
if xy_color is not None and brightness is not None:
rgb = color_util.color_xy_brightness_to_RGB(
*xy_color,
ibrightness=brightness)
params_rgb.setdefault(ATTR_RGB_COLOR, rgb)

for light in target_lights:
support_xy = bool(light.supported_features & SUPPORT_XY_COLOR)
support_rgb = bool(light.supported_features & SUPPORT_RGB_COLOR)
if not support_xy and support_rgb:
lightparams = params_rgb
else:
lightparams = params

if service.service == SERVICE_TURN_ON:
yield from light.async_turn_on(**params)
yield from light.async_turn_on(**lightparams)
elif service.service == SERVICE_TURN_OFF:
yield from light.async_turn_off(**params)
yield from light.async_turn_off(**lightparams)
else:
yield from light.async_toggle(**params)
yield from light.async_toggle(**lightparams)

update_tasks = []

Expand Down
16 changes: 2 additions & 14 deletions homeassistant/components/light/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_EFFECT,
ATTR_RGB_COLOR, ATTR_WHITE_VALUE, ATTR_XY_COLOR, SUPPORT_BRIGHTNESS,
ATTR_RGB_COLOR, ATTR_WHITE_VALUE, SUPPORT_BRIGHTNESS,
SUPPORT_COLOR_TEMP, SUPPORT_EFFECT, SUPPORT_RGB_COLOR, SUPPORT_WHITE_VALUE,
Light)

Expand Down Expand Up @@ -42,15 +42,14 @@ class DemoLight(Light):
"""Representation of a demo light."""

def __init__(self, name, state, available=False, rgb=None, ct=None,
brightness=180, xy_color=(.5, .5), white=200,
brightness=180, white=200,
effect_list=None, effect=None):
"""Initialize the light."""
self._name = name
self._state = state
self._rgb = rgb
self._ct = ct or random.choice(LIGHT_TEMPS)
self._brightness = brightness
self._xy_color = xy_color
self._white = white
self._effect_list = effect_list
self._effect = effect
Expand All @@ -77,11 +76,6 @@ def brightness(self) -> int:
"""Return the brightness of this light between 0..255."""
return self._brightness

@property
def xy_color(self) -> tuple:
"""Return the XY color value [float, float]."""
return self._xy_color

@property
def rgb_color(self) -> tuple:
"""Return the RBG color value."""
Expand Down Expand Up @@ -130,9 +124,6 @@ def turn_on(self, **kwargs) -> None:
if ATTR_BRIGHTNESS in kwargs:
self._brightness = kwargs[ATTR_BRIGHTNESS]

if ATTR_XY_COLOR in kwargs:
self._xy_color = kwargs[ATTR_XY_COLOR]

if ATTR_WHITE_VALUE in kwargs:
self._white = kwargs[ATTR_WHITE_VALUE]

Expand Down Expand Up @@ -165,9 +156,6 @@ def async_restore_state(self, is_on, **kwargs):
if 'rgb_color' in kwargs:
self._rgb = kwargs['rgb_color']

if 'xy_color' in kwargs:
self._xy_color = kwargs['xy_color']

if 'white_value' in kwargs:
self._white = kwargs['white_value']

Expand Down
16 changes: 6 additions & 10 deletions homeassistant/components/light/hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,11 @@ def is_on(self):
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_HUE.get(self.info.get('type'), SUPPORT_HUE_EXTENDED)
features = SUPPORT_HUE.get(self.info.get('type'), SUPPORT_HUE_EXTENDED)
if self.info.get('manufacturername') == "OSRAM":
return features & ~SUPPORT_XY_COLOR
else:
return features

@property
def effect_list(self):
Expand All @@ -379,15 +383,7 @@ def turn_on(self, **kwargs):
command['transitiontime'] = int(kwargs[ATTR_TRANSITION] * 10)

if ATTR_XY_COLOR in kwargs:
if self.info.get('manufacturername') == "OSRAM":
hsv = color_util.color_xy_brightness_to_hsv(
*kwargs[ATTR_XY_COLOR],
ibrightness=self.info['bri'])
command['hue'] = hsv[0]
command['sat'] = hsv[1]
command['bri'] = hsv[2]
else:
command['xy'] = kwargs[ATTR_XY_COLOR]
command['xy'] = kwargs[ATTR_XY_COLOR]
elif ATTR_RGB_COLOR in kwargs:
if self.info.get('manufacturername') == "OSRAM":
hsv = color_util.color_RGB_to_hsv(
Expand Down
3 changes: 0 additions & 3 deletions tests/components/light/test_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def test_state_attributes(self):
self.hass.block_till_done()
state = self.hass.states.get(ENTITY_LIGHT)
self.assertTrue(light.is_on(self.hass, ENTITY_LIGHT))
self.assertEqual((.4, .6), state.attributes.get(light.ATTR_XY_COLOR))
self.assertEqual(25, state.attributes.get(light.ATTR_BRIGHTNESS))
self.assertEqual(
(76, 95, 0), state.attributes.get(light.ATTR_RGB_COLOR))
Expand Down Expand Up @@ -75,7 +74,6 @@ def test_restore_state(hass):
'brightness': 'value-brightness',
'color_temp': 'value-color_temp',
'rgb_color': 'value-rgb_color',
'xy_color': 'value-xy_color',
'white_value': 'value-white_value',
'effect': 'value-effect',
}),
Expand All @@ -93,6 +91,5 @@ def test_restore_state(hass):
assert state.attributes.get('brightness') == 'value-brightness'
assert state.attributes.get('color_temp') == 'value-color_temp'
assert state.attributes.get('rgb_color') == 'value-rgb_color'
assert state.attributes.get('xy_color') == 'value-xy_color'
assert state.attributes.get('white_value') == 'value-white_value'
assert state.attributes.get('effect') == 'value-effect'
21 changes: 18 additions & 3 deletions tests/testing_config/custom_components/light/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@
Call init before using it in your tests to ensure clean test data.
"""
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.components.light import (SUPPORT_XY_COLOR)
from tests.common import MockToggleDevice


DEVICES = []


class MockLightDevice(MockToggleDevice):
"""Provide a mock light device."""

@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_XY_COLOR

@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return None


def init(empty=False):
"""Initalize the platform with devices."""
global DEVICES

DEVICES = [] if empty else [
MockToggleDevice('Ceiling', STATE_ON),
MockToggleDevice('Ceiling', STATE_OFF),
MockToggleDevice(None, STATE_OFF)
MockLightDevice('Ceiling', STATE_ON),
MockLightDevice('Ceiling', STATE_OFF),
MockLightDevice(None, STATE_OFF)
]


Expand Down