mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Fix services registration, and adding schema util to sonos (#2558)
* Moving service registration into def so that it can be called for both discovery methods * Adding use of schemas to sonos
This commit is contained in:
parent
aa079625d4
commit
46216c3bda
@ -6,8 +6,9 @@ https://home-assistant.io/components/media_player.sonos/
|
|||||||
"""
|
"""
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
import socket
|
|
||||||
from os import path
|
from os import path
|
||||||
|
import socket
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
ATTR_MEDIA_ENQUEUE, DOMAIN, MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK,
|
ATTR_MEDIA_ENQUEUE, DOMAIN, MEDIA_TYPE_MUSIC, SUPPORT_NEXT_TRACK,
|
||||||
@ -15,8 +16,10 @@ from homeassistant.components.media_player import (
|
|||||||
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, SUPPORT_CLEAR_PLAYLIST,
|
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, SUPPORT_CLEAR_PLAYLIST,
|
||||||
SUPPORT_SELECT_SOURCE, MediaPlayerDevice)
|
SUPPORT_SELECT_SOURCE, MediaPlayerDevice)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_IDLE, STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN, STATE_OFF)
|
STATE_IDLE, STATE_PAUSED, STATE_PLAYING, STATE_UNKNOWN, STATE_OFF,
|
||||||
|
ATTR_ENTITY_ID)
|
||||||
from homeassistant.config import load_yaml_config_file
|
from homeassistant.config import load_yaml_config_file
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
REQUIREMENTS = ['SoCo==0.11.1']
|
REQUIREMENTS = ['SoCo==0.11.1']
|
||||||
|
|
||||||
@ -43,16 +46,28 @@ SUPPORT_SOURCE_LINEIN = 'Line-in'
|
|||||||
SUPPORT_SOURCE_TV = 'TV'
|
SUPPORT_SOURCE_TV = 'TV'
|
||||||
SUPPORT_SOURCE_RADIO = 'Radio'
|
SUPPORT_SOURCE_RADIO = 'Radio'
|
||||||
|
|
||||||
|
SONOS_SCHEMA = vol.Schema({
|
||||||
|
ATTR_ENTITY_ID: cv.entity_ids,
|
||||||
|
})
|
||||||
|
|
||||||
|
# List of devices that have been registered
|
||||||
|
DEVICES = []
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument, too-many-locals
|
# 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
|
||||||
|
global DEVICES
|
||||||
|
|
||||||
if discovery_info:
|
if discovery_info:
|
||||||
player = soco.SoCo(discovery_info)
|
player = soco.SoCo(discovery_info)
|
||||||
if player.is_visible:
|
if player.is_visible:
|
||||||
add_devices([SonosDevice(hass, player)])
|
device = SonosDevice(hass, player)
|
||||||
|
add_devices([device])
|
||||||
|
if not DEVICES:
|
||||||
|
register_services(hass)
|
||||||
|
DEVICES.append(device)
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -74,61 +89,73 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
_LOGGER.warning('No Sonos speakers found.')
|
_LOGGER.warning('No Sonos speakers found.')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
devices = [SonosDevice(hass, p) for p in players]
|
DEVICES = [SonosDevice(hass, p) for p in players]
|
||||||
add_devices(devices)
|
add_devices(DEVICES)
|
||||||
|
register_services(hass)
|
||||||
_LOGGER.info('Added %s Sonos speakers', len(players))
|
_LOGGER.info('Added %s Sonos speakers', len(players))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def register_services(hass):
|
||||||
|
"""Register all services for sonos devices."""
|
||||||
|
descriptions = load_yaml_config_file(
|
||||||
|
path.join(path.dirname(__file__), 'services.yaml'))
|
||||||
|
|
||||||
|
hass.services.register(DOMAIN, SERVICE_GROUP_PLAYERS,
|
||||||
|
_group_players_service,
|
||||||
|
descriptions.get(SERVICE_GROUP_PLAYERS),
|
||||||
|
schema=SONOS_SCHEMA)
|
||||||
|
|
||||||
|
hass.services.register(DOMAIN, SERVICE_UNJOIN,
|
||||||
|
_unjoin_service,
|
||||||
|
descriptions.get(SERVICE_UNJOIN),
|
||||||
|
schema=SONOS_SCHEMA)
|
||||||
|
|
||||||
|
hass.services.register(DOMAIN, SERVICE_SNAPSHOT,
|
||||||
|
_snapshot_service,
|
||||||
|
descriptions.get(SERVICE_SNAPSHOT),
|
||||||
|
schema=SONOS_SCHEMA)
|
||||||
|
|
||||||
|
hass.services.register(DOMAIN, SERVICE_RESTORE,
|
||||||
|
_restore_service,
|
||||||
|
descriptions.get(SERVICE_RESTORE),
|
||||||
|
schema=SONOS_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
def _apply_service(service, service_func, *service_func_args):
|
def _apply_service(service, service_func, *service_func_args):
|
||||||
"""Internal func for applying a service."""
|
"""Internal func for applying a service."""
|
||||||
entity_id = service.data.get('entity_id')
|
entity_ids = service.data.get('entity_id')
|
||||||
|
|
||||||
if entity_id:
|
if entity_ids:
|
||||||
_devices = [device for device in devices
|
_devices = [device for device in DEVICES
|
||||||
if device.entity_id == entity_id]
|
if device.entity_id in entity_ids]
|
||||||
else:
|
else:
|
||||||
_devices = devices
|
_devices = DEVICES
|
||||||
|
|
||||||
for device in _devices:
|
for device in _devices:
|
||||||
service_func(device, *service_func_args)
|
service_func(device, *service_func_args)
|
||||||
device.update_ha_state(True)
|
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."""
|
||||||
_apply_service(service, SonosDevice.group_players)
|
_apply_service(service, SonosDevice.group_players)
|
||||||
|
|
||||||
def unjoin_service(service):
|
|
||||||
|
def _unjoin_service(service):
|
||||||
"""Unjoin the player from a group."""
|
"""Unjoin the player from a group."""
|
||||||
_apply_service(service, SonosDevice.unjoin)
|
_apply_service(service, SonosDevice.unjoin)
|
||||||
|
|
||||||
def snapshot_service(service):
|
|
||||||
|
def _snapshot_service(service):
|
||||||
"""Take a snapshot."""
|
"""Take a snapshot."""
|
||||||
_apply_service(service, SonosDevice.snapshot)
|
_apply_service(service, SonosDevice.snapshot)
|
||||||
|
|
||||||
def restore_service(service):
|
|
||||||
|
def _restore_service(service):
|
||||||
"""Restore a snapshot."""
|
"""Restore a snapshot."""
|
||||||
_apply_service(service, SonosDevice.restore)
|
_apply_service(service, SonosDevice.restore)
|
||||||
|
|
||||||
descriptions = load_yaml_config_file(
|
|
||||||
path.join(path.dirname(__file__), 'services.yaml'))
|
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_GROUP_PLAYERS,
|
|
||||||
group_players_service,
|
|
||||||
descriptions.get(SERVICE_GROUP_PLAYERS))
|
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_UNJOIN,
|
|
||||||
unjoin_service,
|
|
||||||
descriptions.get(SERVICE_UNJOIN))
|
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_SNAPSHOT,
|
|
||||||
snapshot_service,
|
|
||||||
descriptions.get(SERVICE_SNAPSHOT))
|
|
||||||
|
|
||||||
hass.services.register(DOMAIN, SERVICE_RESTORE,
|
|
||||||
restore_service,
|
|
||||||
descriptions.get(SERVICE_RESTORE))
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def only_if_coordinator(func):
|
def only_if_coordinator(func):
|
||||||
"""Decorator for coordinator.
|
"""Decorator for coordinator.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user