Move action implementation out of HEOS Coordinator (#136539)

* Move play_source

* Update property docstring

* Correct import location
pull/134371/head^2
Andrew Sayre 2025-01-26 04:20:37 -06:00 committed by GitHub
parent ee07f1f290
commit f8013655be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 22 deletions

View File

@ -15,7 +15,6 @@ from pyheos import (
HeosError,
HeosNowPlayingMedia,
HeosOptions,
HeosPlayer,
MediaItem,
MediaType,
PlayerUpdateResult,
@ -25,12 +24,12 @@ from pyheos import (
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HassJob, HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady, ServiceValidationError
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from . import DOMAIN
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -62,6 +61,11 @@ class HeosCoordinator(DataUpdateCoordinator[None]):
self._inputs: list[MediaItem] = []
super().__init__(hass, _LOGGER, config_entry=config_entry, name=DOMAIN)
@property
def inputs(self) -> list[MediaItem]:
"""Get input sources across all devices."""
return self._inputs
async def async_setup(self) -> None:
"""Set up the coordinator; connect to the host; and retrieve initial data."""
# Add before connect as it may occur during initial connection
@ -265,21 +269,3 @@ class HeosCoordinator(DataUpdateCoordinator[None]):
):
return favorite.name
return None
async def async_play_source(self, source: str, player: HeosPlayer) -> None:
"""Determine type of source and play it."""
# Favorite
if (index := self.async_get_favorite_index(source)) is not None:
await player.play_preset_station(index)
return
# Input source
for input_source in self._inputs:
if input_source.name == source:
await player.play_media(input_source)
return
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="unknown_source",
translation_placeholders={"source": source},
)

View File

@ -306,7 +306,21 @@ class HeosMediaPlayer(CoordinatorEntity[HeosCoordinator], MediaPlayerEntity):
@catch_action_error("select source")
async def async_select_source(self, source: str) -> None:
"""Select input source."""
await self.coordinator.async_play_source(source, self._player)
# Favorite
if (index := self.coordinator.async_get_favorite_index(source)) is not None:
await self._player.play_preset_station(index)
return
# Input source
for input_source in self.coordinator.inputs:
if input_source.name == source:
await self._player.play_media(input_source)
return
raise ServiceValidationError(
translation_domain=HEOS_DOMAIN,
translation_key="unknown_source",
translation_placeholders={"source": source},
)
@catch_action_error("set repeat")
async def async_set_repeat(self, repeat: RepeatMode) -> None: