From 476235a2599b375d9bd20cc7fa4a2453d876bb8c Mon Sep 17 00:00:00 2001
From: "J. Nick Koston" <nick@koston.org>
Date: Fri, 31 Jul 2020 09:55:38 -1000
Subject: [PATCH] Restore the ability to tell when a harmony activity is
 starting (#38335)

* Restore the ability to tell when a harmony activity is starting

* adjust for poweroff

* switch to activity name for activity starting

* adjust

* do not set starting on initial update
---
 homeassistant/components/harmony/const.py  |  1 +
 homeassistant/components/harmony/remote.py | 15 ++++++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/homeassistant/components/harmony/const.py b/homeassistant/components/harmony/const.py
index 26368810c83..f6315b57b57 100644
--- a/homeassistant/components/harmony/const.py
+++ b/homeassistant/components/harmony/const.py
@@ -10,4 +10,5 @@ ATTR_ACTIVITY_LIST = "activity_list"
 ATTR_DEVICES_LIST = "devices_list"
 ATTR_LAST_ACTIVITY = "last_activity"
 ATTR_CURRENT_ACTIVITY = "current_activity"
+ATTR_ACTIVITY_STARTING = "activity_starting"
 PREVIOUS_ACTIVE_ACTIVITY = "Previous Active Activity"
diff --git a/homeassistant/components/harmony/remote.py b/homeassistant/components/harmony/remote.py
index badd1cc9508..1ebcfbaa760 100644
--- a/homeassistant/components/harmony/remote.py
+++ b/homeassistant/components/harmony/remote.py
@@ -30,6 +30,7 @@ from homeassistant.helpers.restore_state import RestoreEntity
 from .const import (
     ACTIVITY_POWER_OFF,
     ATTR_ACTIVITY_LIST,
+    ATTR_ACTIVITY_STARTING,
     ATTR_CURRENT_ACTIVITY,
     ATTR_DEVICES_LIST,
     ATTR_LAST_ACTIVITY,
@@ -138,6 +139,8 @@ class HarmonyRemote(remote.RemoteEntity, RestoreEntity):
         self._state = None
         self._current_activity = ACTIVITY_POWER_OFF
         self.default_activity = activity
+        self._activity_starting = None
+        self._is_initial_update = False
         self._client = HarmonyClient(ip_address=host)
         self._config_path = out_path
         self.delay_secs = delay_secs
@@ -172,10 +175,15 @@ class HarmonyRemote(remote.RemoteEntity, RestoreEntity):
             "connect": self.got_connected,
             "disconnect": self.got_disconnected,
             "new_activity_starting": self.new_activity,
-            "new_activity": None,
+            "new_activity": self._new_activity_finished,
         }
         self._client.callbacks = ClientCallbackType(**callbacks)
 
+    def _new_activity_finished(self, activity_info: tuple) -> None:
+        """Call for finished updated current activity."""
+        self._activity_starting = None
+        self.async_write_ha_state()
+
     async def async_added_to_hass(self):
         """Complete the initialization."""
         await super().async_added_to_hass()
@@ -252,6 +260,7 @@ class HarmonyRemote(remote.RemoteEntity, RestoreEntity):
     def device_state_attributes(self):
         """Add platform specific attributes."""
         return {
+            ATTR_ACTIVITY_STARTING: self._activity_starting,
             ATTR_CURRENT_ACTIVITY: self._current_activity,
             ATTR_ACTIVITY_LIST: list_names_from_hublist(
                 self._client.hub_config.activities
@@ -288,6 +297,10 @@ class HarmonyRemote(remote.RemoteEntity, RestoreEntity):
         activity_id, activity_name = activity_info
         _LOGGER.debug("%s: activity reported as: %s", self._name, activity_name)
         self._current_activity = activity_name
+        if self._is_initial_update:
+            self._is_initial_update = False
+        else:
+            self._activity_starting = activity_name
         if activity_id != -1:
             # Save the activity so we can restore
             # to that activity if none is specified