Fix Qbus weather binary sensors with boolean state values (#187124)

This commit is contained in:
xjh
2026-10-19 04:53:06 +00:00
committed by GitHub
parent edbe03f2ed
commit 1d46476ca3
2 changed files with 56 additions and 4 deletions
@@ -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"
)
+51 -1
View File
@@ -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"