Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hue: Use the currently active color mode #13376

Merged
merged 3 commits into from
Mar 23, 2018
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're using an old version of pytest. Since 3.4.0 pytest will create the cache directory in .pytest_cache. See #13308 :)

(I think we can still leave it there for users with old pytest versions)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah, I was already noticing that dir and was like: where did it come from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good call! In any case I'm sure ignoring .cache doesn't hurt.


# 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