Add identify button to SteamVR Base Station (#187572)

This commit is contained in:
g4bri3lDev
2026-10-23 00:21:51 +02:00
committed by GitHub
parent 4a859f28fb
commit 8bee57d3a3
6 changed files with 165 additions and 4 deletions
@@ -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(
@@ -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()
@@ -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()
@@ -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."""
@@ -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': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'button.lhb_747a9bc5_identify',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Identify',
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.IDENTIFY: 'identify'>,
'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({
<EntityStateAttribute.DEVICE_CLASS: 'device_class'>: 'identify',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'LHB-747A9BC5 Identify',
}),
'context': <ANY>,
'entity_id': 'button.lhb_747a9bc5_identify',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
@@ -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"