2016-02-09 23:29:28 +00:00
|
|
|
"""
|
2016-03-08 09:34:33 +00:00
|
|
|
Support for interacting with Snapcast clients.
|
2016-02-09 23:29:28 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/media_player.snapcast/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import socket
|
2016-09-05 20:53:23 +00:00
|
|
|
|
2016-09-02 04:36:14 +00:00
|
|
|
import voluptuous as vol
|
2016-02-09 23:29:28 +00:00
|
|
|
|
|
|
|
from homeassistant.components.media_player import (
|
2016-04-26 09:46:08 +00:00
|
|
|
SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_SET, SUPPORT_SELECT_SOURCE,
|
2017-01-09 00:09:30 +00:00
|
|
|
SUPPORT_PLAY, PLATFORM_SCHEMA, MediaPlayerDevice)
|
2016-04-26 09:46:08 +00:00
|
|
|
from homeassistant.const import (
|
2016-09-05 20:53:23 +00:00
|
|
|
STATE_OFF, STATE_IDLE, STATE_PLAYING, STATE_UNKNOWN, CONF_HOST, CONF_PORT)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
REQUIREMENTS = ['snapcast==1.2.2']
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'snapcast'
|
2016-04-26 09:46:08 +00:00
|
|
|
|
|
|
|
SUPPORT_SNAPCAST = SUPPORT_VOLUME_SET | SUPPORT_VOLUME_MUTE | \
|
2017-01-09 00:09:30 +00:00
|
|
|
SUPPORT_SELECT_SOURCE | SUPPORT_PLAY
|
2016-02-09 23:29:28 +00:00
|
|
|
|
|
|
|
|
2016-09-02 04:36:14 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
2016-09-05 20:53:23 +00:00
|
|
|
vol.Optional(CONF_PORT): cv.port,
|
2016-09-02 04:36:14 +00:00
|
|
|
})
|
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Setup the Snapcast platform."""
|
2016-02-09 23:29:28 +00:00
|
|
|
import snapcast.control
|
2016-09-02 04:36:14 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
port = config.get(CONF_PORT, snapcast.control.CONTROL_PORT)
|
2016-09-05 20:53:23 +00:00
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
try:
|
|
|
|
server = snapcast.control.Snapserver(host, port)
|
|
|
|
except socket.gaierror:
|
|
|
|
_LOGGER.error('Could not connect to Snapcast server at %s:%d',
|
|
|
|
host, port)
|
2016-09-05 20:53:23 +00:00
|
|
|
return False
|
|
|
|
|
2016-02-09 23:29:28 +00:00
|
|
|
add_devices([SnapcastDevice(client) for client in server.clients])
|
|
|
|
|
|
|
|
|
|
|
|
class SnapcastDevice(MediaPlayerDevice):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Representation of a Snapcast client device."""
|
2016-02-09 23:29:28 +00:00
|
|
|
|
|
|
|
def __init__(self, client):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Initialize the Snapcast device."""
|
2016-02-09 23:29:28 +00:00
|
|
|
self._client = client
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the name of the device."""
|
2016-02-09 23:29:28 +00:00
|
|
|
return self._client.identifier
|
|
|
|
|
|
|
|
@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
|
|
|
|
def supported_media_commands(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Flag of media commands that are supported."""
|
2016-02-09 23:29:28 +00:00
|
|
|
return SUPPORT_SNAPCAST
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Return the state of the player."""
|
2016-04-26 09:46:08 +00:00
|
|
|
if not self._client.connected:
|
|
|
|
return STATE_OFF
|
|
|
|
return {
|
|
|
|
'idle': STATE_IDLE,
|
|
|
|
'playing': STATE_PLAYING,
|
2016-09-02 04:36:14 +00:00
|
|
|
'unknown': STATE_UNKNOWN,
|
2016-04-26 09:46:08 +00:00
|
|
|
}.get(self._client.stream.status, STATE_UNKNOWN)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def source(self):
|
|
|
|
"""Return the current input source."""
|
2016-09-02 04:36:14 +00:00
|
|
|
return self._client.stream.name
|
2016-04-26 09:46:08 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def source_list(self):
|
|
|
|
"""List of available input sources."""
|
2016-09-02 04:36:14 +00:00
|
|
|
return list(self._client.streams_by_name().keys())
|
2016-02-09 23:29:28 +00:00
|
|
|
|
|
|
|
def mute_volume(self, mute):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Send the mute command."""
|
2016-02-09 23:29:28 +00:00
|
|
|
self._client.muted = mute
|
|
|
|
|
|
|
|
def set_volume_level(self, volume):
|
2016-03-08 09:34:33 +00:00
|
|
|
"""Set the volume level."""
|
2016-02-09 23:29:28 +00:00
|
|
|
self._client.volume = round(volume * 100)
|
2016-04-26 09:46:08 +00:00
|
|
|
|
|
|
|
def select_source(self, source):
|
|
|
|
"""Set input source."""
|
2016-09-02 04:36:14 +00:00
|
|
|
streams = self._client.streams_by_name()
|
|
|
|
if source in streams:
|
|
|
|
self._client.stream = streams[source].identifier
|