Reduce loss of precision when setting light percent brightness (#34208)

* Reduce loss of precision when setting light percent brightness

This part of an effort to fix all the round trip light
brightness percentages that cause errors with homekit
, alexa, and other devices that use percentage.

* fix demo light test
This commit is contained in:
J. Nick Koston 2020-04-14 13:26:18 -05:00 committed by GitHub
parent 18478ebd05
commit e0a7ea52fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 4 deletions

View File

@ -157,7 +157,7 @@ def preprocess_turn_on_alternatives(params):
brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None)
if brightness_pct is not None:
params[ATTR_BRIGHTNESS] = int(255 * brightness_pct / 100)
params[ATTR_BRIGHTNESS] = round(255 * brightness_pct / 100)
xy_color = params.pop(ATTR_XY_COLOR, None)
if xy_color is not None:
@ -233,7 +233,7 @@ async def async_setup(hass, config):
brightness += params.pop(ATTR_BRIGHTNESS_STEP)
else:
brightness += int(params.pop(ATTR_BRIGHTNESS_STEP_PCT) / 100 * 255)
brightness += round(params.pop(ATTR_BRIGHTNESS_STEP_PCT) / 100 * 255)
params[ATTR_BRIGHTNESS] = max(0, min(255, brightness))
turn_light_off, off_params = preprocess_turn_off(params)

View File

@ -85,7 +85,7 @@ async def test_state_attributes(hass):
state = hass.states.get(ENTITY_LIGHT)
assert state.attributes.get(ATTR_COLOR_TEMP) == 333
assert state.attributes.get(ATTR_BRIGHTNESS) == 127
assert state.attributes.get(ATTR_BRIGHTNESS) == 128
async def test_turn_off(hass):

View File

@ -495,4 +495,56 @@ async def test_light_brightness_step(hass):
)
_, data = entity.last_call("turn_on")
assert data["brightness"] == 125, data
assert data["brightness"] == 126, data
async def test_light_brightness_pct_conversion(hass):
"""Test that light brightness percent conversion."""
platform = getattr(hass.components, "test.light")
platform.init()
entity = platform.ENTITIES[0]
entity.supported_features = light.SUPPORT_BRIGHTNESS
entity.brightness = 100
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
state = hass.states.get(entity.entity_id)
assert state is not None
assert state.attributes["brightness"] == 100
await hass.services.async_call(
"light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 1}, True,
)
_, data = entity.last_call("turn_on")
assert data["brightness"] == 3, data
await hass.services.async_call(
"light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 2}, True,
)
_, data = entity.last_call("turn_on")
assert data["brightness"] == 5, data
await hass.services.async_call(
"light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 50}, True,
)
_, data = entity.last_call("turn_on")
assert data["brightness"] == 128, data
await hass.services.async_call(
"light", "turn_on", {"entity_id": entity.entity_id, "brightness_pct": 99}, True,
)
_, data = entity.last_call("turn_on")
assert data["brightness"] == 252, data
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": entity.entity_id, "brightness_pct": 100},
True,
)
_, data = entity.last_call("turn_on")
assert data["brightness"] == 255, data