Use web sockets for Harmony HUB (#19440)
* Updates to Harmony for web sockets Updates to harmony to use web sockets with async * Lint * Small fixes * Fix send_command Continued improvements: -) Fixed send_command -) Get HUB configuration during update in case it was not retrieved earlier (i.e. HUB unavailable) * Further improvements Completely removed dependency on __main__ for pyharmony, instead everything is now done from the HarmonyClient class. Writing out Harmony configuration file as a JSON file. Using same functionality to determine if activity provided is an ID or name for device, allowing send_command to receive a device ID or device name. * Point requirements to updated pyharmony repo Updated REQUIREMENTS to point to repository containing the updates for pyharmony. * lint lint * Small fix for device and activity ID Small fix in checking if provided device or activity ID is valid. * Pin package version * No I/O in event loop * Point at HA fork with correct version bump * Fix req
This commit is contained in:
parent
1568de62df
commit
23a579421d
3 changed files with 109 additions and 44 deletions
|
@ -4,8 +4,11 @@ Support for Harmony Hub devices.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/remote.harmony/
|
https://home-assistant.io/components/remote.harmony/
|
||||||
"""
|
"""
|
||||||
|
import asyncio
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
import time
|
from datetime import timedelta
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
@ -19,11 +22,17 @@ import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.exceptions import PlatformNotReady
|
from homeassistant.exceptions import PlatformNotReady
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
REQUIREMENTS = ['pyharmony==1.0.20']
|
# REQUIREMENTS = ['pyharmony==1.0.22']
|
||||||
|
REQUIREMENTS = [
|
||||||
|
'https://github.com/home-assistant//pyharmony/archive/'
|
||||||
|
'4b27f8a35ea61123ef531ad078a4357cc26b00db.zip'
|
||||||
|
'#pyharmony==1.0.21b0'
|
||||||
|
]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_PORT = 5222
|
DEFAULT_PORT = 8088
|
||||||
|
SCAN_INTERVAL = timedelta(seconds=5)
|
||||||
DEVICES = []
|
DEVICES = []
|
||||||
CONF_DEVICE_CACHE = 'harmony_device_cache'
|
CONF_DEVICE_CACHE = 'harmony_device_cache'
|
||||||
|
|
||||||
|
@ -43,7 +52,8 @@ HARMONY_SYNC_SCHEMA = vol.Schema({
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
async def async_setup_platform(hass, config, async_add_entities,
|
||||||
|
discovery_info=None):
|
||||||
"""Set up the Harmony platform."""
|
"""Set up the Harmony platform."""
|
||||||
host = None
|
host = None
|
||||||
activity = None
|
activity = None
|
||||||
|
@ -95,7 +105,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
device = HarmonyRemote(
|
device = HarmonyRemote(
|
||||||
name, address, port, activity, harmony_conf_file, delay_secs)
|
name, address, port, activity, harmony_conf_file, delay_secs)
|
||||||
DEVICES.append(device)
|
DEVICES.append(device)
|
||||||
add_entities([device])
|
async_add_entities([device])
|
||||||
register_services(hass)
|
register_services(hass)
|
||||||
except (ValueError, AttributeError):
|
except (ValueError, AttributeError):
|
||||||
raise PlatformNotReady
|
raise PlatformNotReady
|
||||||
|
@ -103,12 +113,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
|
|
||||||
def register_services(hass):
|
def register_services(hass):
|
||||||
"""Register all services for harmony devices."""
|
"""Register all services for harmony devices."""
|
||||||
hass.services.register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_SYNC, _sync_service,
|
DOMAIN, SERVICE_SYNC, _sync_service,
|
||||||
schema=HARMONY_SYNC_SCHEMA)
|
schema=HARMONY_SYNC_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
def _apply_service(service, service_func, *service_func_args):
|
async def _apply_service(service, service_func, *service_func_args):
|
||||||
"""Handle services to apply."""
|
"""Handle services to apply."""
|
||||||
entity_ids = service.data.get('entity_id')
|
entity_ids = service.data.get('entity_id')
|
||||||
|
|
||||||
|
@ -119,12 +129,12 @@ def _apply_service(service, service_func, *service_func_args):
|
||||||
_devices = DEVICES
|
_devices = DEVICES
|
||||||
|
|
||||||
for device in _devices:
|
for device in _devices:
|
||||||
service_func(device, *service_func_args)
|
await service_func(device, *service_func_args)
|
||||||
device.schedule_update_ha_state(True)
|
device.schedule_update_ha_state(True)
|
||||||
|
|
||||||
|
|
||||||
def _sync_service(service):
|
async def _sync_service(service):
|
||||||
_apply_service(service, HarmonyRemote.sync)
|
await _apply_service(service, HarmonyRemote.sync)
|
||||||
|
|
||||||
|
|
||||||
class HarmonyRemote(remote.RemoteDevice):
|
class HarmonyRemote(remote.RemoteDevice):
|
||||||
|
@ -132,8 +142,7 @@ class HarmonyRemote(remote.RemoteDevice):
|
||||||
|
|
||||||
def __init__(self, name, host, port, activity, out_path, delay_secs):
|
def __init__(self, name, host, port, activity, out_path, delay_secs):
|
||||||
"""Initialize HarmonyRemote class."""
|
"""Initialize HarmonyRemote class."""
|
||||||
import pyharmony
|
import pyharmony.client as harmony_client
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
_LOGGER.debug("HarmonyRemote device init started for: %s", name)
|
_LOGGER.debug("HarmonyRemote device init started for: %s", name)
|
||||||
self._name = name
|
self._name = name
|
||||||
|
@ -142,23 +151,30 @@ class HarmonyRemote(remote.RemoteDevice):
|
||||||
self._state = None
|
self._state = None
|
||||||
self._current_activity = None
|
self._current_activity = None
|
||||||
self._default_activity = activity
|
self._default_activity = activity
|
||||||
self._client = pyharmony.get_client(host, port, self.new_activity)
|
# self._client = pyharmony.get_client(host, port, self.new_activity)
|
||||||
|
self._client = harmony_client.HarmonyClient(host)
|
||||||
self._config_path = out_path
|
self._config_path = out_path
|
||||||
self._config = self._client.get_config()
|
|
||||||
if not Path(self._config_path).is_file():
|
|
||||||
_LOGGER.debug("Writing harmony configuration to file: %s",
|
|
||||||
out_path)
|
|
||||||
pyharmony.ha_write_config_file(self._config, self._config_path)
|
|
||||||
self._delay_secs = delay_secs
|
self._delay_secs = delay_secs
|
||||||
|
_LOGGER.debug("HarmonyRemote device init completed for: %s", name)
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
async def async_added_to_hass(self):
|
||||||
"""Complete the initialization."""
|
"""Complete the initialization."""
|
||||||
self.hass.bus.async_listen_once(
|
_LOGGER.debug("HarmonyRemote added for: %s", self._name)
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
|
||||||
lambda event: self._client.disconnect(wait=True))
|
async def shutdown(event):
|
||||||
|
"""Close connection on shutdown."""
|
||||||
|
await self._client.disconnect()
|
||||||
|
|
||||||
|
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown)
|
||||||
|
|
||||||
|
_LOGGER.debug("Connecting.")
|
||||||
|
await self._client.connect()
|
||||||
|
await self._client.get_config()
|
||||||
|
if not Path(self._config_path).is_file():
|
||||||
|
self.write_config_file()
|
||||||
|
|
||||||
# Poll for initial state
|
# Poll for initial state
|
||||||
self.new_activity(self._client.get_current_activity())
|
self.new_activity(await self._client.get_current_activity())
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -168,7 +184,7 @@ class HarmonyRemote(remote.RemoteDevice):
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
"""Return the fact that we should not be polled."""
|
"""Return the fact that we should not be polled."""
|
||||||
return False
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
|
@ -180,52 +196,101 @@ class HarmonyRemote(remote.RemoteDevice):
|
||||||
"""Return False if PowerOff is the current activity, otherwise True."""
|
"""Return False if PowerOff is the current activity, otherwise True."""
|
||||||
return self._current_activity not in [None, 'PowerOff']
|
return self._current_activity not in [None, 'PowerOff']
|
||||||
|
|
||||||
|
async def async_update(self):
|
||||||
|
"""Retrieve current activity from Hub."""
|
||||||
|
_LOGGER.debug("Updating Harmony.")
|
||||||
|
if not self._client.config:
|
||||||
|
await self._client.get_config()
|
||||||
|
|
||||||
|
activity_id = await self._client.get_current_activity()
|
||||||
|
activity_name = self._client.get_activity_name(activity_id)
|
||||||
|
_LOGGER.debug("%s activity reported as: %s", self._name, activity_name)
|
||||||
|
self._current_activity = activity_name
|
||||||
|
self._state = bool(self._current_activity != 'PowerOff')
|
||||||
|
return
|
||||||
|
|
||||||
def new_activity(self, activity_id):
|
def new_activity(self, activity_id):
|
||||||
"""Call for updating the current activity."""
|
"""Call for updating the current activity."""
|
||||||
import pyharmony
|
activity_name = self._client.get_activity_name(activity_id)
|
||||||
activity_name = pyharmony.activity_name(self._config, activity_id)
|
|
||||||
_LOGGER.debug("%s activity reported as: %s", self._name, activity_name)
|
_LOGGER.debug("%s activity reported as: %s", self._name, activity_name)
|
||||||
self._current_activity = activity_name
|
self._current_activity = activity_name
|
||||||
self._state = bool(self._current_activity != 'PowerOff')
|
self._state = bool(self._current_activity != 'PowerOff')
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Start an activity from the Harmony device."""
|
"""Start an activity from the Harmony device."""
|
||||||
import pyharmony
|
|
||||||
activity = kwargs.get(ATTR_ACTIVITY, self._default_activity)
|
activity = kwargs.get(ATTR_ACTIVITY, self._default_activity)
|
||||||
|
|
||||||
if activity:
|
if activity:
|
||||||
activity_id = pyharmony.activity_id(self._config, activity)
|
activity_id = None
|
||||||
self._client.start_activity(activity_id)
|
if activity.isdigit() or activity == '-1':
|
||||||
|
_LOGGER.debug("Activity is numeric")
|
||||||
|
if self._client.get_activity_name(int(activity)):
|
||||||
|
activity_id = activity
|
||||||
|
|
||||||
|
if not activity_id:
|
||||||
|
_LOGGER.debug("Find activity ID based on name")
|
||||||
|
activity_id = self._client.get_activity_id(
|
||||||
|
str(activity).strip())
|
||||||
|
|
||||||
|
if not activity_id:
|
||||||
|
_LOGGER.error("Activity %s is invalid", activity)
|
||||||
|
return
|
||||||
|
|
||||||
|
await self._client.start_activity(activity_id)
|
||||||
self._state = True
|
self._state = True
|
||||||
else:
|
else:
|
||||||
_LOGGER.error("No activity specified with turn_on service")
|
_LOGGER.error("No activity specified with turn_on service")
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Start the PowerOff activity."""
|
"""Start the PowerOff activity."""
|
||||||
self._client.power_off()
|
await self._client.power_off()
|
||||||
|
|
||||||
# pylint: disable=arguments-differ
|
# pylint: disable=arguments-differ
|
||||||
def send_command(self, commands, **kwargs):
|
async def async_send_command(self, command, **kwargs):
|
||||||
"""Send a list of commands to one device."""
|
"""Send a list of commands to one device."""
|
||||||
device = kwargs.get(ATTR_DEVICE)
|
device = kwargs.get(ATTR_DEVICE)
|
||||||
if device is None:
|
if device is None:
|
||||||
_LOGGER.error("Missing required argument: device")
|
_LOGGER.error("Missing required argument: device")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
device_id = None
|
||||||
|
if device.isdigit():
|
||||||
|
_LOGGER.debug("Device is numeric")
|
||||||
|
if self._client.get_device_name(int(device)):
|
||||||
|
device_id = device
|
||||||
|
|
||||||
|
if not device_id:
|
||||||
|
_LOGGER.debug("Find device ID based on device name")
|
||||||
|
device_id = self._client.get_activity_id(str(device).strip())
|
||||||
|
|
||||||
|
if not device_id:
|
||||||
|
_LOGGER.error("Device %s is invalid", device)
|
||||||
|
return
|
||||||
|
|
||||||
num_repeats = kwargs.get(ATTR_NUM_REPEATS)
|
num_repeats = kwargs.get(ATTR_NUM_REPEATS)
|
||||||
delay_secs = kwargs.get(ATTR_DELAY_SECS, self._delay_secs)
|
delay_secs = kwargs.get(ATTR_DELAY_SECS, self._delay_secs)
|
||||||
|
|
||||||
for _ in range(num_repeats):
|
for _ in range(num_repeats):
|
||||||
for command in commands:
|
for single_command in command:
|
||||||
self._client.send_command(device, command)
|
_LOGGER.debug("Sending command %s", single_command)
|
||||||
time.sleep(delay_secs)
|
await self._client.send_command(device, single_command)
|
||||||
|
await asyncio.sleep(delay_secs)
|
||||||
|
|
||||||
def sync(self):
|
async def sync(self):
|
||||||
"""Sync the Harmony device with the web service."""
|
"""Sync the Harmony device with the web service."""
|
||||||
import pyharmony
|
|
||||||
_LOGGER.debug("Syncing hub with Harmony servers")
|
_LOGGER.debug("Syncing hub with Harmony servers")
|
||||||
self._client.sync()
|
await self._client.sync()
|
||||||
self._config = self._client.get_config()
|
await self._client.get_config()
|
||||||
|
await self.hass.async_add_executor_job(self.write_config_file)
|
||||||
|
|
||||||
|
def write_config_file(self):
|
||||||
|
"""Write Harmony configuration file."""
|
||||||
_LOGGER.debug("Writing hub config to file: %s", self._config_path)
|
_LOGGER.debug("Writing hub config to file: %s", self._config_path)
|
||||||
pyharmony.ha_write_config_file(self._config, self._config_path)
|
try:
|
||||||
|
with open(self._config_path, 'w+', encoding='utf-8') as file_out:
|
||||||
|
json.dump(self._client.json_config, file_out,
|
||||||
|
sort_keys=True, indent=4)
|
||||||
|
except IOError as exc:
|
||||||
|
_LOGGER.error("Unable to write HUB configuration to %s: %s",
|
||||||
|
self._config_path, exc)
|
||||||
|
|
|
@ -520,6 +520,9 @@ homematicip==0.9.8
|
||||||
# homeassistant.components.remember_the_milk
|
# homeassistant.components.remember_the_milk
|
||||||
httplib2==0.10.3
|
httplib2==0.10.3
|
||||||
|
|
||||||
|
# homeassistant.components.remote.harmony
|
||||||
|
https://github.com/home-assistant//pyharmony/archive/4b27f8a35ea61123ef531ad078a4357cc26b00db.zip#pyharmony==1.0.21b0
|
||||||
|
|
||||||
# homeassistant.components.huawei_lte
|
# homeassistant.components.huawei_lte
|
||||||
huawei-lte-api==1.0.16
|
huawei-lte-api==1.0.16
|
||||||
|
|
||||||
|
@ -988,9 +991,6 @@ pygtfs==0.1.5
|
||||||
# homeassistant.components.sensor.gtt
|
# homeassistant.components.sensor.gtt
|
||||||
pygtt==1.1.2
|
pygtt==1.1.2
|
||||||
|
|
||||||
# homeassistant.components.remote.harmony
|
|
||||||
pyharmony==1.0.20
|
|
||||||
|
|
||||||
# homeassistant.components.sensor.version
|
# homeassistant.components.sensor.version
|
||||||
pyhaversion==2.0.3
|
pyhaversion==2.0.3
|
||||||
|
|
||||||
|
|
|
@ -209,7 +209,7 @@ def gather_modules():
|
||||||
for req in module.REQUIREMENTS:
|
for req in module.REQUIREMENTS:
|
||||||
if req in IGNORE_REQ:
|
if req in IGNORE_REQ:
|
||||||
continue
|
continue
|
||||||
if '://' in req:
|
if '://' in req and 'pyharmony' not in req:
|
||||||
errors.append(
|
errors.append(
|
||||||
"{}[Only pypi dependencies are allowed: {}]".format(
|
"{}[Only pypi dependencies are allowed: {}]".format(
|
||||||
package, req))
|
package, req))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue