From 1d46476ca3988f01ed477303428efebdd2264806 Mon Sep 17 00:00:00 2001 From: xjh <87483213+xiajiahang@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:53:06 +0800 Subject: [PATCH] Fix Qbus weather binary sensors with boolean state values (#187124) --- .../components/qbus/binary_sensor.py | 8 +-- tests/components/qbus/test_binary_sensor.py | 52 ++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/qbus/binary_sensor.py b/homeassistant/components/qbus/binary_sensor.py index 9396770a4aa5..bcf5b35ba443 100644 --- a/homeassistant/components/qbus/binary_sensor.py +++ b/homeassistant/components/qbus/binary_sensor.py @@ -1,7 +1,7 @@ """Support for Qbus binary sensor.""" from dataclasses import dataclass -from typing import cast, override +from typing import override from qbusmqttapi.discovery import QbusMqttDevice, QbusMqttOutput from qbusmqttapi.factory import QbusMqttTopicFactory @@ -105,9 +105,11 @@ class QbusWeatherBinarySensor(QbusEntity, BinarySensorEntity): @override async def _handle_state_received(self, state: QbusMqttWeatherState) -> None: - if value := state.read_property(self.entity_description.property, None): + if ( + value := state.read_property(self.entity_description.property, None) + ) is not None: self._attr_is_on = ( - None if value is None else cast(str, value).lower() == "true" + value if isinstance(value, bool) else str(value).lower() == "true" ) diff --git a/tests/components/qbus/test_binary_sensor.py b/tests/components/qbus/test_binary_sensor.py index 9160bdb916e9..9e38d118c71b 100644 --- a/tests/components/qbus/test_binary_sensor.py +++ b/tests/components/qbus/test_binary_sensor.py @@ -1,15 +1,17 @@ """Test Qbus binary sensors.""" from collections.abc import Awaitable, Callable +import json from unittest.mock import patch +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from tests.common import MockConfigEntry, snapshot_platform +from tests.common import MockConfigEntry, async_fire_mqtt_message, snapshot_platform async def test_binary_sensor( @@ -25,3 +27,51 @@ async def test_binary_sensor( await setup_integration_deferred() await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +@pytest.mark.usefixtures("setup_integration") +@pytest.mark.parametrize("property_name", ["raining", "twilight"]) +@pytest.mark.parametrize( + ("on_value", "off_value"), + [ + pytest.param(True, False, id="boolean"), + pytest.param("true", "false", id="string"), + pytest.param("TRUE", "FALSE", id="uppercase-string"), + pytest.param("true", False, id="boolean-off"), + ], +) +async def test_weather_binary_sensor_state( + hass: HomeAssistant, + property_name: str, + on_value: bool | str, + off_value: bool | str, +) -> None: + """Test weather states and partial updates for boolean and string payloads.""" + entity_id = f"binary_sensor.tuin_weersensor_{property_name}" + topic = "cloudapp/QBUSMQTTGW/UL1/UL60/state" + + async_fire_mqtt_message( + hass, + topic, + json.dumps( + {"id": "UL60", "properties": {property_name: on_value}, "type": "event"} + ), + ) + await hass.async_block_till_done() + assert hass.states.get(entity_id).state == "on" + + async_fire_mqtt_message( + hass, topic, '{"id":"UL60","properties":{"wind":25},"type":"event"}' + ) + await hass.async_block_till_done() + assert hass.states.get(entity_id).state == "on" + + async_fire_mqtt_message( + hass, + topic, + json.dumps( + {"id": "UL60", "properties": {property_name: off_value}, "type": "event"} + ), + ) + await hass.async_block_till_done() + assert hass.states.get(entity_id).state == "off"