Skip to content

Commit 3b45873

Browse files
authored
Fix setting brightness to 0 in HomeKit when the On characteristic is not sent (#129201)
1 parent 2c8fc67 commit 3b45873

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

homeassistant/components/homekit/type_lights.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,20 @@ def _async_send_events(self, _now: datetime) -> None:
171171
events = []
172172
service = SERVICE_TURN_ON
173173
params: dict[str, Any] = {ATTR_ENTITY_ID: self.entity_id}
174+
has_on = CHAR_ON in char_values
174175

175-
if CHAR_ON in char_values:
176+
if has_on:
176177
if not char_values[CHAR_ON]:
177178
service = SERVICE_TURN_OFF
178179
events.append(f"Set state to {char_values[CHAR_ON]}")
179180

180181
brightness_pct = None
181182
if CHAR_BRIGHTNESS in char_values:
182183
if char_values[CHAR_BRIGHTNESS] == 0:
183-
events[-1] = "Set state to 0"
184+
if has_on:
185+
events[-1] = "Set state to 0"
186+
else:
187+
events.append("Set state to 0")
184188
service = SERVICE_TURN_OFF
185189
else:
186190
brightness_pct = char_values[CHAR_BRIGHTNESS]

tests/components/homekit/test_type_lights.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,24 @@ async def test_light_brightness(
226226
assert len(events) == 3
227227
assert events[-1].data[ATTR_VALUE] == f"Set state to 0, brightness at 0{PERCENTAGE}"
228228

229+
hk_driver.set_characteristics(
230+
{
231+
HAP_REPR_CHARS: [
232+
{
233+
HAP_REPR_AID: acc.aid,
234+
HAP_REPR_IID: char_brightness_iid,
235+
HAP_REPR_VALUE: 0,
236+
},
237+
]
238+
},
239+
"mock_addr",
240+
)
241+
await _wait_for_light_coalesce(hass)
242+
assert call_turn_off
243+
assert call_turn_off[0].data[ATTR_ENTITY_ID] == entity_id
244+
assert len(events) == 4
245+
assert events[-1].data[ATTR_VALUE] == f"Set state to 0, brightness at 0{PERCENTAGE}"
246+
229247
# 0 is a special case for homekit, see "Handle Brightness"
230248
# in update_state
231249
hass.states.async_set(

0 commit comments

Comments
 (0)