Skip to content

Commit

Permalink
Hue: Use the currently active color mode (home-assistant#13376)
Browse files Browse the repository at this point in the history
* Hue: Use the currently active color mode

* Round hue/sat colors before reporting to API

* .gitignore cache fix
  • Loading branch information
emlove authored and balloob committed Mar 23, 2018
1 parent 5539207 commit 2497dd5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Icon

# pytest
.pytest_cache
.cache

# GITHUB Proposed Python stuff:
*.py[cod]
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,10 @@ def state_attributes(self):
*data[ATTR_HS_COLOR])
data[ATTR_XY_COLOR] = color_util.color_hs_to_xy(
*data[ATTR_HS_COLOR])
data[ATTR_HS_COLOR] = (
round(data[ATTR_HS_COLOR][0], 3),
round(data[ATTR_HS_COLOR][1], 3),
)

return data

Expand Down
15 changes: 15 additions & 0 deletions homeassistant/components/light/hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,20 @@ def brightness(self):
return self.light.action.get('bri')
return self.light.state.get('bri')

@property
def _color_mode(self):
"""Return the hue color mode."""
if self.is_group:
return self.light.action.get('colormode')
return self.light.state.get('colormode')

@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":
return None

if self.is_group:
return (
self.light.action.get('hue') / 65535 * 360,
Expand All @@ -241,6 +252,10 @@ def hs_color(self):
@property
def color_temp(self):
"""Return the CT color value."""
# Don't return color temperature unless in color temperature mode
if self._color_mode != "ct":
return None

if self.is_group:
return self.light.action.get('ct')
return self.light.state.get('ct')
Expand Down
37 changes: 36 additions & 1 deletion tests/components/light/test_hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,48 @@ async def test_lights(hass, mock_bridge):
assert lamp_1 is not None
assert lamp_1.state == 'on'
assert lamp_1.attributes['brightness'] == 144
assert lamp_1.attributes['color_temp'] == 467
assert lamp_1.attributes['hs_color'] == (71.896, 83.137)

lamp_2 = hass.states.get('light.hue_lamp_2')
assert lamp_2 is not None
assert lamp_2.state == 'off'


async def test_lights_color_mode(hass, mock_bridge):
"""Test that lights only report appropriate color mode."""
mock_bridge.mock_light_responses.append(LIGHT_RESPONSE)
await setup_bridge(hass, mock_bridge)

lamp_1 = hass.states.get('light.hue_lamp_1')
assert lamp_1 is not None
assert lamp_1.state == 'on'
assert lamp_1.attributes['brightness'] == 144
assert lamp_1.attributes['hs_color'] == (71.896, 83.137)
assert 'color_temp' not in lamp_1.attributes

new_light1_on = LIGHT_1_ON.copy()
new_light1_on['state'] = new_light1_on['state'].copy()
new_light1_on['state']['colormode'] = 'ct'
mock_bridge.mock_light_responses.append({
"1": new_light1_on,
})
mock_bridge.mock_group_responses.append({})

# Calling a service will trigger the updates to run
await hass.services.async_call('light', 'turn_on', {
'entity_id': 'light.hue_lamp_2'
}, blocking=True)
# 2x light update, 1 turn on request
assert len(mock_bridge.mock_requests) == 3

lamp_1 = hass.states.get('light.hue_lamp_1')
assert lamp_1 is not None
assert lamp_1.state == 'on'
assert lamp_1.attributes['brightness'] == 144
assert lamp_1.attributes['color_temp'] == 467
assert 'hs_color' not in lamp_1.attributes


async def test_groups(hass, mock_bridge):
"""Test the update_lights function with some lights."""
mock_bridge.allow_groups = True
Expand Down

0 comments on commit 2497dd5

Please sign in to comment.