2019-04-03 15:40:03 +00:00
|
|
|
"""Support for interacting with Snapcast clients."""
|
2016-02-09 23:29:28 +00:00
|
|
|
import logging
|
|
|
|
import socket
|
2016-09-05 20:53:23 +00:00
|
|
|
|
2019-10-20 08:05:37 +00:00
|
|
|
import snapcast.control
|
|
|
|
from snapcast.control.server import CONTROL_PORT
|
2016-09-02 04:36:14 +00:00
|
|
|
import voluptuous as vol
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2019-10-20 08:05:37 +00:00
|
|
|
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerDevice
|
2019-02-08 22:18:18 +00:00
|
|
|
from homeassistant.components.media_player.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_SELECT_SOURCE,
|
|
|
|
SUPPORT_VOLUME_MUTE,
|
|
|
|
SUPPORT_VOLUME_SET,
|
|
|
|
)
|
2016-04-26 09:46:08 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PORT,
|
|
|
|
STATE_IDLE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_PLAYING,
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2016-09-05 20:53:23 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-19 19:43:44 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
|
|
|
from . import (
|
2019-10-20 08:05:37 +00:00
|
|
|
ATTR_MASTER,
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_JOIN,
|
2019-10-20 08:05:37 +00:00
|
|
|
SERVICE_RESTORE,
|
|
|
|
SERVICE_SNAPSHOT,
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_UNJOIN,
|
|
|
|
)
|
2016-09-05 20:53:23 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_KEY = "snapcast"
|
2016-04-26 09:46:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SUPPORT_SNAPCAST_CLIENT = (
|
|
|
|
SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_SELECT_SOURCE
|
|
|
|
)
|
|
|
|
SUPPORT_SNAPCAST_GROUP = (
|
|
|
|
SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_SELECT_SOURCE
|
|
|
|
)
|
2017-05-30 09:34:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
GROUP_PREFIX = "snapcast_group_"
|
|
|
|
GROUP_SUFFIX = "Snapcast Group"
|
|
|
|
CLIENT_PREFIX = "snapcast_client_"
|
|
|
|
CLIENT_SUFFIX = "Snapcast Client"
|
2017-05-30 09:34:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_HOST): cv.string, vol.Optional(CONF_PORT): cv.port}
|
|
|
|
)
|
2016-09-02 04:36:14 +00:00
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the Snapcast platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-09-02 04:36:14 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2017-05-30 09:34:39 +00:00
|
|
|
port = config.get(CONF_PORT, CONTROL_PORT)
|
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
async def async_service_handle(service_event, service, data):
|
|
|
|
"""Handle dispatched services."""
|
|
|
|
entity_ids = data.get(ATTR_ENTITY_ID)
|
2019-07-31 19:25:30 +00:00
|
|
|
devices = [
|
|
|
|
device for device in hass.data[DATA_KEY] if device.entity_id in entity_ids
|
|
|
|
]
|
2017-05-30 09:34:39 +00:00
|
|
|
for device in devices:
|
2019-07-19 19:43:44 +00:00
|
|
|
if service == SERVICE_SNAPSHOT:
|
2017-05-30 09:34:39 +00:00
|
|
|
device.snapshot()
|
2019-07-19 19:43:44 +00:00
|
|
|
elif service == SERVICE_RESTORE:
|
2018-10-01 06:58:21 +00:00
|
|
|
await device.async_restore()
|
2019-07-19 19:43:44 +00:00
|
|
|
elif service == SERVICE_JOIN:
|
|
|
|
if isinstance(device, SnapcastClientDevice):
|
2019-07-31 19:25:30 +00:00
|
|
|
master = [
|
|
|
|
e
|
|
|
|
for e in hass.data[DATA_KEY]
|
|
|
|
if e.entity_id == data[ATTR_MASTER]
|
|
|
|
]
|
2019-07-19 19:43:44 +00:00
|
|
|
if isinstance(master[0], SnapcastClientDevice):
|
|
|
|
await device.async_join(master[0])
|
|
|
|
elif service == SERVICE_UNJOIN:
|
|
|
|
if isinstance(device, SnapcastClientDevice):
|
|
|
|
await device.async_unjoin()
|
|
|
|
|
|
|
|
service_event.set()
|
2017-05-30 09:34:39 +00:00
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
async_dispatcher_connect(hass, DOMAIN, async_service_handle)
|
2016-09-05 20:53:23 +00:00
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
try:
|
2018-10-01 06:58:21 +00:00
|
|
|
server = await snapcast.control.create_server(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.loop, host, port, reconnect=True
|
|
|
|
)
|
2016-02-09 23:29:28 +00:00
|
|
|
except socket.gaierror:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Could not connect to Snapcast server at %s:%d", host, port)
|
2018-01-21 06:35:38 +00:00
|
|
|
return
|
|
|
|
|
2018-06-12 13:46:53 +00:00
|
|
|
# Note: Host part is needed, when using multiple snapservers
|
2019-09-03 19:14:39 +00:00
|
|
|
hpid = f"{host}:{port}"
|
2018-06-12 13:46:53 +00:00
|
|
|
|
|
|
|
groups = [SnapcastGroupDevice(group, hpid) for group in server.groups]
|
|
|
|
clients = [SnapcastClientDevice(client, hpid) for client in server.clients]
|
2017-05-30 09:34:39 +00:00
|
|
|
devices = groups + clients
|
2018-01-15 22:53:56 +00:00
|
|
|
hass.data[DATA_KEY] = devices
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(devices)
|
2016-09-05 20:53:23 +00:00
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2017-05-30 09:34:39 +00:00
|
|
|
class SnapcastGroupDevice(MediaPlayerDevice):
|
|
|
|
"""Representation of a Snapcast group device."""
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2018-06-12 13:46:53 +00:00
|
|
|
def __init__(self, group, uid_part):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Initialize the Snapcast group device."""
|
|
|
|
group.set_callback(self.schedule_update_ha_state)
|
|
|
|
self._group = group
|
2019-09-03 19:14:39 +00:00
|
|
|
self._uid = f"{GROUP_PREFIX}{uid_part}_{self._group.identifier}"
|
2017-05-30 09:34:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the player."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"idle": STATE_IDLE,
|
|
|
|
"playing": STATE_PLAYING,
|
|
|
|
"unknown": STATE_UNKNOWN,
|
2017-05-30 09:34:39 +00:00
|
|
|
}.get(self._group.stream_status, STATE_UNKNOWN)
|
|
|
|
|
2018-06-10 06:31:42 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the ID of snapcast group."""
|
2018-06-12 13:46:53 +00:00
|
|
|
return self._uid
|
2018-06-10 06:31:42 +00:00
|
|
|
|
2017-05-30 09:34:39 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{GROUP_PREFIX}{self._group.identifier}"
|
2017-05-30 09:34:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return the current input source."""
|
|
|
|
return self._group.stream
|
|
|
|
|
|
|
|
@property
|
|
|
|
def volume_level(self):
|
|
|
|
"""Return the volume level."""
|
|
|
|
return self._group.volume / 100
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
|
|
|
"""Volume muted."""
|
|
|
|
return self._group.muted
|
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
|
|
|
return SUPPORT_SNAPCAST_GROUP
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return list(self._group.streams_by_name().keys())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-09-03 19:14:39 +00:00
|
|
|
name = f"{self._group.friendly_name} {GROUP_SUFFIX}"
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"friendly_name": name}
|
2017-05-30 09:34:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Do not poll for state."""
|
|
|
|
return False
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_select_source(self, source):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Set input source."""
|
|
|
|
streams = self._group.streams_by_name()
|
|
|
|
if source in streams:
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._group.set_stream(streams[source].identifier)
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-30 09:34:39 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_mute_volume(self, mute):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Send the mute command."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._group.set_muted(mute)
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-30 09:34:39 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_set_volume_level(self, volume):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Set the volume level."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._group.set_volume(round(volume * 100))
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2017-05-30 09:34:39 +00:00
|
|
|
|
|
|
|
def snapshot(self):
|
|
|
|
"""Snapshot the group state."""
|
|
|
|
self._group.snapshot()
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_restore(self):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Restore the group state."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._group.restore()
|
2017-05-30 09:34:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SnapcastClientDevice(MediaPlayerDevice):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Representation of a Snapcast client device."""
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2018-06-12 13:46:53 +00:00
|
|
|
def __init__(self, client, uid_part):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Initialize the Snapcast client device."""
|
|
|
|
client.set_callback(self.schedule_update_ha_state)
|
2016-02-09 23:29:28 +00:00
|
|
|
self._client = client
|
2019-09-03 19:14:39 +00:00
|
|
|
self._uid = f"{CLIENT_PREFIX}{uid_part}_{self._client.identifier}"
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2018-06-10 06:31:42 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2018-06-12 13:46:53 +00:00
|
|
|
"""
|
|
|
|
Return the ID of this snapcast client.
|
|
|
|
|
|
|
|
Note: Host part is needed, when using multiple snapservers
|
|
|
|
"""
|
|
|
|
return self._uid
|
2018-06-10 06:31:42 +00:00
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
@property
|
|
|
|
def identifier(self):
|
|
|
|
"""Return the snapcast identifier."""
|
|
|
|
return self._client.identifier
|
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the name of the device."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{CLIENT_PREFIX}{self._client.identifier}"
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return the current input source."""
|
|
|
|
return self._client.group.stream
|
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
@property
|
|
|
|
def volume_level(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the volume level."""
|
2016-02-09 23:29:28 +00:00
|
|
|
return self._client.volume / 100
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_volume_muted(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Volume muted."""
|
2016-02-09 23:29:28 +00:00
|
|
|
return self._client.muted
|
|
|
|
|
|
|
|
@property
|
2017-02-08 04:42:45 +00:00
|
|
|
def supported_features(self):
|
|
|
|
"""Flag media player features that are supported."""
|
2017-05-30 09:34:39 +00:00
|
|
|
return SUPPORT_SNAPCAST_CLIENT
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
|
|
|
return list(self._client.group.streams_by_name().keys())
|
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the state of the player."""
|
2017-05-30 09:34:39 +00:00
|
|
|
if self._client.connected:
|
|
|
|
return STATE_ON
|
|
|
|
return STATE_OFF
|
2016-04-26 09:46:08 +00:00
|
|
|
|
|
|
|
@property
|
2017-05-30 09:34:39 +00:00
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
2019-09-03 19:14:39 +00:00
|
|
|
name = f"{self._client.friendly_name} {CLIENT_SUFFIX}"
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"friendly_name": name}
|
2016-04-26 09:46:08 +00:00
|
|
|
|
|
|
|
@property
|
2017-05-30 09:34:39 +00:00
|
|
|
def should_poll(self):
|
|
|
|
"""Do not poll for state."""
|
|
|
|
return False
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
async def async_select_source(self, source):
|
|
|
|
"""Set input source."""
|
|
|
|
streams = self._client.group.streams_by_name()
|
|
|
|
if source in streams:
|
|
|
|
await self._client.group.set_stream(streams[source].identifier)
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_mute_volume(self, mute):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send the mute command."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._client.set_muted(mute)
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2016-02-09 23:29:28 +00:00
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_set_volume_level(self, volume):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Set the volume level."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._client.set_volume(round(volume * 100))
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state()
|
2016-04-26 09:46:08 +00:00
|
|
|
|
2019-07-19 19:43:44 +00:00
|
|
|
async def async_join(self, master):
|
|
|
|
"""Join the group of the master player."""
|
2019-07-31 19:25:30 +00:00
|
|
|
master_group = [
|
|
|
|
group
|
|
|
|
for group in self._client.groups_available()
|
|
|
|
if master.identifier in group.clients
|
|
|
|
]
|
2019-07-19 19:43:44 +00:00
|
|
|
await master_group[0].add_client(self._client.identifier)
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
async def async_unjoin(self):
|
|
|
|
"""Unjoin the group the player is currently in."""
|
|
|
|
await self._client.group.remove_client(self._client.identifier)
|
|
|
|
self.async_schedule_update_ha_state()
|
|
|
|
|
2017-05-30 09:34:39 +00:00
|
|
|
def snapshot(self):
|
|
|
|
"""Snapshot the client state."""
|
|
|
|
self._client.snapshot()
|
|
|
|
|
2018-10-01 06:58:21 +00:00
|
|
|
async def async_restore(self):
|
2017-05-30 09:34:39 +00:00
|
|
|
"""Restore the client state."""
|
2018-10-01 06:58:21 +00:00
|
|
|
await self._client.restore()
|