Apple tv conecction comes and goes when is off

Hi, i´m having troubles with the apple tv, when is off home assistant detects the conection on and off continuously. In the mediaplayer the turn on and off button appears and dissapiers. The logs shows:


Logger: homeassistant.components.apple_tv
Source: components/apple_tv/__init__.py:181
Integration: Apple TV (documentation, issues)
First occurred: 11:01:15 (84 occurrences)
Last logged: 21:53:32

Connection lost to Apple TV "Apple TV 4K"

Logger: pyatv.support.http
Source: runner.py:188
First occurred: 11:01:11 (78 occurrences)
Last logged: 22:03:42

Got response without having a request: HttpResponse(protocol='RTSP', version='1.0', code=200, message='OK', headers={'Date': 'Sun, 03 Mar 2024 20:58:46 GMT', 'Content-Length': '55', 'Content-Type': 'application/x-apple-binary-plist', 'Server': 'AirTunes/755.3.1', 'CSeq': '11'}, body=b'bplist00\xd1\x01\x02Wstreams\xa0\x08\x0b\x13\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14')
Got response without having a request: HttpResponse(protocol='RTSP', version='1.0', code=200, message='OK', headers={'Date': 'Sun, 03 Mar 2024 21:00:03 GMT', 'Content-Length': '55', 'Content-Type': 'application/x-apple-binary-plist', 'Server': 'AirTunes/755.3.1', 'CSeq': '21'}, body=b'bplist00\xd1\x01\x02Wstreams\xa0\x08\x0b\x13\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14')
Got response without having a request: HttpResponse(protocol='RTSP', version='1.0', code=200, message='OK', headers={'Date': 'Sun, 03 Mar 2024 21:01:05 GMT', 'Content-Length': '55', 'Content-Type': 'application/x-apple-binary-plist', 'Server': 'AirTunes/755.3.1', 'CSeq': '20'}, body=b'bplist00\xd1\x01\x02Wstreams\xa0\x08\x0b\x13\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14')
Got response without having a request: HttpResponse(protocol='RTSP', version='1.0', code=200, message='OK', headers={'Date': 'Sun, 03 Mar 2024 21:02:04 GMT', 'Content-Length': '55', 'Content-Type': 'application/x-apple-binary-plist', 'Server': 'AirTunes/755.3.1', 'CSeq': '19'}, body=b'bplist00\xd1\x01\x02Wstreams\xa0\x08\x0b\x13\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14')
Got response without having a request: HttpResponse(protocol='RTSP', version='1.0', code=200, message='OK', headers={'Date': 'Sun, 03 Mar 2024 21:03:42 GMT', 'Content-Length': '55', 'Content-Type': 'application/x-apple-binary-plist', 'Server': 'AirTunes/755.3.1', 'CSeq': '20'}, body=b'bplist00\xd1\x01\x02Wstreams\xa0\x08\x0b\x13\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14')

Logger: homeassistant.components.apple_tv
Source: components/apple_tv/__init__.py:234
Integration: Apple TV (documentation, issues)
First occurred: 11:09:52 (19 occurrences)
Last logged: 21:14:55

Failed to connect
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
    return await fut
           ^^^^^^^^^
  File "/usr/local/lib/python3.12/asyncio/locks.py", line 212, in wait
    await fut
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/api.py", line 163, in _send_command
    resp = await self._protocol.exchange_opack(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/protocol.py", line 154, in exchange_opack
    return await self._exchange_generic_opack(frame_type, data, identifier, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/protocol.py", line 167, in _exchange_generic_opack
    unpacked_object = await self._queues[identifier].wait(timeout)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/support/collections.py", line 160, in wait
    await asyncio.wait_for(self._event.wait(), timeout)
  File "/usr/local/lib/python3.12/asyncio/tasks.py", line 519, in wait_for
    async with timeouts.timeout(timeout):
  File "/usr/local/lib/python3.12/asyncio/timeouts.py", line 115, in __aexit__
    raise TimeoutError from exc_val
TimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/__init__.py", line 234, in connect_once
    await self._connect(conf, raise_missing_credentials)
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/__init__.py", line 338, in _connect
    self.atv = await connect(conf, self.hass.loop, session=session)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/__init__.py", line 155, in connect
    await atv.connect()
  File "/usr/local/lib/python3.12/site-packages/pyatv/core/facade.py", line 684, in connect
    if await setup_data.connect():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/__init__.py", line 571, in _connect
    await api.connect()
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/api.py", line 145, in connect
    await self.system_info()
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/api.py", line 184, in system_info
    await self._send_command(
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/companion/api.py", line 174, in _send_command
    raise exceptions.ProtocolError(f"Command {identifier} failed") from ex
pyatv.exceptions.ProtocolError: Command _systemInfo failed

When is playing something all works perfect…I tried removing the device and added again but same issue

Any help will be appreciated

Hi, same Problem for me. I hope someone can help!

I wonder if this is the same problem I’m having.

In my case when I turn off Apple TV (via its remote or via HA) the Apple TV reports “standby” as expected, but in a minute or so it reports “off”. When this happens there’s an error in the HA logs:

2024-03-24 13:56:46.872 WARNING (MainThread) [homeassistant.components.apple_tv] Connection lost to Apple TV "Apple TV Family Room"
2024-03-24 13:58:00.633 ERROR (MainThread) [pyatv.protocols.airplay] Failed to set up remote control channel

What appears to happen is the Apple TV device goes to sleep and the connection is lost. I can no longer ping the IP of the Apple TV. In my case the Apple TV integration never restores, even if I power Apple TV back on using its remote. That is, I have to manually restart the Apple TV integration in Home Assistant.

This is pretty new behavior. I’ve been using HA to turn on my Apple TV for a year w/o any problem.

BTW, If I let the ping run sometimes the IP will respond, which might be causing intermittent connection in your case?

What’s interesting to me is I can wake up the Apple TV using my iPhone and Airplay. Makes me think there’s some kind of wake-on-lan that Airplay is using.

More info is at the pyatv issue I opened.

I’m really curious if others are having the same issue.

Same problem here with both Apple TVs:

Logger: homeassistant.components.apple_tv
Quelle: components/apple_tv/__init__.py:206
Integration: Apple TV (Dokumentation, Probleme)
Erstmals aufgetreten: 26. März 2024 um 20:11:23 (173 Vorkommnisse)
Zuletzt protokolliert: 28. März 2024 um 19:40:49

Connection lost to Apple TV "Essbereich"
Connection lost to Apple TV "Wohnzimmer"
Logger: homeassistant.components.apple_tv
Quelle: components/apple_tv/__init__.py:273
Integration: Apple TV (Dokumentation, Probleme)
Erstmals aufgetreten: 26. März 2024 um 20:13:46 (5 Vorkommnisse)
Zuletzt protokolliert: 10:12:18

Failed to connect
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/mrp/protocol.py", line 275, in _receive
    await semaphore.acquire()
  File "/usr/local/lib/python3.12/asyncio/locks.py", line 386, in acquire
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/__init__.py", line 273, in connect_once
    await self._connect_once(raise_missing_credentials)
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/__init__.py", line 258, in _connect_once
    await self._connect(conf, raise_missing_credentials)
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/__init__.py", line 378, in _connect
    self.atv = await connect(conf, self.hass.loop, session=session)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/__init__.py", line 155, in connect
    await atv.connect()
  File "/usr/local/lib/python3.12/site-packages/pyatv/core/facade.py", line 684, in connect
    if await setup_data.connect():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/mrp/__init__.py", line 1092, in _connect
    await protocol.start()
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/mrp/protocol.py", line 146, in start
    self.device_info = await self.send_and_receive(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/mrp/protocol.py", line 262, in send_and_receive
    return await self._receive(identifier, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pyatv/protocols/mrp/protocol.py", line 274, in _receive
    async with async_timeout.timeout(timeout):
  File "/usr/local/lib/python3.12/site-packages/async_timeout/__init__.py", line 141, in __aexit__
    self._do_exit(exc_type)
  File "/usr/local/lib/python3.12/site-packages/async_timeout/__init__.py", line 228, in _do_exit
    raise asyncio.TimeoutError
TimeoutError