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)