From 8bee57d3a3ec8c9bd7ea5e9badd61d3ca463598b Mon Sep 17 00:00:00 2001 From: g4bri3lDev Date: Fri, 23 Oct 2026 00:21:51 +0200 Subject: [PATCH] Add identify button to SteamVR Base Station (#187572) --- .../steamvr_base_station/__init__.py | 2 +- .../components/steamvr_base_station/button.py | 42 +++++++++++++++ .../steamvr_base_station/coordinator.py | 14 +++-- .../steamvr_base_station/conftest.py | 7 +++ .../snapshots/test_button.ambr | 52 +++++++++++++++++++ .../steamvr_base_station/test_button.py | 52 +++++++++++++++++++ 6 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 homeassistant/components/steamvr_base_station/button.py create mode 100644 tests/components/steamvr_base_station/snapshots/test_button.ambr create mode 100644 tests/components/steamvr_base_station/test_button.py diff --git a/homeassistant/components/steamvr_base_station/__init__.py b/homeassistant/components/steamvr_base_station/__init__.py index d740bb658386..d75bf397cf0b 100644 --- a/homeassistant/components/steamvr_base_station/__init__.py +++ b/homeassistant/components/steamvr_base_station/__init__.py @@ -16,7 +16,7 @@ from homeassistant.exceptions import ConfigEntryNotReady from .const import DOMAIN from .coordinator import SteamVRBaseStationConfigEntry, SteamVRBaseStationCoordinator -PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.SWITCH] +PLATFORMS: list[Platform] = [Platform.BUTTON, Platform.SENSOR, Platform.SWITCH] async def async_setup_entry( diff --git a/homeassistant/components/steamvr_base_station/button.py b/homeassistant/components/steamvr_base_station/button.py new file mode 100644 index 000000000000..6c90dc04c0f4 --- /dev/null +++ b/homeassistant/components/steamvr_base_station/button.py @@ -0,0 +1,42 @@ +"""Identify button for SteamVR Base Station.""" + +from typing import override + +from homeassistant.components.button import ( + ButtonDeviceClass, + ButtonEntity, + ButtonEntityDescription, +) +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 + +# Commands to one station are serialized by the library. +PARALLEL_UPDATES = 0 + +IDENTIFY = ButtonEntityDescription( + key="identify", + device_class=ButtonDeviceClass.IDENTIFY, + entity_category=EntityCategory.DIAGNOSTIC, +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: SteamVRBaseStationConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the identify button.""" + async_add_entities([SteamVRBaseStationButton(entry.runtime_data, IDENTIFY)]) + + +class SteamVRBaseStationButton(SteamVRBaseStationEntity, ButtonEntity): + """Blinks the station's LED.""" + + @override + async def async_press(self) -> None: + """Blink the LED.""" + await self.coordinator.async_identify() diff --git a/homeassistant/components/steamvr_base_station/coordinator.py b/homeassistant/components/steamvr_base_station/coordinator.py index 8ef0620568b0..18ca93cb42a2 100644 --- a/homeassistant/components/steamvr_base_station/coordinator.py +++ b/homeassistant/components/steamvr_base_station/coordinator.py @@ -1,7 +1,8 @@ """Coordinator for SteamVR Base Station.""" +from collections.abc import Coroutine import logging -from typing import override +from typing import Any, override from lighthouse_ble import ( BaseStationV2, @@ -79,12 +80,19 @@ class SteamVRBaseStationCoordinator(PassiveBluetoothDataUpdateCoordinator): async def async_set_power(self, target: PowerState) -> None: """Switch the station's power and show the result right away.""" + await self._async_run_command(self.station.set_power(target)) + self.async_update_listeners() + + async def async_identify(self) -> None: + """Blink the station's LED.""" + await self._async_run_command(self.station.identify()) + + async def _async_run_command(self, command: Coroutine[Any, Any, None]) -> None: try: - await self.station.set_power(target) + await command except LighthouseConnectionError as err: raise HomeAssistantError( translation_domain=DOMAIN, translation_key="cannot_connect", translation_placeholders={"name": self.entry.title}, ) from err - self.async_update_listeners() diff --git a/tests/components/steamvr_base_station/conftest.py b/tests/components/steamvr_base_station/conftest.py index 6117cf297a22..caf3908f93c9 100644 --- a/tests/components/steamvr_base_station/conftest.py +++ b/tests/components/steamvr_base_station/conftest.py @@ -38,6 +38,13 @@ def mock_set_power() -> Generator[AsyncMock]: yield mock +@pytest.fixture +def mock_identify() -> Generator[AsyncMock]: + """Mock blinking the LED.""" + with patch.object(BaseStationV2, "identify", autospec=True) as mock: + yield mock + + @pytest.fixture(autouse=True) def mock_read_device_info() -> Generator[AsyncMock]: """Mock reading the Device Information Service.""" diff --git a/tests/components/steamvr_base_station/snapshots/test_button.ambr b/tests/components/steamvr_base_station/snapshots/test_button.ambr new file mode 100644 index 000000000000..5063f0645601 --- /dev/null +++ b/tests/components/steamvr_base_station/snapshots/test_button.ambr @@ -0,0 +1,52 @@ +# serializer version: 1 +# name: test_button_entities[button.lhb_747a9bc5_identify-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': 'button', + 'entity_category': , + 'entity_id': 'button.lhb_747a9bc5_identify', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Identify', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Identify', + '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_identify', + 'unit_of_measurement': None, + }) +# --- +# name: test_button_entities[button.lhb_747a9bc5_identify-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'identify', + : 'LHB-747A9BC5 Identify', + }), + 'context': , + 'entity_id': 'button.lhb_747a9bc5_identify', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/steamvr_base_station/test_button.py b/tests/components/steamvr_base_station/test_button.py new file mode 100644 index 000000000000..70503e8b22e1 --- /dev/null +++ b/tests/components/steamvr_base_station/test_button.py @@ -0,0 +1,52 @@ +"""Test the SteamVR Base Station identify button.""" + +from unittest.mock import AsyncMock + +from lighthouse_ble import LighthouseConnectionError +import pytest +from pytest_homeassistant.common import MockConfigEntry, snapshot_platform +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.const import ATTR_ENTITY_ID, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import entity_registry as er + +IDENTIFY = "button.lhb_747a9bc5_identify" + + +@pytest.fixture +def platforms() -> list[Platform]: + """Only set up the button platform.""" + return [Platform.BUTTON] + + +async def test_button_entities( + hass: HomeAssistant, + init_integration: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the button entities.""" + await snapshot_platform(hass, entity_registry, snapshot, init_integration.entry_id) + + +@pytest.mark.usefixtures("init_integration") +async def test_identify(hass: HomeAssistant, mock_identify: AsyncMock) -> None: + """Test pressing the button blinks the LED.""" + await hass.services.async_call( + BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: IDENTIFY}, blocking=True + ) + mock_identify.assert_awaited_once() + + +@pytest.mark.usefixtures("init_integration") +async def test_identify_error(hass: HomeAssistant, mock_identify: AsyncMock) -> None: + """Test a failed identify raises a translated error.""" + mock_identify.side_effect = LighthouseConnectionError("gone") + with pytest.raises(HomeAssistantError) as raised: + await hass.services.async_call( + BUTTON_DOMAIN, SERVICE_PRESS, {ATTR_ENTITY_ID: IDENTIFY}, blocking=True + ) + assert raised.value.translation_key == "cannot_connect"