Add Sonos unjoin functionality (#2379)

This commit is contained in:
Lewis Juggins 2016-06-30 22:21:57 +01:00 committed by Paulus Schoutsen
parent d1f4901d53
commit 21be4c1828
2 changed files with 48 additions and 42 deletions

View File

@ -154,12 +154,20 @@ sonos_group_players:
description: Name(s) of entites that will coordinate the grouping. Platform dependent. description: Name(s) of entites that will coordinate the grouping. Platform dependent.
example: 'media_player.living_room_sonos' example: 'media_player.living_room_sonos'
sonos_unjoin:
description: Unjoin the player from a group.
fields:
entity_id:
description: Name(s) of entites that will be unjoined from their group. Platform dependent.
example: 'media_player.living_room_sonos'
sonos_snapshot: sonos_snapshot:
description: Take a snapshot of the media player. description: Take a snapshot of the media player.
fields: fields:
entity_id: entity_id:
description: Name(s) of entites that will coordinate the grouping. Platform dependent. description: Name(s) of entites that will be snapshot. Platform dependent.
example: 'media_player.living_room_sonos' example: 'media_player.living_room_sonos'
sonos_restore: sonos_restore:
@ -167,5 +175,5 @@ sonos_restore:
fields: fields:
entity_id: entity_id:
description: Name(s) of entites that will coordinate the grouping. Platform dependent. description: Name(s) of entites that will be restored. Platform dependent.
example: 'media_player.living_room_sonos' example: 'media_player.living_room_sonos'

View File

@ -34,11 +34,12 @@ SUPPORT_SONOS = SUPPORT_PAUSE | SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE |\
SUPPORT_SEEK SUPPORT_SEEK
SERVICE_GROUP_PLAYERS = 'sonos_group_players' SERVICE_GROUP_PLAYERS = 'sonos_group_players'
SERVICE_UNJOIN = 'sonos_unjoin'
SERVICE_SNAPSHOT = 'sonos_snapshot' SERVICE_SNAPSHOT = 'sonos_snapshot'
SERVICE_RESTORE = 'sonos_restore' SERVICE_RESTORE = 'sonos_restore'
# pylint: disable=unused-argument # pylint: disable=unused-argument, too-many-locals
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Sonos platform.""" """Setup the Sonos platform."""
import soco import soco
@ -72,47 +73,35 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
add_devices(devices) add_devices(devices)
_LOGGER.info('Added %s Sonos speakers', len(players)) _LOGGER.info('Added %s Sonos speakers', len(players))
def _apply_service(service, service_func, *service_func_args):
"""Internal func for applying a service."""
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:
service_func(device, *service_func_args)
device.update_ha_state(True)
def group_players_service(service): def group_players_service(service):
"""Group media players, use player as coordinator.""" """Group media players, use player as coordinator."""
entity_id = service.data.get('entity_id') _apply_service(service, SonosDevice.group_players)
if entity_id: def unjoin_service(service):
_devices = [device for device in devices """Unjoin the player from a group."""
if device.entity_id == entity_id] _apply_service(service, SonosDevice.unjoin)
else:
_devices = devices
for device in _devices: def snapshot_service(service):
device.group_players()
device.update_ha_state(True)
def snapshot(service):
"""Take a snapshot.""" """Take a snapshot."""
entity_id = service.data.get('entity_id') _apply_service(service, SonosDevice.snapshot)
if entity_id: def restore_service(service):
_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.""" """Restore a snapshot."""
entity_id = service.data.get('entity_id') _apply_service(service, SonosDevice.restore)
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( descriptions = load_yaml_config_file(
path.join(path.dirname(__file__), 'services.yaml')) path.join(path.dirname(__file__), 'services.yaml'))
@ -121,12 +110,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
group_players_service, group_players_service,
descriptions.get(SERVICE_GROUP_PLAYERS)) descriptions.get(SERVICE_GROUP_PLAYERS))
hass.services.register(DOMAIN, SERVICE_UNJOIN,
unjoin_service,
descriptions.get(SERVICE_UNJOIN))
hass.services.register(DOMAIN, SERVICE_SNAPSHOT, hass.services.register(DOMAIN, SERVICE_SNAPSHOT,
snapshot, snapshot_service,
descriptions.get(SERVICE_SNAPSHOT)) descriptions.get(SERVICE_SNAPSHOT))
hass.services.register(DOMAIN, SERVICE_RESTORE, hass.services.register(DOMAIN, SERVICE_RESTORE,
restore, restore_service,
descriptions.get(SERVICE_RESTORE)) descriptions.get(SERVICE_RESTORE))
return True return True
@ -356,12 +349,17 @@ class SonosDevice(MediaPlayerDevice):
self._player.partymode() self._player.partymode()
@only_if_coordinator @only_if_coordinator
def snapshot(self, service): def unjoin(self):
"""Unjoin the player from a group."""
self._player.unjoin()
@only_if_coordinator
def snapshot(self):
"""Snapshot the player.""" """Snapshot the player."""
self.soco_snapshot.snapshot() self.soco_snapshot.snapshot()
@only_if_coordinator @only_if_coordinator
def restore(self, service): def restore(self):
"""Restore snapshot for the player.""" """Restore snapshot for the player."""
self.soco_snapshot.restore(True) self.soco_snapshot.restore(True)