Sonos fix favorite, coordinator, cleanup update (#5778)
* Sonos fix favorite, coordinator, cleanup update * Bugfix snapshot restore
This commit is contained in:
parent
7927a6b588
commit
48161697f8
2 changed files with 278 additions and 265 deletions
|
@ -334,13 +334,41 @@ class SonosDevice(MediaPlayerDevice):
|
|||
self._speaker_info = self._player.get_speaker_info(True)
|
||||
self._name = self._speaker_info['zone_name'].replace(
|
||||
' (R)', '').replace(' (L)', '')
|
||||
self._favorite_sources = \
|
||||
self._player.get_sonos_favorites()['favorites']
|
||||
|
||||
if self._last_avtransport_event:
|
||||
is_available = True
|
||||
else:
|
||||
is_available = self._is_available()
|
||||
|
||||
if is_available:
|
||||
if not is_available:
|
||||
self._player_volume = None
|
||||
self._player_volume_muted = None
|
||||
self._status = 'OFF'
|
||||
self._coordinator = None
|
||||
self._media_content_id = None
|
||||
self._media_duration = None
|
||||
self._media_position = None
|
||||
self._media_position_updated_at = None
|
||||
self._media_image_url = None
|
||||
self._media_artist = None
|
||||
self._media_album_name = None
|
||||
self._media_title = None
|
||||
self._media_radio_show = None
|
||||
self._media_next_title = None
|
||||
self._current_track_uri = None
|
||||
self._current_track_is_radio_stream = False
|
||||
self._support_previous_track = False
|
||||
self._support_next_track = False
|
||||
self._support_stop = False
|
||||
self._support_pause = False
|
||||
self._is_playing_tv = False
|
||||
self._is_playing_line_in = False
|
||||
self._favorite_sources = None
|
||||
self._source_name = None
|
||||
self._last_avtransport_event = None
|
||||
return
|
||||
|
||||
# set group coordinator
|
||||
if self._player.is_coordinator:
|
||||
|
@ -387,7 +415,9 @@ class SonosDevice(MediaPlayerDevice):
|
|||
if not track_info:
|
||||
track_info = self._player.get_current_track_info()
|
||||
|
||||
if not self._coordinator:
|
||||
if self._coordinator:
|
||||
self._last_avtransport_event = None
|
||||
return
|
||||
|
||||
is_playing_tv = self._player.is_playing_tv
|
||||
is_playing_line_in = self._player.is_playing_line_in
|
||||
|
@ -441,10 +471,8 @@ class SonosDevice(MediaPlayerDevice):
|
|||
|
||||
source_name = 'Radio'
|
||||
# Check if currently playing radio station is in favorites
|
||||
favs = self._player.get_sonos_favorites()['favorites']
|
||||
favc = [
|
||||
fav for fav in favs if fav['uri'] == current_media_uri
|
||||
]
|
||||
favc = [fav for fav in self._favorite_sources
|
||||
if fav['uri'] == current_media_uri]
|
||||
if len(favc) == 1:
|
||||
src = favc.pop()
|
||||
source_name = src['title']
|
||||
|
@ -465,8 +493,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||
media_artist = self._media_radio_show
|
||||
|
||||
current_uri_metadata = media_info["CurrentURIMetaData"]
|
||||
if current_uri_metadata not in \
|
||||
('', 'NOT_IMPLEMENTED', None):
|
||||
if current_uri_metadata not in ('', 'NOT_IMPLEMENTED', None):
|
||||
|
||||
# currently soco does not have an API for this
|
||||
import soco
|
||||
|
@ -491,9 +518,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||
)
|
||||
chars = min(len(media_artist), len(str_to_trim))
|
||||
|
||||
if media_artist[:chars].upper() == \
|
||||
str_to_trim[:chars].upper():
|
||||
|
||||
if media_artist[:chars].upper() == str_to_trim[:chars].upper():
|
||||
media_artist = media_artist[chars:]
|
||||
|
||||
else:
|
||||
|
@ -524,15 +549,12 @@ class SonosDevice(MediaPlayerDevice):
|
|||
self._media_position is None
|
||||
|
||||
# position changed?
|
||||
if rel_time is not None and \
|
||||
self._media_position is not None:
|
||||
if rel_time is not None and self._media_position is not None:
|
||||
|
||||
time_diff = utcnow() - self._media_position_updated_at
|
||||
time_diff = time_diff.total_seconds()
|
||||
|
||||
calculated_position = \
|
||||
self._media_position + \
|
||||
time_diff
|
||||
calculated_position = self._media_position + time_diff
|
||||
|
||||
update_media_position = \
|
||||
abs(calculated_position - rel_time) > 1.5
|
||||
|
@ -544,8 +566,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||
# don't update media_position (don't want unneeded
|
||||
# state transitions)
|
||||
media_position = self._media_position
|
||||
media_position_updated_at = \
|
||||
self._media_position_updated_at
|
||||
media_position_updated_at = self._media_position_updated_at
|
||||
|
||||
playlist_position = track_info.get('playlist_position')
|
||||
if playlist_position in ('', 'NOT_IMPLEMENTED', None):
|
||||
|
@ -559,8 +580,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||
else:
|
||||
playlist_size = int(playlist_size)
|
||||
|
||||
if playlist_position is not None and \
|
||||
playlist_size is not None:
|
||||
if playlist_position is not None and playlist_size is not None:
|
||||
|
||||
if playlist_position == 1:
|
||||
support_previous_track = False
|
||||
|
@ -596,33 +616,6 @@ class SonosDevice(MediaPlayerDevice):
|
|||
|
||||
if self._queue is None and self.entity_id is not None:
|
||||
self._subscribe_to_player_events()
|
||||
favs = self._player.get_sonos_favorites().get('favorites', [])
|
||||
self._favorite_sources = [fav['title'] for fav in favs]
|
||||
else:
|
||||
self._player_volume = None
|
||||
self._player_volume_muted = None
|
||||
self._status = 'OFF'
|
||||
self._coordinator = None
|
||||
self._media_content_id = None
|
||||
self._media_duration = None
|
||||
self._media_position = None
|
||||
self._media_position_updated_at = None
|
||||
self._media_image_url = None
|
||||
self._media_artist = None
|
||||
self._media_album_name = None
|
||||
self._media_title = None
|
||||
self._media_radio_show = None
|
||||
self._media_next_title = None
|
||||
self._current_track_uri = None
|
||||
self._current_track_is_radio_stream = False
|
||||
self._support_previous_track = False
|
||||
self._support_next_track = False
|
||||
self._support_stop = False
|
||||
self._support_pause = False
|
||||
self._is_playing_tv = False
|
||||
self._is_playing_line_in = False
|
||||
self._favorite_sources = None
|
||||
self._source_name = None
|
||||
|
||||
self._last_avtransport_event = None
|
||||
|
||||
|
@ -807,15 +800,17 @@ class SonosDevice(MediaPlayerDevice):
|
|||
|
||||
def select_source(self, source):
|
||||
"""Select input source."""
|
||||
if source == SUPPORT_SOURCE_LINEIN:
|
||||
if self._coordinator:
|
||||
self._coordinator.select_source(source)
|
||||
elif source == SUPPORT_SOURCE_LINEIN:
|
||||
self._source_name = SUPPORT_SOURCE_LINEIN
|
||||
self._player.switch_to_line_in()
|
||||
elif source == SUPPORT_SOURCE_TV:
|
||||
self._source_name = SUPPORT_SOURCE_TV
|
||||
self._player.switch_to_tv()
|
||||
else:
|
||||
favorites = self._player.get_sonos_favorites()['favorites']
|
||||
fav = [fav for fav in favorites if fav['title'] == source]
|
||||
fav = [fav for fav in self._favorite_sources
|
||||
if fav['title'] == source]
|
||||
if len(fav) == 1:
|
||||
src = fav.pop()
|
||||
self._source_name = src['title']
|
||||
|
@ -824,9 +819,15 @@ class SonosDevice(MediaPlayerDevice):
|
|||
@property
|
||||
def source_list(self):
|
||||
"""List of available input sources."""
|
||||
model_name = self._speaker_info['model_name']
|
||||
if self._coordinator:
|
||||
return self._coordinator.source_list
|
||||
|
||||
sources = self._favorite_sources.copy()
|
||||
model_name = self._speaker_info['model_name']
|
||||
sources = []
|
||||
|
||||
if self._favorite_sources:
|
||||
for fav in self._favorite_sources:
|
||||
sources.append(fav['title'])
|
||||
|
||||
if 'PLAY:5' in model_name:
|
||||
sources += [SUPPORT_SOURCE_LINEIN]
|
||||
|
@ -940,7 +941,15 @@ class SonosDevice(MediaPlayerDevice):
|
|||
|
||||
def snapshot(self, with_group=True):
|
||||
"""Snapshot the player."""
|
||||
from soco.exceptions import SoCoException
|
||||
try:
|
||||
self.soco_snapshot.is_playing_queue = False
|
||||
self.soco_snapshot.is_coordinator = False
|
||||
self.soco_snapshot.snapshot()
|
||||
except SoCoException:
|
||||
_LOGGER.debug("Error on snapshot %s", self.entity_id)
|
||||
self._snapshot_group = None
|
||||
return
|
||||
|
||||
if with_group:
|
||||
self._snapshot_group = self._player.group
|
||||
|
|
|
@ -52,6 +52,10 @@ class SoCoMock():
|
|||
"""Clear the sleep timer."""
|
||||
return
|
||||
|
||||
def get_sonos_favorites(self):
|
||||
"""Get favorites list from sonos."""
|
||||
return {'favorites': []}
|
||||
|
||||
def get_speaker_info(self, force):
|
||||
"""Return a dict with various data points about the speaker."""
|
||||
return {'serial_number': 'B8-E9-37-BO-OC-BA:2',
|
||||
|
|
Loading…
Add table
Reference in a new issue