From ddf7ceecd40167585cdc243f538b4aac1f7ef404 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 27 Jul 2020 06:31:30 -1000 Subject: [PATCH] Prevent harmony from resetting state with multiple turn ons (#38183) Automations or HomeKit may turn the device on multiple times when the current activity is already active which will cause harmony to lose state. This behavior is unexpected as turning the device on when its already on isn't expected to reset state. --- homeassistant/components/harmony/remote.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/harmony/remote.py b/homeassistant/components/harmony/remote.py index 7b49321c7b0..badd1cc9508 100644 --- a/homeassistant/components/harmony/remote.py +++ b/homeassistant/components/harmony/remote.py @@ -136,7 +136,7 @@ class HarmonyRemote(remote.RemoteEntity, RestoreEntity): self._name = name self.host = host self._state = None - self._current_activity = None + self._current_activity = ACTIVITY_POWER_OFF self.default_activity = activity self._client = HarmonyClient(ip_address=host) self._config_path = out_path @@ -340,19 +340,33 @@ class HarmonyRemote(remote.RemoteEntity, RestoreEntity): if activity: activity_id = None + activity_name = None + if activity.isdigit() or activity == "-1": _LOGGER.debug("%s: Activity is numeric", self.name) - if self._client.get_activity_name(int(activity)): + activity_name = self._client.get_activity_name(int(activity)) + if activity_name: activity_id = activity if activity_id is None: _LOGGER.debug("%s: Find activity ID based on name", self.name) - activity_id = self._client.get_activity_id(str(activity)) + activity_name = str(activity) + activity_id = self._client.get_activity_id(activity_name) if activity_id is None: _LOGGER.error("%s: Activity %s is invalid", self.name, activity) return + if self._current_activity == activity_name: + # Automations or HomeKit may turn the device on multiple times + # when the current activity is already active which will cause + # harmony to loose state. This behavior is unexpected as turning + # the device on when its already on isn't expected to reset state. + _LOGGER.debug( + "%s: Current activity is already %s", self.name, activity_name + ) + return + try: await self._client.start_activity(activity_id) except aioexc.TimeOut: