diff --git a/homeassistant/components/remote/harmony.py b/homeassistant/components/remote/harmony.py index 07fd5831dbb..a5e4f5a8528 100644 --- a/homeassistant/components/remote/harmony.py +++ b/homeassistant/components/remote/harmony.py @@ -22,7 +22,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.exceptions import PlatformNotReady from homeassistant.util import slugify -REQUIREMENTS = ['aioharmony==0.1.2'] +REQUIREMENTS = ['aioharmony==0.1.5'] _LOGGER = logging.getLogger(__name__) @@ -106,6 +106,9 @@ async def async_setup_platform(hass, config, async_add_entities, try: device = HarmonyRemote( name, address, port, activity, harmony_conf_file, delay_secs) + if not await device.connect(): + raise PlatformNotReady + DEVICES.append(device) async_add_entities([device]) register_services(hass) @@ -152,33 +155,36 @@ class HarmonyRemote(remote.RemoteDevice): def __init__(self, name, host, port, activity, out_path, delay_secs): """Initialize HarmonyRemote class.""" - from aioharmony.harmonyapi import ( - HarmonyAPI as HarmonyClient, ClientCallbackType - ) + from aioharmony.harmonyapi import HarmonyAPI as HarmonyClient - _LOGGER.debug("%s: Device init started", name) self._name = name self.host = host self.port = port self._state = None self._current_activity = None self._default_activity = activity - self._client = HarmonyClient( - ip_address=host, - callbacks=ClientCallbackType( - new_activity=self.new_activity, - config_updated=self.new_config, - connect=self.got_connected, - disconnect=self.got_disconnected - ) - ) + self._client = HarmonyClient(ip_address=host) self._config_path = out_path self._delay_secs = delay_secs self._available = False async def async_added_to_hass(self): """Complete the initialization.""" + from aioharmony.harmonyapi import ClientCallbackType + _LOGGER.debug("%s: Harmony Hub added", self._name) + # Register the callbacks + self._client.callbacks = ClientCallbackType( + new_activity=self.new_activity, + config_updated=self.new_config, + connect=self.got_connected, + disconnect=self.got_disconnected + ) + + # Store Harmony HUB config, this will also update our current + # activity + await self.new_config() + import aioharmony.exceptions as aioexc async def shutdown(_): @@ -191,15 +197,6 @@ class HarmonyRemote(remote.RemoteDevice): self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown) - _LOGGER.debug("%s: Connecting", self._name) - try: - await self._client.connect() - except aioexc.TimeOut: - _LOGGER.error("%s: Connection timed-out", self._name) - else: - # Set initial state - self.new_activity(self._client.current_activity) - @property def name(self): """Return the Harmony device's name.""" @@ -225,6 +222,22 @@ class HarmonyRemote(remote.RemoteDevice): """Return True if connected to Hub, otherwise False.""" return self._available + async def connect(self): + """Connect to the Harmony HUB.""" + import aioharmony.exceptions as aioexc + + _LOGGER.debug("%s: Connecting", self._name) + try: + if not await self._client.connect(): + _LOGGER.warning("%s: Unable to connect to HUB.", self._name) + await self._client.close() + return False + except aioexc.TimeOut: + _LOGGER.warning("%s: Connection timed-out", self._name) + return False + + return True + def new_activity(self, activity_info: tuple) -> None: """Call for updating the current activity.""" activity_id, activity_name = activity_info @@ -340,7 +353,7 @@ class HarmonyRemote(remote.RemoteDevice): for _ in range(num_repeats): for single_command in command: send_command = SendCommandDevice( - device=device, + device=device_id, command=single_command, delay=0 ) @@ -360,8 +373,8 @@ class HarmonyRemote(remote.RemoteDevice): "%s: %s", result.command.command, result.command.device, - result.command.code, - result.command.msg + result.code, + result.msg ) async def change_channel(self, channel): diff --git a/requirements_all.txt b/requirements_all.txt index 5331e95ceee..b380ae85228 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -105,7 +105,7 @@ aiofreepybox==0.0.6 aioftp==0.12.0 # homeassistant.components.remote.harmony -aioharmony==0.1.2 +aioharmony==0.1.5 # homeassistant.components.emulated_hue # homeassistant.components.http