From ce829d194cf2ddcc4168f144c2552b01366b86cd Mon Sep 17 00:00:00 2001 From: Dan Sullivan Date: Thu, 9 Jun 2016 05:47:49 +0100 Subject: [PATCH] Added Sonos snapshot feature (#2240) * Added Sonos snapshot feature * Fix lint errors * Use snake case * Import dependency in a method --- .../components/media_player/services.yaml | 16 ++++++ .../components/media_player/sonos.py | 50 +++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/homeassistant/components/media_player/services.yaml b/homeassistant/components/media_player/services.yaml index 5f1f35f065f..d1ef92ee4d5 100644 --- a/homeassistant/components/media_player/services.yaml +++ b/homeassistant/components/media_player/services.yaml @@ -153,3 +153,19 @@ sonos_group_players: entity_id: description: Name(s) of entites that will coordinate the grouping. Platform dependent. example: 'media_player.living_room_sonos' + +sonos_snapshot: + description: Take a snapshot of the media player. + + fields: + entity_id: + description: Name(s) of entites that will coordinate the grouping. Platform dependent. + example: 'media_player.living_room_sonos' + +sonos_restore: + description: Restore a snapshot of the media player. + + fields: + entity_id: + description: Name(s) of entites that will coordinate the grouping. Platform dependent. + example: 'media_player.living_room_sonos' \ No newline at end of file diff --git a/homeassistant/components/media_player/sonos.py b/homeassistant/components/media_player/sonos.py index 01e3f8d9efc..9239f1edae8 100644 --- a/homeassistant/components/media_player/sonos.py +++ b/homeassistant/components/media_player/sonos.py @@ -34,6 +34,8 @@ SUPPORT_SONOS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |\ SUPPORT_SEEK SERVICE_GROUP_PLAYERS = 'sonos_group_players' +SERVICE_SNAPSHOT = 'sonos_snapshot' +SERVICE_RESTORE = 'sonos_restore' # pylint: disable=unused-argument @@ -84,6 +86,34 @@ def setup_platform(hass, config, add_devices, discovery_info=None): device.group_players() device.update_ha_state(True) + def snapshot(service): + """Take a snapshot.""" + entity_id = service.data.get('entity_id') + + if entity_id: + _devices = [device for device in devices + if device.entity_id == entity_id] + else: + _devices = devices + + for device in _devices: + device.snapshot(service) + device.update_ha_state(True) + + def restore(service): + """Restore a snapshot.""" + entity_id = service.data.get('entity_id') + + if entity_id: + _devices = [device for device in devices + if device.entity_id == entity_id] + else: + _devices = devices + + for device in _devices: + device.restore(service) + device.update_ha_state(True) + descriptions = load_yaml_config_file( path.join(path.dirname(__file__), 'services.yaml')) @@ -91,6 +121,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None): group_players_service, descriptions.get(SERVICE_GROUP_PLAYERS)) + hass.services.register(DOMAIN, SERVICE_SNAPSHOT, + snapshot, + descriptions.get(SERVICE_SNAPSHOT)) + + hass.services.register(DOMAIN, SERVICE_RESTORE, + restore, + descriptions.get(SERVICE_RESTORE)) + return True @@ -136,6 +174,8 @@ class SonosDevice(MediaPlayerDevice): super(SonosDevice, self).__init__() self._player = player self.update() + from soco.snapshot import Snapshot + self.soco_snapshot = Snapshot(self._player) @property def should_poll(self): @@ -315,6 +355,16 @@ class SonosDevice(MediaPlayerDevice): """Group all players under this coordinator.""" self._player.partymode() + @only_if_coordinator + def snapshot(self, service): + """Snapshot the player.""" + self.soco_snapshot.snapshot() + + @only_if_coordinator + def restore(self, service): + """Restore snapshot for the player.""" + self.soco_snapshot.restore(True) + @property def available(self): """Return True if player is reachable, False otherwise."""