mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
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
@ -334,13 +334,41 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
self._speaker_info = self._player.get_speaker_info(True)
|
self._speaker_info = self._player.get_speaker_info(True)
|
||||||
self._name = self._speaker_info['zone_name'].replace(
|
self._name = self._speaker_info['zone_name'].replace(
|
||||||
' (R)', '').replace(' (L)', '')
|
' (R)', '').replace(' (L)', '')
|
||||||
|
self._favorite_sources = \
|
||||||
|
self._player.get_sonos_favorites()['favorites']
|
||||||
|
|
||||||
if self._last_avtransport_event:
|
if self._last_avtransport_event:
|
||||||
is_available = True
|
is_available = True
|
||||||
else:
|
else:
|
||||||
is_available = self._is_available()
|
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
|
# set group coordinator
|
||||||
if self._player.is_coordinator:
|
if self._player.is_coordinator:
|
||||||
@ -387,7 +415,9 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
if not track_info:
|
if not track_info:
|
||||||
track_info = self._player.get_current_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_tv = self._player.is_playing_tv
|
||||||
is_playing_line_in = self._player.is_playing_line_in
|
is_playing_line_in = self._player.is_playing_line_in
|
||||||
@ -441,10 +471,8 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
source_name = 'Radio'
|
source_name = 'Radio'
|
||||||
# Check if currently playing radio station is in favorites
|
# Check if currently playing radio station is in favorites
|
||||||
favs = self._player.get_sonos_favorites()['favorites']
|
favc = [fav for fav in self._favorite_sources
|
||||||
favc = [
|
if fav['uri'] == current_media_uri]
|
||||||
fav for fav in favs if fav['uri'] == current_media_uri
|
|
||||||
]
|
|
||||||
if len(favc) == 1:
|
if len(favc) == 1:
|
||||||
src = favc.pop()
|
src = favc.pop()
|
||||||
source_name = src['title']
|
source_name = src['title']
|
||||||
@ -465,8 +493,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
media_artist = self._media_radio_show
|
media_artist = self._media_radio_show
|
||||||
|
|
||||||
current_uri_metadata = media_info["CurrentURIMetaData"]
|
current_uri_metadata = media_info["CurrentURIMetaData"]
|
||||||
if current_uri_metadata not in \
|
if current_uri_metadata not in ('', 'NOT_IMPLEMENTED', None):
|
||||||
('', 'NOT_IMPLEMENTED', None):
|
|
||||||
|
|
||||||
# currently soco does not have an API for this
|
# currently soco does not have an API for this
|
||||||
import soco
|
import soco
|
||||||
@ -491,9 +518,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
)
|
)
|
||||||
chars = min(len(media_artist), len(str_to_trim))
|
chars = min(len(media_artist), len(str_to_trim))
|
||||||
|
|
||||||
if media_artist[:chars].upper() == \
|
if media_artist[:chars].upper() == str_to_trim[:chars].upper():
|
||||||
str_to_trim[:chars].upper():
|
|
||||||
|
|
||||||
media_artist = media_artist[chars:]
|
media_artist = media_artist[chars:]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@ -524,15 +549,12 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
self._media_position is None
|
self._media_position is None
|
||||||
|
|
||||||
# position changed?
|
# position changed?
|
||||||
if rel_time is not None and \
|
if rel_time is not None and self._media_position is not None:
|
||||||
self._media_position is not None:
|
|
||||||
|
|
||||||
time_diff = utcnow() - self._media_position_updated_at
|
time_diff = utcnow() - self._media_position_updated_at
|
||||||
time_diff = time_diff.total_seconds()
|
time_diff = time_diff.total_seconds()
|
||||||
|
|
||||||
calculated_position = \
|
calculated_position = self._media_position + time_diff
|
||||||
self._media_position + \
|
|
||||||
time_diff
|
|
||||||
|
|
||||||
update_media_position = \
|
update_media_position = \
|
||||||
abs(calculated_position - rel_time) > 1.5
|
abs(calculated_position - rel_time) > 1.5
|
||||||
@ -544,8 +566,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
# don't update media_position (don't want unneeded
|
# don't update media_position (don't want unneeded
|
||||||
# state transitions)
|
# state transitions)
|
||||||
media_position = self._media_position
|
media_position = self._media_position
|
||||||
media_position_updated_at = \
|
media_position_updated_at = self._media_position_updated_at
|
||||||
self._media_position_updated_at
|
|
||||||
|
|
||||||
playlist_position = track_info.get('playlist_position')
|
playlist_position = track_info.get('playlist_position')
|
||||||
if playlist_position in ('', 'NOT_IMPLEMENTED', None):
|
if playlist_position in ('', 'NOT_IMPLEMENTED', None):
|
||||||
@ -559,8 +580,7 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
else:
|
else:
|
||||||
playlist_size = int(playlist_size)
|
playlist_size = int(playlist_size)
|
||||||
|
|
||||||
if playlist_position is not None and \
|
if playlist_position is not None and playlist_size is not None:
|
||||||
playlist_size is not None:
|
|
||||||
|
|
||||||
if playlist_position == 1:
|
if playlist_position == 1:
|
||||||
support_previous_track = False
|
support_previous_track = False
|
||||||
@ -596,33 +616,6 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
if self._queue is None and self.entity_id is not None:
|
if self._queue is None and self.entity_id is not None:
|
||||||
self._subscribe_to_player_events()
|
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
|
self._last_avtransport_event = None
|
||||||
|
|
||||||
@ -807,15 +800,17 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
def select_source(self, source):
|
def select_source(self, source):
|
||||||
"""Select input 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._source_name = SUPPORT_SOURCE_LINEIN
|
||||||
self._player.switch_to_line_in()
|
self._player.switch_to_line_in()
|
||||||
elif source == SUPPORT_SOURCE_TV:
|
elif source == SUPPORT_SOURCE_TV:
|
||||||
self._source_name = SUPPORT_SOURCE_TV
|
self._source_name = SUPPORT_SOURCE_TV
|
||||||
self._player.switch_to_tv()
|
self._player.switch_to_tv()
|
||||||
else:
|
else:
|
||||||
favorites = self._player.get_sonos_favorites()['favorites']
|
fav = [fav for fav in self._favorite_sources
|
||||||
fav = [fav for fav in favorites if fav['title'] == source]
|
if fav['title'] == source]
|
||||||
if len(fav) == 1:
|
if len(fav) == 1:
|
||||||
src = fav.pop()
|
src = fav.pop()
|
||||||
self._source_name = src['title']
|
self._source_name = src['title']
|
||||||
@ -824,9 +819,15 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
@property
|
@property
|
||||||
def source_list(self):
|
def source_list(self):
|
||||||
"""List of available input sources."""
|
"""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:
|
if 'PLAY:5' in model_name:
|
||||||
sources += [SUPPORT_SOURCE_LINEIN]
|
sources += [SUPPORT_SOURCE_LINEIN]
|
||||||
@ -940,7 +941,15 @@ class SonosDevice(MediaPlayerDevice):
|
|||||||
|
|
||||||
def snapshot(self, with_group=True):
|
def snapshot(self, with_group=True):
|
||||||
"""Snapshot the player."""
|
"""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()
|
self.soco_snapshot.snapshot()
|
||||||
|
except SoCoException:
|
||||||
|
_LOGGER.debug("Error on snapshot %s", self.entity_id)
|
||||||
|
self._snapshot_group = None
|
||||||
|
return
|
||||||
|
|
||||||
if with_group:
|
if with_group:
|
||||||
self._snapshot_group = self._player.group
|
self._snapshot_group = self._player.group
|
||||||
|
@ -52,6 +52,10 @@ class SoCoMock():
|
|||||||
"""Clear the sleep timer."""
|
"""Clear the sleep timer."""
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def get_sonos_favorites(self):
|
||||||
|
"""Get favorites list from sonos."""
|
||||||
|
return {'favorites': []}
|
||||||
|
|
||||||
def get_speaker_info(self, force):
|
def get_speaker_info(self, force):
|
||||||
"""Return a dict with various data points about the speaker."""
|
"""Return a dict with various data points about the speaker."""
|
||||||
return {'serial_number': 'B8-E9-37-BO-OC-BA:2',
|
return {'serial_number': 'B8-E9-37-BO-OC-BA:2',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user