Use _attr in pandora media player (#82835)

This commit is contained in:
epenet 2022-11-28 10:47:11 +01:00 committed by GitHub
parent 19abba7f6b
commit ac896121d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,30 +81,20 @@ class PandoraMediaPlayer(MediaPlayerEntity):
def __init__(self, name): def __init__(self, name):
"""Initialize the Pandora device.""" """Initialize the Pandora device."""
self._name = name self._attr_name = name
self._player_state = MediaPlayerState.OFF self._attr_state = MediaPlayerState.OFF
self._station = "" self._attr_source = ""
self._media_title = "" self._attr_media_title = ""
self._media_artist = "" self._attr_media_artist = ""
self._media_album = "" self._attr_media_album_name = ""
self._stations = [] self._attr_source_list = []
self._time_remaining = 0 self._time_remaining = 0
self._media_duration = 0 self._attr_media_duration = 0
self._pianobar = None self._pianobar = None
@property
def name(self):
"""Return the name of the media player."""
return self._name
@property
def state(self):
"""Return the state of the player."""
return self._player_state
def turn_on(self) -> None: def turn_on(self) -> None:
"""Turn the media player on.""" """Turn the media player on."""
if self._player_state != MediaPlayerState.OFF: if self.state != MediaPlayerState.OFF:
return return
self._pianobar = pexpect.spawn("pianobar") self._pianobar = pexpect.spawn("pianobar")
_LOGGER.info("Started pianobar subprocess") _LOGGER.info("Started pianobar subprocess")
@ -129,7 +119,7 @@ class PandoraMediaPlayer(MediaPlayerEntity):
self._update_stations() self._update_stations()
self.update_playing_status() self.update_playing_status()
self._player_state = MediaPlayerState.IDLE self._attr_state = MediaPlayerState.IDLE
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self) -> None: def turn_off(self) -> None:
@ -146,19 +136,19 @@ class PandoraMediaPlayer(MediaPlayerEntity):
os.killpg(os.getpgid(self._pianobar.pid), signal.SIGTERM) os.killpg(os.getpgid(self._pianobar.pid), signal.SIGTERM)
_LOGGER.debug("Killed Pianobar subprocess") _LOGGER.debug("Killed Pianobar subprocess")
self._pianobar = None self._pianobar = None
self._player_state = MediaPlayerState.OFF self._attr_state = MediaPlayerState.OFF
self.schedule_update_ha_state() self.schedule_update_ha_state()
def media_play(self) -> None: def media_play(self) -> None:
"""Send play command.""" """Send play command."""
self._send_pianobar_command(SERVICE_MEDIA_PLAY_PAUSE) self._send_pianobar_command(SERVICE_MEDIA_PLAY_PAUSE)
self._player_state = MediaPlayerState.PLAYING self._attr_state = MediaPlayerState.PLAYING
self.schedule_update_ha_state() self.schedule_update_ha_state()
def media_pause(self) -> None: def media_pause(self) -> None:
"""Send pause command.""" """Send pause command."""
self._send_pianobar_command(SERVICE_MEDIA_PLAY_PAUSE) self._send_pianobar_command(SERVICE_MEDIA_PLAY_PAUSE)
self._player_state = MediaPlayerState.PAUSED self._attr_state = MediaPlayerState.PAUSED
self.schedule_update_ha_state() self.schedule_update_ha_state()
def media_next_track(self) -> None: def media_next_track(self) -> None:
@ -167,40 +157,17 @@ class PandoraMediaPlayer(MediaPlayerEntity):
self.schedule_update_ha_state() self.schedule_update_ha_state()
@property @property
def source(self): def media_title(self) -> str | None:
"""Name of the current input source."""
return self._station
@property
def source_list(self):
"""List of available input sources."""
return self._stations
@property
def media_title(self):
"""Title of current playing media.""" """Title of current playing media."""
self.update_playing_status() self.update_playing_status()
return self._media_title return self._attr_media_title
@property
def media_artist(self):
"""Artist of current playing media, music track only."""
return self._media_artist
@property
def media_album_name(self):
"""Album name of current playing media, music track only."""
return self._media_album
@property
def media_duration(self):
"""Duration of current playing media in seconds."""
return self._media_duration
def select_source(self, source: str) -> None: def select_source(self, source: str) -> None:
"""Choose a different Pandora station and play it.""" """Choose a different Pandora station and play it."""
if self.source_list is None:
return
try: try:
station_index = self._stations.index(source) station_index = self.source_list.index(source)
except ValueError: except ValueError:
_LOGGER.warning("Station %s is not in list", source) _LOGGER.warning("Station %s is not in list", source)
return return
@ -208,7 +175,7 @@ class PandoraMediaPlayer(MediaPlayerEntity):
self._send_station_list_command() self._send_station_list_command()
self._pianobar.sendline(f"{station_index}") self._pianobar.sendline(f"{station_index}")
self._pianobar.expect("\r\n") self._pianobar.expect("\r\n")
self._player_state = MediaPlayerState.PLAYING self._attr_state = MediaPlayerState.PLAYING
def _send_station_list_command(self): def _send_station_list_command(self):
"""Send a station list command.""" """Send a station list command."""
@ -269,8 +236,8 @@ class PandoraMediaPlayer(MediaPlayerEntity):
def _update_current_station(self, response): def _update_current_station(self, response):
"""Update current station.""" """Update current station."""
if station_match := re.search(STATION_PATTERN, response): if station_match := re.search(STATION_PATTERN, response):
self._station = station_match.group(1) self._attr_source = station_match.group(1)
_LOGGER.debug("Got station as: %s", self._station) _LOGGER.debug("Got station as: %s", self._attr_source)
else: else:
_LOGGER.warning("No station match") _LOGGER.warning("No station match")
@ -278,11 +245,11 @@ class PandoraMediaPlayer(MediaPlayerEntity):
"""Update info about current song.""" """Update info about current song."""
if song_match := re.search(CURRENT_SONG_PATTERN, response): if song_match := re.search(CURRENT_SONG_PATTERN, response):
( (
self._media_title, self._attr_media_title,
self._media_artist, self._attr_media_artist,
self._media_album, self._attr_media_album_name,
) = song_match.groups() ) = song_match.groups()
_LOGGER.debug("Got song as: %s", self._media_title) _LOGGER.debug("Got song as: %s", self._attr_media_title)
else: else:
_LOGGER.warning("No song match") _LOGGER.warning("No song match")
@ -302,12 +269,12 @@ class PandoraMediaPlayer(MediaPlayerEntity):
total_seconds, total_seconds,
) = self._pianobar.match.groups() ) = self._pianobar.match.groups()
time_remaining = int(cur_minutes) * 60 + int(cur_seconds) time_remaining = int(cur_minutes) * 60 + int(cur_seconds)
self._media_duration = int(total_minutes) * 60 + int(total_seconds) self._attr_media_duration = int(total_minutes) * 60 + int(total_seconds)
if time_remaining not in (self._time_remaining, self._media_duration): if time_remaining not in (self._time_remaining, self._attr_media_duration):
self._player_state = MediaPlayerState.PLAYING self._attr_state = MediaPlayerState.PLAYING
elif self._player_state == MediaPlayerState.PLAYING: elif self.state == MediaPlayerState.PLAYING:
self._player_state = MediaPlayerState.PAUSED self._attr_state = MediaPlayerState.PAUSED
self._time_remaining = time_remaining self._time_remaining = time_remaining
def _log_match(self): def _log_match(self):
@ -333,12 +300,12 @@ class PandoraMediaPlayer(MediaPlayerEntity):
self._send_station_list_command() self._send_station_list_command()
station_lines = self._pianobar.before.decode("utf-8") station_lines = self._pianobar.before.decode("utf-8")
_LOGGER.debug("Getting stations: %s", station_lines) _LOGGER.debug("Getting stations: %s", station_lines)
self._stations = [] self._attr_source_list = []
for line in station_lines.split("\r\n"): for line in station_lines.split("\r\n"):
if match := re.search(r"\d+\).....(.+)", line): if match := re.search(r"\d+\).....(.+)", line):
station = match.group(1).strip() station = match.group(1).strip()
_LOGGER.debug("Found station %s", station) _LOGGER.debug("Found station %s", station)
self._stations.append(station) self._attr_source_list.append(station)
else: else:
_LOGGER.debug("No station match on %s", line) _LOGGER.debug("No station match on %s", line)
self._pianobar.sendcontrol("m") # press enter with blank line self._pianobar.sendcontrol("m") # press enter with blank line