Fix Hue brightness values over 127 off by one (#34190)

This commit is contained in:
Paulus Schoutsen 2020-04-14 04:31:38 -07:00 committed by GitHub
parent 91d35c7c5c
commit 6fa29d6e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 9 deletions

View File

@ -184,6 +184,16 @@ def async_update_items(bridge, api, current, async_add_entities, create_item):
async_add_entities(new_items)
def hue_brightness_to_hass(value):
"""Convert hue brightness 1..254 to hass format 0..255."""
return min(255, round((value / 254) * 255))
def hass_to_hue_brightness(value):
"""Convert hass brightness 0..255 to hue 1..254 scale."""
return max(1, round((value / 255) * 254))
class HueLight(Light):
"""Representation of a Hue light."""
@ -245,8 +255,11 @@ class HueLight(Light):
def brightness(self):
"""Return the brightness of this light between 0..255."""
if self.is_group:
return self.light.action.get("bri")
return self.light.state.get("bri")
bri = self.light.action.get("bri")
else:
bri = self.light.state.get("bri")
return hue_brightness_to_hass(bri)
@property
def _color_mode(self):
@ -372,7 +385,7 @@ class HueLight(Light):
command["ct"] = max(self.min_mireds, min(temp, self.max_mireds))
if ATTR_BRIGHTNESS in kwargs:
command["bri"] = kwargs[ATTR_BRIGHTNESS]
command["bri"] = hass_to_hue_brightness(kwargs[ATTR_BRIGHTNESS])
flash = kwargs.get(ATTR_FLASH)

View File

@ -222,7 +222,7 @@ async def test_lights(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["brightness"] == 145
assert lamp_1.attributes["hs_color"] == (36.067, 69.804)
lamp_2 = hass.states.get("light.hue_lamp_2")
@ -238,7 +238,7 @@ async def test_lights_color_mode(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["brightness"] == 145
assert lamp_1.attributes["hs_color"] == (36.067, 69.804)
assert "color_temp" not in lamp_1.attributes
@ -258,7 +258,7 @@ async def test_lights_color_mode(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["brightness"] == 145
assert lamp_1.attributes["color_temp"] == 467
assert "hs_color" not in lamp_1.attributes
@ -277,7 +277,7 @@ async def test_groups(hass, mock_bridge):
lamp_1 = hass.states.get("light.group_1")
assert lamp_1 is not None
assert lamp_1.state == "on"
assert lamp_1.attributes["brightness"] == 254
assert lamp_1.attributes["brightness"] == 255
assert lamp_1.attributes["color_temp"] == 250
lamp_2 = hass.states.get("light.group_2")
@ -328,7 +328,7 @@ async def test_new_group_discovered(hass, mock_bridge):
new_group = hass.states.get("light.group_3")
assert new_group is not None
assert new_group.state == "on"
assert new_group.attributes["brightness"] == 153
assert new_group.attributes["brightness"] == 154
assert new_group.attributes["color_temp"] == 250
@ -448,7 +448,7 @@ async def test_other_group_update(hass, mock_bridge):
assert group_2 is not None
assert group_2.name == "Group 2"
assert group_2.state == "on"
assert group_2.attributes["brightness"] == 153
assert group_2.attributes["brightness"] == 154
assert group_2.attributes["color_temp"] == 250
updated_group_response = dict(GROUP_RESPONSE)