diff --git a/homeassistant/components/light/__init__.py b/homeassistant/components/light/__init__.py index 7005fce63ccb..9750e37f32c0 100644 --- a/homeassistant/components/light/__init__.py +++ b/homeassistant/components/light/__init__.py @@ -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) diff --git a/tests/components/demo/test_light.py b/tests/components/demo/test_light.py index f48bc28c7727..fd17d7bdb0e7 100644 --- a/tests/components/demo/test_light.py +++ b/tests/components/demo/test_light.py @@ -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): diff --git a/tests/components/light/test_init.py b/tests/components/light/test_init.py index 49bc626a9574..2e2f74828d95 100644 --- a/tests/components/light/test_init.py +++ b/tests/components/light/test_init.py @@ -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