From 2be99fb68be02cca40b853c1ae7fbc3fa69b488d Mon Sep 17 00:00:00 2001 From: g4bri3lDev Date: Sat, 24 Oct 2026 11:47:54 +0200 Subject: [PATCH] Add fault binary sensor to SteamVR Base Station (#188261) --- .../steamvr_base_station/__init__.py | 1 + .../steamvr_base_station/binary_sensor.py | 42 +++++++++++++++ .../snapshots/test_binary_sensor.ambr | 52 +++++++++++++++++++ .../test_binary_sensor.py | 46 ++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 homeassistant/components/steamvr_base_station/binary_sensor.py create mode 100644 tests/components/steamvr_base_station/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/steamvr_base_station/test_binary_sensor.py diff --git a/homeassistant/components/steamvr_base_station/__init__.py b/homeassistant/components/steamvr_base_station/__init__.py index 368844e641b2..c2ee862d2b7f 100644 --- a/homeassistant/components/steamvr_base_station/__init__.py +++ b/homeassistant/components/steamvr_base_station/__init__.py @@ -17,6 +17,7 @@ from .const import DOMAIN from .coordinator import SteamVRBaseStationConfigEntry, SteamVRBaseStationCoordinator PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SELECT, Platform.SENSOR, diff --git a/homeassistant/components/steamvr_base_station/binary_sensor.py b/homeassistant/components/steamvr_base_station/binary_sensor.py new file mode 100644 index 000000000000..b2f5e3b73f2a --- /dev/null +++ b/homeassistant/components/steamvr_base_station/binary_sensor.py @@ -0,0 +1,42 @@ +"""Fault binary sensor for SteamVR Base Station.""" + +from typing import override + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .coordinator import SteamVRBaseStationConfigEntry +from .entity import SteamVRBaseStationEntity + +PARALLEL_UPDATES = 0 + +FAULT = BinarySensorEntityDescription( + key="fault", + device_class=BinarySensorDeviceClass.PROBLEM, + entity_category=EntityCategory.DIAGNOSTIC, +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: SteamVRBaseStationConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the fault binary sensor.""" + async_add_entities([SteamVRBaseStationFaultSensor(entry.runtime_data, FAULT)]) + + +class SteamVRBaseStationFaultSensor(SteamVRBaseStationEntity, BinarySensorEntity): + """On when the station reports a fault.""" + + @property + @override + def is_on(self) -> bool | None: + """Return whether the station reports a fault.""" + return self.coordinator.station.state.faulty diff --git a/tests/components/steamvr_base_station/snapshots/test_binary_sensor.ambr b/tests/components/steamvr_base_station/snapshots/test_binary_sensor.ambr new file mode 100644 index 000000000000..a60d34bc9d78 --- /dev/null +++ b/tests/components/steamvr_base_station/snapshots/test_binary_sensor.ambr @@ -0,0 +1,52 @@ +# serializer version: 1 +# name: test_binary_sensor_entities[binary_sensor.lhb_747a9bc5_problem-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.lhb_747a9bc5_problem', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Problem', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Problem', + 'platform': 'steamvr_base_station', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'AA:BB:CC:DD:EE:01_fault', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_sensor_entities[binary_sensor.lhb_747a9bc5_problem-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'problem', + : 'LHB-747A9BC5 Problem', + }), + 'context': , + 'entity_id': 'binary_sensor.lhb_747a9bc5_problem', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/steamvr_base_station/test_binary_sensor.py b/tests/components/steamvr_base_station/test_binary_sensor.py new file mode 100644 index 000000000000..dc2ccef3097a --- /dev/null +++ b/tests/components/steamvr_base_station/test_binary_sensor.py @@ -0,0 +1,46 @@ +"""Test the SteamVR Base Station fault binary sensor.""" + +import pytest +from pytest_homeassistant.common import MockConfigEntry, snapshot_platform +from pytest_homeassistant.components.bluetooth import ( + inject_bluetooth_service_info_bleak, +) +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import STATE_OFF, STATE_ON, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import make_service_info, payload + +FAULT = "binary_sensor.lhb_747a9bc5_problem" + + +@pytest.fixture +def platforms() -> list[Platform]: + """Only set up the binary sensor platform.""" + return [Platform.BINARY_SENSOR] + + +async def test_binary_sensor_entities( + hass: HomeAssistant, + init_integration: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the binary sensor entities.""" + await snapshot_platform(hass, entity_registry, snapshot, init_integration.entry_id) + + +@pytest.mark.parametrize( + ("fault", "expected"), + [pytest.param(0, STATE_OFF, id="ok"), pytest.param(1, STATE_ON, id="fault")], +) +@pytest.mark.usefixtures("init_integration") +async def test_fault_follows_advertisements( + hass: HomeAssistant, fault: int, expected: str +) -> None: + """Test the fault sensor reflects the advertised fault flag.""" + inject_bluetooth_service_info_bleak(hass, make_service_info(payload(fault=fault))) + await hass.async_block_till_done() + assert hass.states.get(FAULT).state == expected