mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
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:
parent
18478ebd05
commit
e0a7ea52fd
@ -157,7 +157,7 @@ def preprocess_turn_on_alternatives(params):
|
|||||||
|
|
||||||
brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None)
|
brightness_pct = params.pop(ATTR_BRIGHTNESS_PCT, None)
|
||||||
if brightness_pct is not 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)
|
xy_color = params.pop(ATTR_XY_COLOR, None)
|
||||||
if xy_color is not None:
|
if xy_color is not None:
|
||||||
@ -233,7 +233,7 @@ async def async_setup(hass, config):
|
|||||||
brightness += params.pop(ATTR_BRIGHTNESS_STEP)
|
brightness += params.pop(ATTR_BRIGHTNESS_STEP)
|
||||||
|
|
||||||
else:
|
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))
|
params[ATTR_BRIGHTNESS] = max(0, min(255, brightness))
|
||||||
turn_light_off, off_params = preprocess_turn_off(params)
|
turn_light_off, off_params = preprocess_turn_off(params)
|
||||||
|
@ -85,7 +85,7 @@ async def test_state_attributes(hass):
|
|||||||
|
|
||||||
state = hass.states.get(ENTITY_LIGHT)
|
state = hass.states.get(ENTITY_LIGHT)
|
||||||
assert state.attributes.get(ATTR_COLOR_TEMP) == 333
|
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):
|
async def test_turn_off(hass):
|
||||||
|
@ -495,4 +495,56 @@ async def test_light_brightness_step(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
_, data = entity.last_call("turn_on")
|
_, 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user