Refactor openhome media player getters and attrs (#98690)

pull/98871/head
Barry Williams 2023-08-23 10:31:46 +01:00 committed by GitHub
parent b143fe285f
commit 918d822ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 81 deletions

View File

@ -106,18 +106,11 @@ class OpenhomeDevice(MediaPlayerEntity):
"""Initialise the Openhome device.""" """Initialise the Openhome device."""
self.hass = hass self.hass = hass
self._device = device self._device = device
self._track_information = {} self._attr_unique_id = device.uuid()
self._in_standby = None
self._transport_state = None
self._volume_level = None
self._volume_muted = None
self._attr_supported_features = SUPPORT_OPENHOME self._attr_supported_features = SUPPORT_OPENHOME
self._source_names = []
self._source_index = {} self._source_index = {}
self._source = {}
self._name = None
self._attr_state = MediaPlayerState.PLAYING self._attr_state = MediaPlayerState.PLAYING
self._available = True self._attr_available = True
@property @property
def device_info(self): def device_info(self):
@ -131,47 +124,47 @@ class OpenhomeDevice(MediaPlayerEntity):
name=self._device.friendly_name(), name=self._device.friendly_name(),
) )
@property
def available(self):
"""Device is available."""
return self._available
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update state of device.""" """Update state of device."""
try: try:
self._in_standby = await self._device.is_in_standby() self._attr_name = await self._device.room()
self._transport_state = await self._device.transport_state()
self._track_information = await self._device.track_info()
self._source = await self._device.source()
self._name = await self._device.room()
self._attr_supported_features = SUPPORT_OPENHOME self._attr_supported_features = SUPPORT_OPENHOME
source_index = {} source_index = {}
source_names = [] source_names = []
track_information = await self._device.track_info()
self._attr_media_image_url = track_information.get("albumArtwork")
self._attr_media_album_name = track_information.get("albumTitle")
self._attr_media_title = track_information.get("title")
if artists := track_information.get("artist"):
self._attr_media_artist = artists[0]
if self._device.volume_enabled: if self._device.volume_enabled:
self._attr_supported_features |= ( self._attr_supported_features |= (
MediaPlayerEntityFeature.VOLUME_STEP MediaPlayerEntityFeature.VOLUME_STEP
| MediaPlayerEntityFeature.VOLUME_MUTE | MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_SET | MediaPlayerEntityFeature.VOLUME_SET
) )
self._volume_level = await self._device.volume() / 100.0 self._attr_volume_level = await self._device.volume() / 100.0
self._volume_muted = await self._device.is_muted() self._attr_is_volume_muted = await self._device.is_muted()
for source in await self._device.sources(): for source in await self._device.sources():
source_names.append(source["name"]) source_names.append(source["name"])
source_index[source["name"]] = source["index"] source_index[source["name"]] = source["index"]
source = await self._device.source()
self._attr_source = source.get("name")
self._source_index = source_index self._source_index = source_index
self._source_names = source_names self._attr_source_list = source_names
if self._source["type"] == "Radio": if source["type"] == "Radio":
self._attr_supported_features |= ( self._attr_supported_features |= (
MediaPlayerEntityFeature.STOP MediaPlayerEntityFeature.STOP
| MediaPlayerEntityFeature.PLAY | MediaPlayerEntityFeature.PLAY
| MediaPlayerEntityFeature.PLAY_MEDIA | MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.BROWSE_MEDIA | MediaPlayerEntityFeature.BROWSE_MEDIA
) )
if self._source["type"] in ("Playlist", "Spotify"): if source["type"] in ("Playlist", "Spotify"):
self._attr_supported_features |= ( self._attr_supported_features |= (
MediaPlayerEntityFeature.PREVIOUS_TRACK MediaPlayerEntityFeature.PREVIOUS_TRACK
| MediaPlayerEntityFeature.NEXT_TRACK | MediaPlayerEntityFeature.NEXT_TRACK
@ -181,21 +174,23 @@ class OpenhomeDevice(MediaPlayerEntity):
| MediaPlayerEntityFeature.BROWSE_MEDIA | MediaPlayerEntityFeature.BROWSE_MEDIA
) )
if self._in_standby: in_standby = await self._device.is_in_standby()
transport_state = await self._device.transport_state()
if in_standby:
self._attr_state = MediaPlayerState.OFF self._attr_state = MediaPlayerState.OFF
elif self._transport_state == "Paused": elif transport_state == "Paused":
self._attr_state = MediaPlayerState.PAUSED self._attr_state = MediaPlayerState.PAUSED
elif self._transport_state in ("Playing", "Buffering"): elif transport_state in ("Playing", "Buffering"):
self._attr_state = MediaPlayerState.PLAYING self._attr_state = MediaPlayerState.PLAYING
elif self._transport_state == "Stopped": elif transport_state == "Stopped":
self._attr_state = MediaPlayerState.IDLE self._attr_state = MediaPlayerState.IDLE
else: else:
# Device is playing an external source with no transport controls # Device is playing an external source with no transport controls
self._attr_state = MediaPlayerState.PLAYING self._attr_state = MediaPlayerState.PLAYING
self._available = True self._attr_available = True
except (asyncio.TimeoutError, aiohttp.ClientError, UpnpError): except (asyncio.TimeoutError, aiohttp.ClientError, UpnpError):
self._available = False self._attr_available = False
@catch_request_errors() @catch_request_errors()
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
@ -273,57 +268,6 @@ class OpenhomeDevice(MediaPlayerEntity):
except UpnpError: except UpnpError:
_LOGGER.error("Error invoking pin %s", pin) _LOGGER.error("Error invoking pin %s", pin)
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def unique_id(self):
"""Return a unique ID."""
return self._device.uuid()
@property
def source_list(self):
"""List of available input sources."""
return self._source_names
@property
def media_image_url(self):
"""Image url of current playing media."""
return self._track_information.get("albumArtwork")
@property
def media_artist(self):
"""Artist of current playing media, music track only."""
if artists := self._track_information.get("artist"):
return artists[0]
@property
def media_album_name(self):
"""Album name of current playing media, music track only."""
return self._track_information.get("albumTitle")
@property
def media_title(self):
"""Title of current playing media."""
return self._track_information.get("title")
@property
def source(self):
"""Name of the current input source."""
return self._source.get("name")
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
return self._volume_level
@property
def is_volume_muted(self):
"""Return true if volume is muted."""
return self._volume_muted
@catch_request_errors() @catch_request_errors()
async def async_volume_up(self) -> None: async def async_volume_up(self) -> None:
"""Volume up media player.""" """Volume up media player."""