From 22c0733d8ea56b460b8597380ead1de70c93ba02 Mon Sep 17 00:00:00 2001 From: ehendrix23 Date: Sat, 12 Jan 2019 11:02:00 -0700 Subject: [PATCH] Add service change_channel to Harmony component (#19649) * Update requirements Updated requirements * Add attributes Add firmware and config version attributes * Small bump for aioharmony Small version bump increase for aioharmony * Order ATTR constants * Add the service Add service change_channel * Fix requirements file For some reason aioharmony ended up in there as a duplicate. Fixed it. * Updates based on review --- homeassistant/components/remote/harmony.py | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/homeassistant/components/remote/harmony.py b/homeassistant/components/remote/harmony.py index 4df471aa918..07fd5831dbb 100644 --- a/homeassistant/components/remote/harmony.py +++ b/homeassistant/components/remote/harmony.py @@ -26,6 +26,7 @@ REQUIREMENTS = ['aioharmony==0.1.2'] _LOGGER = logging.getLogger(__name__) +ATTR_CHANNEL = 'channel' ATTR_CURRENT_ACTIVITY = 'current_activity' DEFAULT_PORT = 8088 @@ -33,6 +34,7 @@ DEVICES = [] CONF_DEVICE_CACHE = 'harmony_device_cache' SERVICE_SYNC = 'harmony_sync' +SERVICE_CHANGE_CHANNEL = 'harmony_change_channel' PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Optional(ATTR_ACTIVITY): cv.string, @@ -47,6 +49,11 @@ HARMONY_SYNC_SCHEMA = vol.Schema({ vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, }) +HARMONY_CHANGE_CHANNEL_SCHEMA = vol.Schema({ + vol.Required(ATTR_ENTITY_ID): cv.entity_ids, + vol.Required(ATTR_CHANNEL): cv.positive_int +}) + async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): @@ -112,6 +119,10 @@ def register_services(hass): DOMAIN, SERVICE_SYNC, _sync_service, schema=HARMONY_SYNC_SCHEMA) + hass.services.async_register( + DOMAIN, SERVICE_CHANGE_CHANNEL, _change_channel_service, + schema=HARMONY_CHANGE_CHANNEL_SCHEMA) + async def _apply_service(service, service_func, *service_func_args): """Handle services to apply.""" @@ -131,6 +142,11 @@ async def _sync_service(service): await _apply_service(service, HarmonyRemote.sync) +async def _change_channel_service(service): + channel = service.data.get(ATTR_CHANNEL) + await _apply_service(service, HarmonyRemote.change_channel, channel) + + class HarmonyRemote(remote.RemoteDevice): """Remote representation used to control a Harmony device.""" @@ -348,6 +364,19 @@ class HarmonyRemote(remote.RemoteDevice): result.command.msg ) + async def change_channel(self, channel): + """Change the channel using Harmony remote.""" + import aioharmony.exceptions as aioexc + + _LOGGER.debug("%s: Changing channel to %s", + self.name, channel) + try: + await self._client.change_channel(channel) + except aioexc.TimeOut: + _LOGGER.error("%s: Changing channel to %s timed-out", + self.name, + channel) + async def sync(self): """Sync the Harmony device with the web service.""" import aioharmony.exceptions as aioexc