mirror of
https://github.com/home-assistant/core.git
synced 2026-09-28 02:18:10 -04:00
Add party mode to Russound RIO (#167342)
This commit is contained in:
@@ -14,7 +14,7 @@ from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
|
||||
|
||||
from .const import DOMAIN, RUSSOUND_RIO_EXCEPTIONS
|
||||
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SWITCH]
|
||||
PLATFORMS = [Platform.MEDIA_PLAYER, Platform.NUMBER, Platform.SELECT, Platform.SWITCH]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
{
|
||||
"entity": {
|
||||
"select": {
|
||||
"party_mode": {
|
||||
"default": "mdi:party-popper"
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"loudness": {
|
||||
"default": "mdi:volume-high",
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
"""Support for Russound RIO select entities."""
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from aiorussound.models import PartyMode
|
||||
from aiorussound.rio import Controller, ZoneControlSurface
|
||||
|
||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from . import RussoundConfigEntry
|
||||
from .entity import RussoundBaseEntity, command
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class RussoundZoneSelectEntityDescription(SelectEntityDescription):
|
||||
"""Describes Russound RIO select entity."""
|
||||
|
||||
value_fn: Callable[[ZoneControlSurface], str | None]
|
||||
set_value_fn: Callable[[ZoneControlSurface, str], Awaitable[None]]
|
||||
|
||||
|
||||
CONTROL_ENTITIES: tuple[RussoundZoneSelectEntityDescription, ...] = (
|
||||
RussoundZoneSelectEntityDescription(
|
||||
key="party_mode",
|
||||
translation_key="party_mode",
|
||||
options=[
|
||||
PartyMode.OFF.value.lower(),
|
||||
PartyMode.ON.value.lower(),
|
||||
PartyMode.MASTER.value.lower(),
|
||||
],
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
value_fn=lambda zone: zone.party_mode.lower() if zone.party_mode else None,
|
||||
set_value_fn=lambda zone, value: zone.set_party_mode(PartyMode(value.upper())),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: RussoundConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Russound RIO select entities based on a config entry."""
|
||||
client = entry.runtime_data
|
||||
async_add_entities(
|
||||
RussoundSelectEntity(controller, zone_id, description)
|
||||
for controller in client.controllers.values()
|
||||
for zone_id in controller.zones
|
||||
for description in CONTROL_ENTITIES
|
||||
)
|
||||
|
||||
|
||||
class RussoundSelectEntity(RussoundBaseEntity, SelectEntity):
|
||||
"""Defines a Russound RIO select entity."""
|
||||
|
||||
entity_description: RussoundZoneSelectEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
controller: Controller,
|
||||
zone_id: int,
|
||||
description: RussoundZoneSelectEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize Russound RIO select."""
|
||||
super().__init__(controller, zone_id)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{self._primary_mac_address}-{self._zone.device_str}-{description.key}"
|
||||
)
|
||||
|
||||
@property
|
||||
def current_option(self) -> str | None:
|
||||
"""Return the state of the select."""
|
||||
return self.entity_description.value_fn(self._zone)
|
||||
|
||||
@command
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Change the selected option."""
|
||||
await self.entity_description.set_value_fn(self._zone, option)
|
||||
@@ -55,6 +55,16 @@
|
||||
"name": "Turn-on volume"
|
||||
}
|
||||
},
|
||||
"select": {
|
||||
"party_mode": {
|
||||
"name": "Party mode",
|
||||
"state": {
|
||||
"master": "Leader",
|
||||
"off": "[%key:common::state::off%]",
|
||||
"on": "[%key:common::state::on%]"
|
||||
}
|
||||
}
|
||||
},
|
||||
"switch": {
|
||||
"loudness": {
|
||||
"name": "Loudness"
|
||||
|
||||
@@ -85,6 +85,7 @@ def mock_russound_client() -> Generator[AsyncMock]:
|
||||
zone.set_turn_on_volume = AsyncMock()
|
||||
zone.set_loudness = AsyncMock()
|
||||
zone.restore_preset = AsyncMock()
|
||||
zone.set_party_mode = AsyncMock()
|
||||
|
||||
client.controllers = {
|
||||
1: Controller(
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
# serializer version: 1
|
||||
# name: test_all_entities[select.backyard_party_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'select',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'select.backyard_party_mode',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Party mode',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Party mode',
|
||||
'platform': 'russound_rio',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'party_mode',
|
||||
'unique_id': '00:11:22:33:44:55-C[1].Z[1]-party_mode',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.backyard_party_mode-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Backyard Party mode',
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.backyard_party_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.bedroom_party_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'select',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'select.bedroom_party_mode',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Party mode',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Party mode',
|
||||
'platform': 'russound_rio',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'party_mode',
|
||||
'unique_id': '00:11:22:33:44:55-C[1].Z[3]-party_mode',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.bedroom_party_mode-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Bedroom Party mode',
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.bedroom_party_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.kitchen_party_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'select',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'select.kitchen_party_mode',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Party mode',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Party mode',
|
||||
'platform': 'russound_rio',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'party_mode',
|
||||
'unique_id': '00:11:22:33:44:55-C[1].Z[2]-party_mode',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.kitchen_party_mode-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Kitchen Party mode',
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.kitchen_party_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.living_room_party_mode-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': list([
|
||||
None,
|
||||
]),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'select',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'select.living_room_party_mode',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'object_id_base': 'Party mode',
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Party mode',
|
||||
'platform': 'russound_rio',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'party_mode',
|
||||
'unique_id': '00:11:22:33:44:55-C[2].Z[9]-party_mode',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[select.living_room_party_mode-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Living Room Party mode',
|
||||
'options': list([
|
||||
'off',
|
||||
'on',
|
||||
'master',
|
||||
]),
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'select.living_room_party_mode',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Tests for the Russound RIO select platform."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aiorussound.models import PartyMode
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.select import (
|
||||
DOMAIN as SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_OPTION, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_russound_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
with patch("homeassistant.components.russound_rio.PLATFORMS", [Platform.SELECT]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_setting_value(
|
||||
hass: HomeAssistant,
|
||||
mock_russound_client: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test setting value."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.backyard_party_mode",
|
||||
ATTR_OPTION: "master",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
mock_russound_client.controllers[1].zones[1].set_party_mode.assert_called_once_with(
|
||||
PartyMode.MASTER
|
||||
)
|
||||
mock_russound_client.controllers[1].zones[1].set_party_mode.reset_mock()
|
||||
await hass.services.async_call(
|
||||
SELECT_DOMAIN,
|
||||
SERVICE_SELECT_OPTION,
|
||||
{
|
||||
ATTR_ENTITY_ID: "select.backyard_party_mode",
|
||||
ATTR_OPTION: "off",
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
mock_russound_client.controllers[1].zones[1].set_party_mode.assert_called_once_with(
|
||||
PartyMode.OFF
|
||||
)
|
||||
Reference in New Issue
Block a user