From a8beae3c517210823d2ef901196a885d321d5e6e Mon Sep 17 00:00:00 2001
From: David Dix <david@dixieworld.co.uk>
Date: Fri, 12 Feb 2021 13:58:01 +0000
Subject: [PATCH] Add apple tv remote delay command (#46301)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
---
 homeassistant/components/apple_tv/remote.py | 23 ++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/homeassistant/components/apple_tv/remote.py b/homeassistant/components/apple_tv/remote.py
index a76c4c6a208..3d88bddcbc9 100644
--- a/homeassistant/components/apple_tv/remote.py
+++ b/homeassistant/components/apple_tv/remote.py
@@ -1,8 +1,14 @@
 """Remote control support for Apple TV."""
 
+import asyncio
 import logging
 
-from homeassistant.components.remote import RemoteEntity
+from homeassistant.components.remote import (
+    ATTR_DELAY_SECS,
+    ATTR_NUM_REPEATS,
+    DEFAULT_DELAY_SECS,
+    RemoteEntity,
+)
 from homeassistant.const import CONF_NAME
 
 from . import AppleTVEntity
@@ -43,12 +49,19 @@ class AppleTVRemote(AppleTVEntity, RemoteEntity):
 
     async def async_send_command(self, command, **kwargs):
         """Send a command to one device."""
+        num_repeats = kwargs[ATTR_NUM_REPEATS]
+        delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
+
         if not self.is_on:
             _LOGGER.error("Unable to send commands, not connected to %s", self._name)
             return
 
-        for single_command in command:
-            if not hasattr(self.atv.remote_control, single_command):
-                continue
+        for _ in range(num_repeats):
+            for single_command in command:
+                attr_value = getattr(self.atv.remote_control, single_command, None)
+                if not attr_value:
+                    raise ValueError("Command not found. Exiting sequence")
 
-            await getattr(self.atv.remote_control, single_command)()
+                _LOGGER.info("Sending command %s", single_command)
+                await attr_value()
+                await asyncio.sleep(delay)
-- 
GitLab