From fe5d45ed5770d2515518dc309d598840d61df2c4 Mon Sep 17 00:00:00 2001 From: Thomas D <11554546+thomasddn@users.noreply.github.com> Date: Fri, 10 Apr 2026 17:24:00 +0200 Subject: [PATCH] Fix light on action for qbus integration (#167917) --- homeassistant/components/qbus/light.py | 6 +++-- tests/components/qbus/test_light.py | 35 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/qbus/light.py b/homeassistant/components/qbus/light.py index 81c7a3aa21ac..2e43b1d444f9 100644 --- a/homeassistant/components/qbus/light.py +++ b/homeassistant/components/qbus/light.py @@ -79,8 +79,10 @@ class QbusLight(QbusEntity, LightEntity): await self._async_publish_output_state(state) async def _handle_state_received(self, state: QbusMqttAnalogState) -> None: - percentage = round(state.read_percentage() or 0) - self._set_state(percentage) + percentage = state.read_percentage() + + if percentage is not None: + self._set_state(round(percentage)) def _set_state(self, percentage: int) -> None: self._attr_is_on = percentage > 0 diff --git a/tests/components/qbus/test_light.py b/tests/components/qbus/test_light.py index 2db2c622289c..093bb658ade2 100644 --- a/tests/components/qbus/test_light.py +++ b/tests/components/qbus/test_light.py @@ -20,6 +20,7 @@ _PAYLOAD_LIGHT_STATE_ON = '{"id":"UL15","properties":{"value":60},"type":"state" _PAYLOAD_LIGHT_STATE_BRIGHTNESS = ( '{"id":"UL15","properties":{"value":' + str(_BRIGHTNESS_PCT) + '},"type":"state"}' ) +_PAYLOAD_LIGHT_STATE_EVENT = '{"id":"UL15","action":"on","type":"event"}' _PAYLOAD_LIGHT_STATE_OFF = '{"id":"UL15","properties":{"value":0},"type":"state"}' _PAYLOAD_LIGHT_SET_STATE_ON = '{"id": "UL15", "type": "action", "action": "on"}' @@ -104,3 +105,37 @@ async def test_light( await hass.async_block_till_done() assert hass.states.get(_LIGHT_ENTITY_ID).state == STATE_OFF + + +async def test_light_ignore_missing_percentage( + hass: HomeAssistant, + mqtt_mock: MqttMockHAClient, + setup_integration: None, +) -> None: + """Test ignoring events without percentage.""" + + # Switch ON + mqtt_mock.reset_mock() + await hass.services.async_call( + LIGHT_DOMAIN, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: _LIGHT_ENTITY_ID}, + blocking=True, + ) + + # Simulate response + async_fire_mqtt_message(hass, _TOPIC_LIGHT_STATE, _PAYLOAD_LIGHT_STATE_ON) + await hass.async_block_till_done() + + entity = hass.states.get(_LIGHT_ENTITY_ID) + brightness = entity.attributes.get(ATTR_BRIGHTNESS) + assert entity.state == STATE_ON + assert brightness > 0 + + # Simulate additional event response + async_fire_mqtt_message(hass, _TOPIC_LIGHT_STATE, _PAYLOAD_LIGHT_STATE_EVENT) + await hass.async_block_till_done() + + entity = hass.states.get(_LIGHT_ENTITY_ID) + assert entity.state == STATE_ON + assert entity.attributes.get(ATTR_BRIGHTNESS) == brightness