From 8025b2ba3c7fa97a5b4a5eefbdf86fe1c5a549e2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 23 Jul 2026 08:28:59 -1000 Subject: [PATCH] Fix homekit_controller sending zero brightness for low brightness values (#177148) --- .../components/homekit_controller/light.py | 7 +++-- .../homekit_controller/test_light.py | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homekit_controller/light.py b/homeassistant/components/homekit_controller/light.py index fd08872fc8ef..c49083a12651 100644 --- a/homeassistant/components/homekit_controller/light.py +++ b/homeassistant/components/homekit_controller/light.py @@ -188,8 +188,11 @@ class HomeKitLight(HomeKitEntity, LightEntity): characteristics: dict[str, Any] = {} if brightness is not None: - characteristics[CharacteristicsTypes.BRIGHTNESS] = int( - brightness * 100 / 255 + # Some devices such as Nanoleaf Essentials treat brightness 0 + # with on as full brightness; a real brightness 0 is handled + # as turn_off by the light component. + characteristics[CharacteristicsTypes.BRIGHTNESS] = max( + 1, int(brightness * 100 / 255) ) # If they send both temperature and hs_color, and the device diff --git a/tests/components/homekit_controller/test_light.py b/tests/components/homekit_controller/test_light.py index c656507ffaf3..3b3ad971b5d0 100644 --- a/tests/components/homekit_controller/test_light.py +++ b/tests/components/homekit_controller/test_light.py @@ -7,6 +7,7 @@ from aiohomekit.model import Accessory from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.services import Service, ServicesTypes from aiohomekit.testing import FakeController +import pytest from homeassistant.components.homekit_controller.const import KNOWN_DEVICES from homeassistant.components.light import ( @@ -60,6 +61,34 @@ def create_lightbulb_service_with_color_temp(accessory: Accessory) -> Service: return service +@pytest.mark.parametrize("brightness", [1, 2, 3]) +async def test_turn_on_low_brightness_no_zero( + hass: HomeAssistant, get_next_aid: Callable[[], int], brightness: int +) -> None: + """Test low brightness values never send 0 percent to the device. + + Sending brightness 0 with on results in full brightness on some + devices such as Nanoleaf Essentials bulbs. + """ + helper = await setup_test_component( + hass, get_next_aid(), create_lightbulb_service_with_hs + ) + + await hass.services.async_call( + "light", + "turn_on", + {"entity_id": "light.testdevice", "brightness": brightness}, + blocking=True, + ) + helper.async_assert_service_values( + ServicesTypes.LIGHTBULB, + { + CharacteristicsTypes.ON: True, + CharacteristicsTypes.BRIGHTNESS: 1, + }, + ) + + async def test_switch_change_light_state( hass: HomeAssistant, get_next_aid: Callable[[], int] ) -> None: