mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Use new media player enums in forked_daapd (#78100)
This commit is contained in:
parent
d88334b2b2
commit
52ea9998bb
@ -10,22 +10,15 @@ from pyforked_daapd import ForkedDaapdAPI
|
|||||||
from pylibrespot_java import LibrespotJavaAPI
|
from pylibrespot_java import LibrespotJavaAPI
|
||||||
|
|
||||||
from homeassistant.components import media_source
|
from homeassistant.components import media_source
|
||||||
from homeassistant.components.media_player import BrowseMedia, MediaPlayerEntity
|
from homeassistant.components.media_player import (
|
||||||
from homeassistant.components.media_player.browse_media import (
|
BrowseMedia,
|
||||||
|
MediaPlayerEntity,
|
||||||
|
MediaPlayerState,
|
||||||
|
MediaType,
|
||||||
async_process_play_media_url,
|
async_process_play_media_url,
|
||||||
)
|
)
|
||||||
from homeassistant.components.media_player.const import MEDIA_TYPE_MUSIC
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
|
||||||
CONF_HOST,
|
|
||||||
CONF_PASSWORD,
|
|
||||||
CONF_PORT,
|
|
||||||
STATE_IDLE,
|
|
||||||
STATE_OFF,
|
|
||||||
STATE_ON,
|
|
||||||
STATE_PAUSED,
|
|
||||||
STATE_PLAYING,
|
|
||||||
)
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
@ -171,7 +164,7 @@ class ForkedDaapdZone(MediaPlayerEntity):
|
|||||||
|
|
||||||
async def async_toggle(self) -> None:
|
async def async_toggle(self) -> None:
|
||||||
"""Toggle the power on the zone."""
|
"""Toggle the power on the zone."""
|
||||||
if self.state == STATE_OFF:
|
if self.state == MediaPlayerState.OFF:
|
||||||
await self.async_turn_on()
|
await self.async_turn_on()
|
||||||
else:
|
else:
|
||||||
await self.async_turn_off()
|
await self.async_turn_off()
|
||||||
@ -195,11 +188,11 @@ class ForkedDaapdZone(MediaPlayerEntity):
|
|||||||
return f"{FD_NAME} output ({self._output['name']})"
|
return f"{FD_NAME} output ({self._output['name']})"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> MediaPlayerState:
|
||||||
"""State of the zone."""
|
"""State of the zone."""
|
||||||
if self._output["selected"]:
|
if self._output["selected"]:
|
||||||
return STATE_ON
|
return MediaPlayerState.ON
|
||||||
return STATE_OFF
|
return MediaPlayerState.OFF
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_level(self):
|
def volume_level(self):
|
||||||
@ -452,7 +445,7 @@ class ForkedDaapdMaster(MediaPlayerEntity):
|
|||||||
Default media player component method counts idle as off.
|
Default media player component method counts idle as off.
|
||||||
We consider idle to be on but just not playing.
|
We consider idle to be on but just not playing.
|
||||||
"""
|
"""
|
||||||
if self.state == STATE_OFF:
|
if self.state == MediaPlayerState.OFF:
|
||||||
await self.async_turn_on()
|
await self.async_turn_on()
|
||||||
else:
|
else:
|
||||||
await self.async_turn_off()
|
await self.async_turn_off()
|
||||||
@ -463,16 +456,17 @@ class ForkedDaapdMaster(MediaPlayerEntity):
|
|||||||
return f"{FD_NAME} server"
|
return f"{FD_NAME} server"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self) -> MediaPlayerState | None:
|
||||||
"""State of the player."""
|
"""State of the player."""
|
||||||
if self._player["state"] == "play":
|
if self._player["state"] == "play":
|
||||||
return STATE_PLAYING
|
return MediaPlayerState.PLAYING
|
||||||
if self._player["state"] == "pause":
|
if self._player["state"] == "pause":
|
||||||
return STATE_PAUSED
|
return MediaPlayerState.PAUSED
|
||||||
if not any(output["selected"] for output in self._outputs):
|
if not any(output["selected"] for output in self._outputs):
|
||||||
return STATE_OFF
|
return MediaPlayerState.OFF
|
||||||
if self._player["state"] == "stop": # this should catch all remaining cases
|
if self._player["state"] == "stop": # this should catch all remaining cases
|
||||||
return STATE_IDLE
|
return MediaPlayerState.IDLE
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_level(self):
|
def volume_level(self):
|
||||||
@ -661,17 +655,17 @@ class ForkedDaapdMaster(MediaPlayerEntity):
|
|||||||
self._paused_event.clear()
|
self._paused_event.clear()
|
||||||
|
|
||||||
async def async_play_media(
|
async def async_play_media(
|
||||||
self, media_type: str, media_id: str, **kwargs: Any
|
self, media_type: MediaType | str, media_id: str, **kwargs: Any
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Play a URI."""
|
"""Play a URI."""
|
||||||
if media_source.is_media_source_id(media_id):
|
if media_source.is_media_source_id(media_id):
|
||||||
media_type = MEDIA_TYPE_MUSIC
|
media_type = MediaType.MUSIC
|
||||||
play_item = await media_source.async_resolve_media(
|
play_item = await media_source.async_resolve_media(
|
||||||
self.hass, media_id, self.entity_id
|
self.hass, media_id, self.entity_id
|
||||||
)
|
)
|
||||||
media_id = play_item.url
|
media_id = play_item.url
|
||||||
|
|
||||||
if media_type == MEDIA_TYPE_MUSIC:
|
if media_type == MediaType.MUSIC:
|
||||||
media_id = async_process_play_media_url(self.hass, media_id)
|
media_id = async_process_play_media_url(self.hass, media_id)
|
||||||
|
|
||||||
saved_state = self.state # save play state
|
saved_state = self.state # save play state
|
||||||
@ -717,7 +711,7 @@ class ForkedDaapdMaster(MediaPlayerEntity):
|
|||||||
await self._api.add_to_queue(
|
await self._api.add_to_queue(
|
||||||
uris=self._sources_uris[self._source], clear=True
|
uris=self._sources_uris[self._source], clear=True
|
||||||
)
|
)
|
||||||
if saved_state == STATE_PLAYING:
|
if saved_state == MediaPlayerState.PLAYING:
|
||||||
await self.async_media_play()
|
await self.async_media_play()
|
||||||
else: # restore stashed queue
|
else: # restore stashed queue
|
||||||
if saved_queue:
|
if saved_queue:
|
||||||
@ -731,9 +725,9 @@ class ForkedDaapdMaster(MediaPlayerEntity):
|
|||||||
clear=True,
|
clear=True,
|
||||||
)
|
)
|
||||||
await self._api.seek(position_ms=saved_song_position)
|
await self._api.seek(position_ms=saved_song_position)
|
||||||
if saved_state == STATE_PAUSED:
|
if saved_state == MediaPlayerState.PAUSED:
|
||||||
await self.async_media_pause()
|
await self.async_media_pause()
|
||||||
elif saved_state != STATE_PLAYING:
|
elif saved_state != MediaPlayerState.PLAYING:
|
||||||
await self.async_media_stop()
|
await self.async_media_stop()
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug("Media type '%s' not supported", media_type)
|
_LOGGER.debug("Media type '%s' not supported", media_type)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user