Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,6 @@

LIGHT_PROFILES_FILE = "light_profiles.csv"

PROP_TO_ATTR = {
'brightness': ATTR_BRIGHTNESS,
'color_temp': ATTR_COLOR_TEMP,
'min_mireds': ATTR_MIN_MIREDS,
'max_mireds': ATTR_MAX_MIREDS,
'hs_color': ATTR_HS_COLOR,
'white_value': ATTR_WHITE_VALUE,
'effect_list': ATTR_EFFECT_LIST,
'effect': ATTR_EFFECT,
}

# Service call validation schemas
VALID_TRANSITION = vol.All(vol.Coerce(float), vol.Clamp(min=0, max=6553))
VALID_BRIGHTNESS = vol.All(vol.Coerce(int), vol.Clamp(min=0, max=255))
Expand Down Expand Up @@ -494,29 +483,37 @@ def effect(self):
def state_attributes(self):
"""Return optional state attributes."""
data = {}
supported_features = self.supported_features

if self.supported_features & SUPPORT_COLOR_TEMP:
if supported_features & SUPPORT_COLOR_TEMP:
data[ATTR_MIN_MIREDS] = self.min_mireds
data[ATTR_MAX_MIREDS] = self.max_mireds

if self.is_on:
for prop, attr in PROP_TO_ATTR.items():
value = getattr(self, prop)
if value is not None:
data[attr] = value

# Expose current color also as RGB and XY
if ATTR_HS_COLOR in data:
data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(
*data[ATTR_HS_COLOR])
data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(
*data[ATTR_HS_COLOR])
if supported_features & SUPPORT_BRIGHTNESS:
data[ATTR_BRIGHTNESS] = self.brightness

if supported_features & SUPPORT_COLOR_TEMP:
data[ATTR_COLOR_TEMP] = self.color_temp

if self.supported_features & SUPPORT_COLOR and self.hs_color:
# pylint: disable=unsubscriptable-object,not-an-iterable
hs_color = self.hs_color
data[ATTR_HS_COLOR] = (
round(data[ATTR_HS_COLOR][0], 3),
round(data[ATTR_HS_COLOR][1], 3),
round(hs_color[0], 3),
round(hs_color[1], 3),
)
data[ATTR_RGB_COLOR] = color_util.color_hs_to_RGB(*hs_color)
data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(*hs_color)

if supported_features & SUPPORT_WHITE_VALUE:
data[ATTR_WHITE_VALUE] = self.white_value

if supported_features & SUPPORT_EFFECT:
data[ATTR_EFFECT_LIST] = self.effect_list
data[ATTR_EFFECT] = self.effect

return data
return {key: val for key, val in data.items() if val is not None}

@property
def supported_features(self):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/light/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def __init__(self, name, effect_list, topic, templates, qos,
topic[CONF_COLOR_TEMP_COMMAND_TOPIC] is not None and
SUPPORT_COLOR_TEMP)
self._supported_features |= (
topic[CONF_EFFECT_STATE_TOPIC] is not None and
topic[CONF_EFFECT_COMMAND_TOPIC] is not None and
SUPPORT_EFFECT)
self._supported_features |= (
topic[CONF_WHITE_VALUE_COMMAND_TOPIC] is not None and
Expand Down
19 changes: 11 additions & 8 deletions tests/components/light/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,24 @@ async def test_effect_list(hass):
}})

hass.states.async_set('light.test1', 'on',
{'effect_list': ['None', 'Random', 'Colorloop']})
{'effect_list': ['None', 'Random', 'Colorloop'],
'supported_features': 4})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert set(state.attributes['effect_list']) == {
'None', 'Random', 'Colorloop'}

hass.states.async_set('light.test2', 'on',
{'effect_list': ['None', 'Random', 'Rainbow']})
{'effect_list': ['None', 'Random', 'Rainbow'],
'supported_features': 4})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert set(state.attributes['effect_list']) == {
'None', 'Random', 'Colorloop', 'Rainbow'}

hass.states.async_set('light.test1', 'off',
{'effect_list': ['None', 'Colorloop', 'Seven']})
{'effect_list': ['None', 'Colorloop', 'Seven'],
'supported_features': 4})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert set(state.attributes['effect_list']) == {
Expand All @@ -229,27 +232,27 @@ async def test_effect(hass):
}})

hass.states.async_set('light.test1', 'on',
{'effect': 'None', 'supported_features': 2})
{'effect': 'None', 'supported_features': 6})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert state.attributes['effect'] == 'None'

hass.states.async_set('light.test2', 'on',
{'effect': 'None', 'supported_features': 2})
{'effect': 'None', 'supported_features': 6})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert state.attributes['effect'] == 'None'

hass.states.async_set('light.test3', 'on',
{'effect': 'Random', 'supported_features': 2})
{'effect': 'Random', 'supported_features': 6})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert state.attributes['effect'] == 'None'

hass.states.async_set('light.test1', 'off',
{'effect': 'None', 'supported_features': 2})
{'effect': 'None', 'supported_features': 6})
hass.states.async_set('light.test2', 'off',
{'effect': 'None', 'supported_features': 2})
{'effect': 'None', 'supported_features': 6})
await hass.async_block_till_done()
state = hass.states.get('light.light_group')
assert state.attributes['effect'] == 'Random'
Expand Down
7 changes: 7 additions & 0 deletions tests/components/light/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ def test_controlling_state_via_topic_with_templates(self):
'name': 'test',
'state_topic': 'test_light_rgb/status',
'command_topic': 'test_light_rgb/set',
'brightness_command_topic': 'test_light_rgb/brightness/set',
'rgb_command_topic': 'test_light_rgb/rgb/set',
'color_temp_command_topic': 'test_light_rgb/color_temp/set',
'effect_command_topic': 'test_light_rgb/effect/set',
'white_value_command_topic': 'test_light_rgb/white_value/set',
'xy_command_topic': 'test_light_rgb/xy/set',
'brightness_state_topic': 'test_light_rgb/brightness/status',
'color_temp_state_topic': 'test_light_rgb/color_temp/status',
'effect_state_topic': 'test_light_rgb/effect/status',
Expand Down Expand Up @@ -475,6 +481,7 @@ def test_sending_mqtt_commands_and_optimistic(self):
'effect_command_topic': 'test_light_rgb/effect/set',
'white_value_command_topic': 'test_light_rgb/white_value/set',
'xy_command_topic': 'test_light_rgb/xy/set',
'effect_list': ['colorloop', 'random'],
'qos': 2,
'payload_on': 'on',
'payload_off': 'off'
Expand Down
2 changes: 2 additions & 0 deletions tests/components/light/test_mqtt_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ def test_optimistic(self):
'{{ green|d }}-'
'{{ blue|d }}',
'command_off_template': 'off',
'effect_list': ['colorloop', 'random'],
'effect_command_topic': 'test_light_rgb/effect/set',
'qos': 2
}
})
Expand Down