From 49ecdae9a38dc3eeeab1e7c68af3449ed9cd6724 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Sun, 12 Apr 2020 21:45:17 -0500 Subject: [PATCH 01/11] Implemented snapcast latency attributes --- homeassistant/components/snapcast/__init__.py | 7 +++++ .../components/snapcast/media_player.py | 29 +++++++++++++++++++ .../components/snapcast/services.yaml | 9 ++++++ 3 files changed, 45 insertions(+) diff --git a/homeassistant/components/snapcast/__init__.py b/homeassistant/components/snapcast/__init__.py index e6c574b7b2bd48..8945a3ede8d29e 100644 --- a/homeassistant/components/snapcast/__init__.py +++ b/homeassistant/components/snapcast/__init__.py @@ -14,13 +14,17 @@ SERVICE_RESTORE = "restore" SERVICE_JOIN = "join" SERVICE_UNJOIN = "unjoin" +SERVICE_SET_LATENCY = "set_latency" ATTR_MASTER = "master" +ATTR_LATENCY = "latency" SERVICE_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_ids}) JOIN_SERVICE_SCHEMA = SERVICE_SCHEMA.extend({vol.Required(ATTR_MASTER): cv.entity_id}) +LATENCY_SCHEMA = SERVICE_SCHEMA.extend({vol.Required(ATTR_LATENCY): cv.positive_int}) + async def async_setup(hass, config): """Handle service configuration.""" @@ -46,5 +50,8 @@ async def service_handle(service): hass.services.async_register( DOMAIN, SERVICE_UNJOIN, service_handle, schema=SERVICE_SCHEMA ) + hass.services.async_register( + DOMAIN, SERVICE_SET_LATENCY, service_handle, schema=LATENCY_SCHEMA + ) return True diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 004dd36ed4a993..773270d19897c3 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -26,10 +26,12 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from . import ( + ATTR_LATENCY, ATTR_MASTER, DOMAIN, SERVICE_JOIN, SERVICE_RESTORE, + SERVICE_SET_LATENCY, SERVICE_SNAPSHOT, SERVICE_UNJOIN, ) @@ -84,6 +86,14 @@ async def async_service_handle(service_event, service, data): elif service == SERVICE_UNJOIN: if isinstance(device, SnapcastClientDevice): await device.async_unjoin() + elif service == SERVICE_SET_LATENCY: + if isinstance(device, SnapcastClientDevice): + master = [ + e + for e in hass.data[DATA_KEY] + if e.entity_id == data[ATTR_ENTITY_ID] + ] + await device.async_set_latency(data.get(ATTR_LATENCY)) service_event.set() @@ -257,6 +267,15 @@ def state(self): return STATE_ON return STATE_OFF + @property + def state_attributes(self): + """Return entity state attributes.""" + state_attrs = {} + state_attrs.update(super().state_attributes) + if self.latency is not None: + state_attrs["latency"] = self.latency + return state_attrs + @property def device_state_attributes(self): """Return the state attributes.""" @@ -268,6 +287,11 @@ def should_poll(self): """Do not poll for state.""" return False + @property + def latency(self): + """Latency for Client.""" + return self._client.latency + async def async_select_source(self, source): """Set input source.""" streams = self._client.group.streams_by_name() @@ -307,3 +331,8 @@ def snapshot(self): async def async_restore(self): """Restore the client state.""" await self._client.restore() + + async def async_set_latency(self, latency): + """Set the latency of the client.""" + await self._client.set_latency(latency) + self.async_write_ha_state() diff --git a/homeassistant/components/snapcast/services.yaml b/homeassistant/components/snapcast/services.yaml index 1517f12f52d593..3b83aa3774d4b5 100644 --- a/homeassistant/components/snapcast/services.yaml +++ b/homeassistant/components/snapcast/services.yaml @@ -28,3 +28,12 @@ restore: entity_id: description: Name(s) of entities that will be restored. Platform dependent. example: "media_player.living_room" + +set_latency: + description: Set client set_latency + fields: + entity_id: + description: Name of entities that will have adjusted latency + latency: + description: Latency in master + example: 14 From f81bfc15c79314728dba586154634e2fb9fcaed3 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Mon, 13 Apr 2020 17:34:38 +0000 Subject: [PATCH 02/11] Code review changes and Snapcast maintenance Updated how entity services get called - now conforms to most current method --- .coveragerc | 2 +- homeassistant/components/snapcast/__init__.py | 34 --------- .../components/snapcast/media_player.py | 70 ++++++------------- 3 files changed, 22 insertions(+), 84 deletions(-) diff --git a/.coveragerc b/.coveragerc index acf72eb254c34e..f1f4aeaf0d184f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -648,7 +648,7 @@ omit = homeassistant/components/smarthab/* homeassistant/components/sms/* homeassistant/components/smtp/notify.py - homeassistant/components/snapcast/media_player.py + homeassistant/components/snapcast/* homeassistant/components/snmp/* homeassistant/components/sochain/sensor.py homeassistant/components/socialblade/sensor.py diff --git a/homeassistant/components/snapcast/__init__.py b/homeassistant/components/snapcast/__init__.py index 8945a3ede8d29e..76c493fb7cd011 100644 --- a/homeassistant/components/snapcast/__init__.py +++ b/homeassistant/components/snapcast/__init__.py @@ -1,12 +1,9 @@ """The snapcast component.""" -import asyncio - import voluptuous as vol from homeassistant.const import ATTR_ENTITY_ID from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.dispatcher import async_dispatcher_send DOMAIN = "snapcast" @@ -24,34 +21,3 @@ JOIN_SERVICE_SCHEMA = SERVICE_SCHEMA.extend({vol.Required(ATTR_MASTER): cv.entity_id}) LATENCY_SCHEMA = SERVICE_SCHEMA.extend({vol.Required(ATTR_LATENCY): cv.positive_int}) - - -async def async_setup(hass, config): - """Handle service configuration.""" - service_event = asyncio.Event() - - async def service_handle(service): - """Dispatch a service call.""" - service_event.clear() - async_dispatcher_send( - hass, DOMAIN, service_event, service.service, service.data - ) - await service_event.wait() - - hass.services.async_register( - DOMAIN, SERVICE_SNAPSHOT, service_handle, schema=SERVICE_SCHEMA - ) - hass.services.async_register( - DOMAIN, SERVICE_RESTORE, service_handle, schema=SERVICE_SCHEMA - ) - hass.services.async_register( - DOMAIN, SERVICE_JOIN, service_handle, schema=JOIN_SERVICE_SCHEMA - ) - hass.services.async_register( - DOMAIN, SERVICE_UNJOIN, service_handle, schema=SERVICE_SCHEMA - ) - hass.services.async_register( - DOMAIN, SERVICE_SET_LATENCY, service_handle, schema=LATENCY_SCHEMA - ) - - return True diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 773270d19897c3..c1843b5474c510 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -13,7 +13,6 @@ SUPPORT_VOLUME_SET, ) from homeassistant.const import ( - ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, STATE_IDLE, @@ -22,13 +21,11 @@ STATE_PLAYING, STATE_UNKNOWN, ) -import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers import config_validation as cv, entity_platform from . import ( ATTR_LATENCY, ATTR_MASTER, - DOMAIN, SERVICE_JOIN, SERVICE_RESTORE, SERVICE_SET_LATENCY, @@ -60,44 +57,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Snapcast platform.""" + _LOGGER.debug("Reached async_setup_platform") + host = config.get(CONF_HOST) port = config.get(CONF_PORT, CONTROL_PORT) - async def async_service_handle(service_event, service, data): - """Handle dispatched services.""" - entity_ids = data.get(ATTR_ENTITY_ID) - devices = [ - device for device in hass.data[DATA_KEY] if device.entity_id in entity_ids - ] - for device in devices: - if service == SERVICE_SNAPSHOT: - device.snapshot() - elif service == SERVICE_RESTORE: - await device.async_restore() - elif service == SERVICE_JOIN: - if isinstance(device, SnapcastClientDevice): - master = [ - e - for e in hass.data[DATA_KEY] - if e.entity_id == data[ATTR_MASTER] - ] - if isinstance(master[0], SnapcastClientDevice): - await device.async_join(master[0]) - elif service == SERVICE_UNJOIN: - if isinstance(device, SnapcastClientDevice): - await device.async_unjoin() - elif service == SERVICE_SET_LATENCY: - if isinstance(device, SnapcastClientDevice): - master = [ - e - for e in hass.data[DATA_KEY] - if e.entity_id == data[ATTR_ENTITY_ID] - ] - await device.async_set_latency(data.get(ATTR_LATENCY)) - - service_event.set() - - async_dispatcher_connect(hass, DOMAIN, async_service_handle) + platform = entity_platform.current_platform.get() + platform.async_register_entity_service(SERVICE_SNAPSHOT, {}, "snapshot") + platform.async_register_entity_service(SERVICE_RESTORE, {}, "async_restore") + platform.async_register_entity_service( + SERVICE_JOIN, {vol.Required(ATTR_MASTER): cv.entity_id}, "async_join" + ) + platform.async_register_entity_service(SERVICE_UNJOIN, {}, "async_unjoin") + platform.async_register_entity_service( + SERVICE_SET_LATENCY, + {vol.Required(ATTR_LATENCY): cv.positive_int}, + "async_set_latency", + ) try: server = await snapcast.control.create_server( @@ -268,19 +244,14 @@ def state(self): return STATE_OFF @property - def state_attributes(self): - """Return entity state attributes.""" + def device_state_attributes(self): + """Return the state attributes.""" state_attrs = {} - state_attrs.update(super().state_attributes) if self.latency is not None: state_attrs["latency"] = self.latency - return state_attrs - - @property - def device_state_attributes(self): - """Return the state attributes.""" name = f"{self._client.friendly_name} {CLIENT_SUFFIX}" - return {"friendly_name": name} + state_attrs["friendly_name"] = name + return state_attrs @property def should_poll(self): @@ -311,10 +282,11 @@ async def async_set_volume_level(self, volume): async def async_join(self, master): """Join the group of the master player.""" + masters = [e for e in self.hass.data[DATA_KEY] if e.entity_id == master] master_group = [ group for group in self._client.groups_available() - if master.identifier in group.clients + if masters[0].identifier in group.clients ] await master_group[0].add_client(self._client.identifier) self.async_write_ha_state() From 35abd342ce8f730369d00b8e2547db6b1b9d3e83 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 02:43:30 +0000 Subject: [PATCH 03/11] Cleanup tasks Moved constants into separate file Removed unnecessary logger message Remove unnecessary schemas --- homeassistant/components/snapcast/__init__.py | 22 ---------------- homeassistant/components/snapcast/const.py | 18 +++++++++++++ .../components/snapcast/media_player.py | 25 ++----------------- 3 files changed, 20 insertions(+), 45 deletions(-) create mode 100644 homeassistant/components/snapcast/const.py diff --git a/homeassistant/components/snapcast/__init__.py b/homeassistant/components/snapcast/__init__.py index 76c493fb7cd011..b5279fa3ce06c2 100644 --- a/homeassistant/components/snapcast/__init__.py +++ b/homeassistant/components/snapcast/__init__.py @@ -1,23 +1 @@ """The snapcast component.""" - -import voluptuous as vol - -from homeassistant.const import ATTR_ENTITY_ID -from homeassistant.helpers import config_validation as cv - -DOMAIN = "snapcast" - -SERVICE_SNAPSHOT = "snapshot" -SERVICE_RESTORE = "restore" -SERVICE_JOIN = "join" -SERVICE_UNJOIN = "unjoin" -SERVICE_SET_LATENCY = "set_latency" - -ATTR_MASTER = "master" -ATTR_LATENCY = "latency" - -SERVICE_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_ids}) - -JOIN_SERVICE_SCHEMA = SERVICE_SCHEMA.extend({vol.Required(ATTR_MASTER): cv.entity_id}) - -LATENCY_SCHEMA = SERVICE_SCHEMA.extend({vol.Required(ATTR_LATENCY): cv.positive_int}) diff --git a/homeassistant/components/snapcast/const.py b/homeassistant/components/snapcast/const.py new file mode 100644 index 00000000000000..0b21bfa9dd63b2 --- /dev/null +++ b/homeassistant/components/snapcast/const.py @@ -0,0 +1,18 @@ +"""Constants for Snapcast.""" +DOMAIN = "snapcast" + +DATA_KEY = "snapcast" + +GROUP_PREFIX = "snapcast_group_" +GROUP_SUFFIX = "Snapcast Group" +CLIENT_PREFIX = "snapcast_client_" +CLIENT_SUFFIX = "Snapcast Client" + +SERVICE_SNAPSHOT = "snapshot" +SERVICE_RESTORE = "restore" +SERVICE_JOIN = "join" +SERVICE_UNJOIN = "unjoin" +SERVICE_SET_LATENCY = "set_latency" + +ATTR_MASTER = "master" +ATTR_LATENCY = "latency" diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index c1843b5474c510..830f53bf8c313a 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -6,7 +6,7 @@ from snapcast.control.server import CONTROL_PORT import voluptuous as vol -from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerDevice +from homeassistant.components.media_player import MediaPlayerDevice from homeassistant.components.media_player.const import ( SUPPORT_SELECT_SOURCE, SUPPORT_VOLUME_MUTE, @@ -23,20 +23,10 @@ ) from homeassistant.helpers import config_validation as cv, entity_platform -from . import ( - ATTR_LATENCY, - ATTR_MASTER, - SERVICE_JOIN, - SERVICE_RESTORE, - SERVICE_SET_LATENCY, - SERVICE_SNAPSHOT, - SERVICE_UNJOIN, -) +from .const import * _LOGGER = logging.getLogger(__name__) -DATA_KEY = "snapcast" - SUPPORT_SNAPCAST_CLIENT = ( SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_SELECT_SOURCE ) @@ -44,21 +34,10 @@ SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_SELECT_SOURCE ) -GROUP_PREFIX = "snapcast_group_" -GROUP_SUFFIX = "Snapcast Group" -CLIENT_PREFIX = "snapcast_client_" -CLIENT_SUFFIX = "Snapcast Client" - -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( - {vol.Required(CONF_HOST): cv.string, vol.Optional(CONF_PORT): cv.port} -) - async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Snapcast platform.""" - _LOGGER.debug("Reached async_setup_platform") - host = config.get(CONF_HOST) port = config.get(CONF_PORT, CONTROL_PORT) From c234c564dab6334764707ebd4d729e6ec32c40c3 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 03:12:27 +0000 Subject: [PATCH 04/11] FIx linting errors --- homeassistant/components/snapcast/const.py | 1 - homeassistant/components/snapcast/media_player.py | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/snapcast/const.py b/homeassistant/components/snapcast/const.py index 0b21bfa9dd63b2..674a22993b910c 100644 --- a/homeassistant/components/snapcast/const.py +++ b/homeassistant/components/snapcast/const.py @@ -1,5 +1,4 @@ """Constants for Snapcast.""" -DOMAIN = "snapcast" DATA_KEY = "snapcast" diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 830f53bf8c313a..aff4d9600a2138 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -23,7 +23,20 @@ ) from homeassistant.helpers import config_validation as cv, entity_platform -from .const import * +from .const import ( + DATA_KEY, + GROUP_PREFIX, + GROUP_SUFFIX, + CLIENT_PREFIX, + CLIENT_SUFFIX, + SERVICE_SNAPSHOT, + SERVICE_RESTORE, + SERVICE_JOIN, + SERVICE_UNJOIN, + SERVICE_SET_LATENCY, + ATTR_MASTER, + ATTR_LATENCY, +) _LOGGER = logging.getLogger(__name__) From cde19dcbc713a52d85007206c2e1f73f9c95ae7e Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 03:21:22 +0000 Subject: [PATCH 05/11] Sort imports --- homeassistant/components/snapcast/media_player.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index aff4d9600a2138..a2900a074a82d2 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -24,18 +24,18 @@ from homeassistant.helpers import config_validation as cv, entity_platform from .const import ( + ATTR_LATENCY, + ATTR_MASTER, + CLIENT_PREFIX, + CLIENT_SUFFIX, DATA_KEY, GROUP_PREFIX, GROUP_SUFFIX, - CLIENT_PREFIX, - CLIENT_SUFFIX, - SERVICE_SNAPSHOT, - SERVICE_RESTORE, SERVICE_JOIN, - SERVICE_UNJOIN, + SERVICE_RESTORE, SERVICE_SET_LATENCY, - ATTR_MASTER, - ATTR_LATENCY, + SERVICE_SNAPSHOT, + SERVICE_UNJOIN, ) _LOGGER = logging.getLogger(__name__) From 027586c53c3a11175c2872994aa74626edc2658a Mon Sep 17 00:00:00 2001 From: BarrettLowe Date: Mon, 13 Apr 2020 23:12:31 -0500 Subject: [PATCH 06/11] Update with requested change Better - use next() Co-Authored-By: Martin Hjelmare --- homeassistant/components/snapcast/media_player.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index a2900a074a82d2..ba1e68c91e989a 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -274,7 +274,12 @@ async def async_set_volume_level(self, volume): async def async_join(self, master): """Join the group of the master player.""" - masters = [e for e in self.hass.data[DATA_KEY] if e.entity_id == master] + master_entity = next( + ( + entity for entity in self.hass.data[DATA_KEY] + if entity.entity_id == master + ) + ) master_group = [ group for group in self._client.groups_available() From b5c737f6c1408556ec42fb132079a3aa68da3fa8 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 06:46:51 +0000 Subject: [PATCH 07/11] Add guards for bad service calls --- .../components/snapcast/media_player.py | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index ba1e68c91e989a..54351bcdaab687 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -58,13 +58,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= platform.async_register_entity_service(SERVICE_SNAPSHOT, {}, "snapshot") platform.async_register_entity_service(SERVICE_RESTORE, {}, "async_restore") platform.async_register_entity_service( - SERVICE_JOIN, {vol.Required(ATTR_MASTER): cv.entity_id}, "async_join" + SERVICE_JOIN, {vol.Required(ATTR_MASTER): cv.entity_id}, handle_async_join ) platform.async_register_entity_service(SERVICE_UNJOIN, {}, "async_unjoin") platform.async_register_entity_service( SERVICE_SET_LATENCY, {vol.Required(ATTR_LATENCY): cv.positive_int}, - "async_set_latency", + handle_set_latency, ) try: @@ -85,6 +85,20 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= async_add_entities(devices) +async def handle_async_join(entity, service_call): + """Handle the entity service join.""" + if not isinstance(entity, SnapcastClientDevice): + raise ValueError("Entity is not a client. Can only join clients.") + await entity.async_join(service_call.data[ATTR_MASTER]) + + +async def handle_set_latency(entity, service_call): + """Handle the entity service set_latency.""" + if not isinstance(entity, SnapcastClientDevice): + raise ValueError("Latency can only be set for a Snapcast client.") + await entity.async_set_latency(service_call.data[ATTR_LATENCY]) + + class SnapcastGroupDevice(MediaPlayerDevice): """Representation of a Snapcast group device.""" @@ -274,18 +288,25 @@ async def async_set_volume_level(self, volume): async def async_join(self, master): """Join the group of the master player.""" + master_entity = next( ( - entity for entity in self.hass.data[DATA_KEY] + entity + for entity in self.hass.data[DATA_KEY] if entity.entity_id == master ) ) - master_group = [ - group - for group in self._client.groups_available() - if masters[0].identifier in group.clients - ] - await master_group[0].add_client(self._client.identifier) + if not isinstance(master_entity, SnapcastClientDevice): + raise ValueError("Master is not a client device. Can only join clients.") + + master_group = next( + ( + group + for group in self._client.groups_available() + if master_entity.identifier in group.clients + ) + ) + await master_group.add_client(self._client.identifier) self.async_write_ha_state() async def async_unjoin(self): From 47d85f1fe7290d0386ac188aef143766b6d27871 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 13:28:08 +0000 Subject: [PATCH 08/11] Add back in platform schema --- homeassistant/components/snapcast/media_player.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 54351bcdaab687..49f33769a718a5 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -6,7 +6,7 @@ from snapcast.control.server import CONTROL_PORT import voluptuous as vol -from homeassistant.components.media_player import MediaPlayerDevice +from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerDevice from homeassistant.components.media_player.const import ( SUPPORT_SELECT_SOURCE, SUPPORT_VOLUME_MUTE, @@ -47,6 +47,10 @@ SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | SUPPORT_SELECT_SOURCE ) +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( + {vol.Required(CONF_HOST): cv.string, vol.Optional(CONF_PORT): cv.port} +) + async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the Snapcast platform.""" From 6a7bed7397d0627f3ce837dd3cb942ac1ee80b13 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 13:29:54 +0000 Subject: [PATCH 09/11] Add check for unjoin service call --- homeassistant/components/snapcast/media_player.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 49f33769a718a5..2a3c5c78b92040 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -64,7 +64,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= platform.async_register_entity_service( SERVICE_JOIN, {vol.Required(ATTR_MASTER): cv.entity_id}, handle_async_join ) - platform.async_register_entity_service(SERVICE_UNJOIN, {}, "async_unjoin") + platform.async_register_entity_service(SERVICE_UNJOIN, {}, handle_async_unjoin) platform.async_register_entity_service( SERVICE_SET_LATENCY, {vol.Required(ATTR_LATENCY): cv.positive_int}, @@ -96,6 +96,13 @@ async def handle_async_join(entity, service_call): await entity.async_join(service_call.data[ATTR_MASTER]) +async def handle_async_unjoin(entity, service_call): + """Handle the entity service unjoin.""" + if not isinstance(entity, SnapcastClientDevice): + raise ValueError("Entity is not a client. Can only unjoin clients.") + await entity.async_unjoin() + + async def handle_set_latency(entity, service_call): """Handle the entity service set_latency.""" if not isinstance(entity, SnapcastClientDevice): From 68a7d90e0980f4a690b06b2cf25d379cbb8557b4 Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 15:08:24 +0000 Subject: [PATCH 10/11] Fix lint/format --- homeassistant/components/snapcast/manifest.json | 10 +++++----- homeassistant/components/snapcast/media_player.py | 14 ++++---------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/snapcast/manifest.json b/homeassistant/components/snapcast/manifest.json index 31eb0491eb4fea..1cd20df933eeb0 100644 --- a/homeassistant/components/snapcast/manifest.json +++ b/homeassistant/components/snapcast/manifest.json @@ -1,7 +1,7 @@ { - "domain": "snapcast", - "name": "Snapcast", - "documentation": "https://www.home-assistant.io/integrations/snapcast", - "requirements": ["snapcast==2.0.10"], - "codeowners": [] + "domain": "snapcast", + "name": "Snapcast", + "documentation": "https://www.home-assistant.io/integrations/snapcast", + "requirements": ["snapcast==2.0.10"], + "codeowners": [], } diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 2a3c5c78b92040..1bd44959ab1dc0 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -301,21 +301,15 @@ async def async_join(self, master): """Join the group of the master player.""" master_entity = next( - ( - entity - for entity in self.hass.data[DATA_KEY] - if entity.entity_id == master - ) + entity for entity in self.hass.data[DATA_KEY] if entity.entity_id == master ) if not isinstance(master_entity, SnapcastClientDevice): raise ValueError("Master is not a client device. Can only join clients.") master_group = next( - ( - group - for group in self._client.groups_available() - if master_entity.identifier in group.clients - ) + group + for group in self._client.groups_available() + if master_entity.identifier in group.clients ) await master_group.add_client(self._client.identifier) self.async_write_ha_state() From 6f50d59e9e4806ba3247cbf8a17329cee691bbcb Mon Sep 17 00:00:00 2001 From: Barrett Lowe Date: Tue, 14 Apr 2020 16:31:56 +0000 Subject: [PATCH 11/11] remove comma inserted by black --- homeassistant/components/snapcast/manifest.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/snapcast/manifest.json b/homeassistant/components/snapcast/manifest.json index 1cd20df933eeb0..31eb0491eb4fea 100644 --- a/homeassistant/components/snapcast/manifest.json +++ b/homeassistant/components/snapcast/manifest.json @@ -1,7 +1,7 @@ { - "domain": "snapcast", - "name": "Snapcast", - "documentation": "https://www.home-assistant.io/integrations/snapcast", - "requirements": ["snapcast==2.0.10"], - "codeowners": [], + "domain": "snapcast", + "name": "Snapcast", + "documentation": "https://www.home-assistant.io/integrations/snapcast", + "requirements": ["snapcast==2.0.10"], + "codeowners": [] }