postlund / pyatv

A client library for Apple TV and AirPlay devices
https://pyatv.dev
MIT License
882 stars 97 forks source link

Home Assistant integration does not work with tvOS 15 #1333

Closed WanKa-zone closed 2 years ago

WanKa-zone commented 3 years ago

Describe the bug

An aborted noted popped up every time when I try to add my Apple TV into hass. image

Error log

<place log here>

How to reproduce the bug?

After typed in the PIN code displayed on Apple TV.

What is expected behavior?

Successfully add Apple TV Integration.

Operating System

core-2021.9.7

Python

Other

pyatv

not sure

Device

Apple TV 4K tvOS 15.0(19J346)

Additional context

Using tvOS 15.0(19J346) on my Apple TV 4K.

Smiggel commented 3 years ago

Same here. After entering the first pin (out of two - as it was before?) it stops working.

postlund commented 3 years ago

It is because of tvOS 15, there are a couple of issues about it. You either have to sit tight and wait until I have updated the integration or run the beta version until I'm done.

https://github.com/postlund/hass-atv-beta

(It's available in HACS)

Smiggel commented 3 years ago

It is because of tvOS 15, there are a couple of issues about it. You either have to sit tight and wait until I have updated the integration or run the beta version until I'm done.

https://github.com/postlund/hass-atv-beta

(It's available in HACS)

Where do I have to look for? I can't seem to find the beta in HACS.

DuncanRae commented 3 years ago

Hi,

I’ve installed the beta component and it sees all my Apple TV’s, and can integrate them with the pairing codes, although I do get the same error above, but HA still can’t see their state. I have all my light automations set up to dim based on their playing, paused etc. state. Feels like I’m back in the 80s having to manually dim the lights.

Apple TV state in HA just says Off. I did remove he, from HA before installing the beta component, and they have been detected but the integration isn’t working yet.

Thanks so much for your hard work on this. Really appreciate it and I really miss this function not working.

postlund commented 3 years ago

@Smiggel it should be called "Apple TV beta component" or something like that. If it doesn't show up, try adding the address manually. That should work.

@DuncanRae Can you check the log? Might be that the connection fails for some reason.

Smiggel commented 3 years ago

Installed the new beta and it works good for what I can see. HA sees my Apple TV. I was able to pair it and as soon as I play something on Netflix, I see it in HA! Nice.

Can't say if it stable yet. Have to test it more. But looks really promising! Thanks!

postlund commented 3 years ago

@Smiggel Sounds great! 👍 I'm very interested in stability related testing, so please report back on that!

Smiggel commented 3 years ago

@postlund Will do. Will test it this week.

DuncanRae commented 3 years ago

@Smiggel it should be called "Apple TV beta component" or something like that. If it doesn't show up, try adding the address manually. That should work.

@DuncanRae Can you check the log? Might be that the connection fails for some reason.

Hi,

The only thing I can find of interest in the log is this which is happening over and over again. I do see it mentioned in another thread too:

2021-09-25 20:53:14 DEBUG (MainThread) [custom_components.apple_tv] Discovering device DEFB5818-ABD3-4C56-A2FB-21A5248ACC44 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.support.knock] Knocking at ports [3689, 7000, 49152, 32498] on 192.168.0.64 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.core.scan] Auto-discovered Lounge Apple TV at 192.168.0.64:7000 via Protocol.AirPlay ({'acl': '0', 'btaddr': 'D0:D2:B0:9C:A4:5A', 'deviceid': 'D0:D2:B0:9C:A4:5B', 'fex': '1d9/St5/FbwI', 'features': '0x4A7FDFD5,0xBC157FDE', 'flags': '0x644', 'gid': '0789667B-D142-4802-9308-F055822F7444', 'igl': '1', 'gcgl': '1', 'model': 'AppleTV6,2', 'protovers': '1.1', 'pi': '16e57f49-27ed-4be3-a311-8cf2468ac8c4', 'psi': 'DD9F8738-B45B-475B-8901-34CD70478ADD', 'pk': '1d1aa49693908f1fcb1db0a14e0703296ba9ee180745e7c5d1210fbf6cf7b0a7', 'srcvers': '566.25.43', 'osvers': '15.0', 'vv': '2'}) 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.core.scan] Auto-discovered D0D2B09CA45B@Lounge Apple TV at 192.168.0.64:7000 via Protocol.RAOP ({'cn': '0,1,2,3', 'da': 'true', 'et': '0,3,5', 'ft': '0x4A7FDFD5,0xBC157FDE', 'sf': '0x644', 'md': '0,1,2', 'am': 'AppleTV6,2', 'pk': '1d1aa49693908f1fcb1db0a14e0703296ba9ee180745e7c5d1210fbf6cf7b0a7', 'tp': 'UDP', 'vn': '65537', 'vs': '566.25.43', 'ov': '15.0', 'vv': '2'}) 2021-09-25 20:53:14 DEBUG (MainThread) [custom_components.apple_tv] Failed to find device DEFB5818-ABD3-4C56-A2FB-21A5248ACC44 with address 192.168.0.64, trying to scan 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.support.net] Binding on *:5353 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.support.net] Binding on 127.0.0.1:0 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.support.net] Binding on 192.168.0.5:0 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.support.net] Binding on 172.30.32.1:0 2021-09-25 20:53:14 DEBUG (MainThread) [pyatv.support.net] Binding on 172.17.0.1:0

I did completely remove the devices from HA before re-adding with the new beta component. I also cleaned out all the old pyatv references on the Apple TV itself.

postlund commented 3 years ago

@DuncanRae It's the same situation as in #1335. It has taken some time for me to grasp the situation, but I know exactly why this happens. Will try to work out a fix as soon as I can.

DuncanRae commented 3 years ago

@DuncanRae It's the same situation as in #1335. It has taken some time for me to grasp the situation, but I know exactly why this happens. Will try to work out a fix as soon as I can.

Awesome. Really appreciate the work!

DAaviide commented 3 years ago

i cant find the beta in hacs

WanKa-zone commented 3 years ago

It is because of tvOS 15, there are a couple of issues about it. You either have to sit tight and wait until I have updated the integration or run the beta version until I'm done.

https://github.com/postlund/hass-atv-beta

(It's available in HACS)

Cheers. Really appreciate.

Couldn't find the "Apple TV beta component" or "pyatv" on HACS. How do I install manually?

Smiggel commented 3 years ago

So far so good. Ran multiple test today with Netflix and Disney+ and it’s running fine. No problems so far.

DAaviide commented 3 years ago

It is because of tvOS 15, there are a couple of issues about it. You either have to sit tight and wait until I have updated the integration or run the beta version until I'm done. https://github.com/postlund/hass-atv-beta (It's available in HACS)

Cheers. Really appreciate.

Couldn't find the "Apple TV beta component" or "pyatv" on HACS. How do I install manually?

same here😅 how can i do it

postlund commented 3 years ago

Manually adding a repo can be done like this:

https://hacs.xyz/docs/faq/custom_repositories

Just use https://github.com/postlund/hass-atv-beta as address.

WanKa-zone commented 3 years ago

Manually adding a repo can be done like this:

https://hacs.xyz/docs/faq/custom_repositories

Just use https://github.com/postlund/hass-atv-beta as address.

Got it. Cheers

mpeterson commented 3 years ago

@postlund even with the config version bump that you pushed around 1 hour ago I'm still facing issues. I made sure to delete all references from ./storage/core.config_entries too.

My logs show:

Authentication problem

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/__init__.py", line 33, in error_handler
    return await func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/auth.py", line 54, in start_pairing
    resp = await self.protocol.exchange_opack(
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/protocol.py", line 80, in exchange_opack
    unpacked_object, _ = opack.unpack(payload)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/opack.py", line 108, in unpack
    return _unpack(data, [])
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/opack.py", line 115, in _unpack
    if data[0] == 0x01:
IndexError: index out of range

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

Traceback (most recent call last):
  File "/config/custom_components/apple_tv/config_flow.py", line 285, in async_pair_next_protocol
    await self.pairing.begin()
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/pairing.py", line 53, in begin
    await error_handler(
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/__init__.py", line 41, in error_handler
    raise fallback(str(ex)) from ex
pyatv.exceptions.PairingError: index out of range

This happens after the second input of an AirPlay pin.

When I go to "Remote App and Devices" I have under devices "Unknown Device" entries as many times I tried to pair HASS to the AppleTV

postlund commented 3 years ago

@mpeterson That looks like a bug or something I have not implemented in OPACK to me, cool! It would be great if you either enable debug logs for pyatv (https://github.com/postlund/hass-atv-beta#debug-logs) or try to reproduce with atvremote like this:

atvremote --debug -s <ip to the device> --protocol companion pair
filikun commented 3 years ago

It seems I have the same problem (with authentication) as above and got a log. @postlund I sent you a PM on discord!

robyevolution commented 3 years ago

with the second beta version the player is always off, I tried to remove and reinstall the integration but it doesn't solve it

postlund commented 3 years ago

with the second beta version the player is always off, I tried to remove and reinstall the integration but it doesn't solve it

What does the log say?

robyevolution commented 3 years ago
2021-09-28 18:02:31 WARNING (MainThread) [pyatv.protocols.airplay] Failed to set up remote control channel: not authenticated
2021-09-28 18:02:31 ERROR (MainThread) [custom_components.apple_tv] Failed to connect
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/apple_tv/__init__.py", line 261, in _connect_loop
    await self._connect(conf)
  File "/home/homeassistant/.homeassistant/custom_components/apple_tv/__init__.py", line 337, in _connect
    self.atv = await connect(conf, self.hass.loop, session=session)
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/__init__.py", line 96, in connect
    await atv.connect()
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/core/facade.py", line 425, in connect
    if await setup_data.connect():
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/__init__.py", line 879, in _connect
    await protocol.start()
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/protocol.py", line 119, in start
    await self.connection.connect()
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/connection.py", line 100, in connect
    await self.loop.create_connection(lambda: self, self.host, self.port)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "/usr/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 496, in sock_connect
    return await fut
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 528, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('192.168.1.110', 0)
2021-09-28 18:02:34 WARNING (MainThread) [pyatv.protocols.airplay] Failed to set up remote control channel: not authenticated
2021-09-28 18:02:34 ERROR (MainThread) [custom_components.apple_tv] Failed to connect
Traceback (most recent call last):
  File "/home/homeassistant/.homeassistant/custom_components/apple_tv/__init__.py", line 261, in _connect_loop
    await self._connect(conf)
  File "/home/homeassistant/.homeassistant/custom_components/apple_tv/__init__.py", line 337, in _connect
    self.atv = await connect(conf, self.hass.loop, session=session)
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/__init__.py", line 96, in connect
    await atv.connect()
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/core/facade.py", line 425, in connect
    if await setup_data.connect():
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/__init__.py", line 879, in _connect
    await protocol.start()
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/protocol.py", line 119, in start
    await self.connection.connect()
  File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/connection.py", line 100, in connect
    await self.loop.create_connection(lambda: self, self.host, self.port)
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "/usr/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 496, in sock_connect
    return await fut
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 528, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('192.168.1.110', 0)
postlund commented 3 years ago

@robyevolution Did AirPlay succeed to pair when you added the device?

robyevolution commented 3 years ago

it is added correctly but the entity is always off, tried to restart home assistant and apple tv but the problem persists

postlund commented 3 years ago

This line:

2021-09-28 18:02:31 WARNING (MainThread) [pyatv.protocols.airplay] Failed to set up remote control channel: not authenticated

Means that the MRP-over-AirPlay channel failed to set up, so there's basically nothing that provides metadata and that's why the device appears to be off. We need to figure out why that is happening. What device is this? Can you install pyatv so we can run atvremote? Or run it via docker?

robyevolution commented 3 years ago

home assistant is installed on a proxmox container so I can update pyatv, with beta 1 it worked fine even if the MRP error came out. i have a 4k tv apple

postlund commented 3 years ago

Ok, that's interesting. Can you open .storage/core.config_entries and look for your device there, hopefully you find a credentials map containing the key "3" mapping to a long string (the AirPlay credentials), is that the case? The string should be four values in hex, concatenated by colons.

If the above is true, then you should verify the credentials with atvremote like this:

atvremote -s <device ip> --airplay-credentials <value from above> --scan-protocol airplay playing

It should hopefully show what's playing. Instructions for docker is here:

https://pyatv.dev/documentation/#container-docker

You can use tag v0.9.2 as that aligns with Home Assistant.

robyevolution commented 3 years ago

give me this error:

2021-09-28 19:34:07 WARNING [pyatv.protocols.airplay]: Failed to set up remote control channel: not authenticated Traceback (most recent call last): File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/scripts/atvremote.py", line 716, in _run_application return await cli_handler(loop) File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/scripts/atvremote.py", line 510, in cli_handler return await _handle_commands(args, config, loop) File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/scripts/atvremote.py", line 601, in _handle_commands atv = await connect(config, loop, protocol=args.protocol) File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/init.py", line 96, in connect await atv.connect() File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/core/facade.py", line 425, in connect if await setup_data.connect(): File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/init.py", line 879, in _connect await protocol.start() File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/protocol.py", line 119, in start await self.connection.connect() File "/srv/homeassistant/lib/python3.8/site-packages/pyatv/protocols/mrp/connection.py", line 100, in connect await self.loop.create_connection(lambda: self, self.host, self.port) File "/usr/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection raise exceptions[0] File "/usr/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection sock = await self._connect_sock( File "/usr/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock await self.sock_connect(sock, address) File "/usr/lib/python3.8/asyncio/selector_events.py", line 496, in sock_connect return await fut File "/usr/lib/python3.8/asyncio/selector_events.py", line 528, in _sock_connect_cb raise OSError(err, f'Connect call failed {address}') ConnectionRefusedError: [Errno 111] Connect call failed ('192.168.1.110', 0)

robyevolution commented 3 years ago

2021-09-28 20:55:53 ERROR (MainThread) [custom_components.apple_tv] Failed to connect Traceback (most recent call last): File "/home/homeassistant/.homeassistant/custom_components/apple_tv/init.py", line 259, in _connect_loop conf = await self._scan() File "/home/homeassistant/.homeassistant/custom_components/apple_tv/init.py", line 290, in _scan identifiers = set(self.config_entry.data[CONF_IDENTIFIERS]) KeyError: 'identifiers'

robyevolution commented 3 years ago

Now it works, the problem was that I had a static airplay password, removed the static password works fine.

postlund commented 3 years ago

Ah, right, I should add a check for that (pyatv can detect if it is needed).

mpeterson commented 3 years ago

@postlund I have the debug logs you requested. It has a lot of PII though, how can I give them to you without exposing it here?

mpeterson commented 3 years ago

@postlund sent them via email to you

postlund commented 3 years ago

@postlund sent them via email to you

Perfect, thank you! Looks like the same issue as @filikun is seeing: some kind of NoOp is sent back. I'm looking into it.

filikun commented 3 years ago

@mpeterson You seem to have the same problem as myself, do you happen to have the Apple TV on another VLAN than your Home Assistant?

mpeterson commented 3 years ago

@filikun I actually do! How did you even think about that?

filikun commented 3 years ago

@filikun I actually do! How did you even think about that?

@postlund so this might be it.

@mpeterson thing is I have this Xiaomi fan that just won’t work when on another vlan. But then the ATV did work before tvOS15 so I’m not sure what changed.

I got it to work by putting the ATV on the same network as HA.

postlund commented 3 years ago

@filikun Yes, it is most likely the case. It's a reasonable restriction, so I'm not that surprised. I'm re-working the message handling in Companion to support events, which will allow me to handle this case in a more elegant fashion. Would be great to have yet someone else confirm this, just to be sure.

lafferlaffer commented 3 years ago

Hi @postlund First of all, thank you so much for your work on this. It is much appreciated.

I installed the beta and it seems to be working great, so I just have a question; How is the 'State' update handled when going from 'paused' to 'idle'? Have you made any changes in this version? It seems to be changing to an 'idle' state much quicker now. Automations relying on the 'paused' state doesn't seem to work as (i) intended anymore.

Edit: After further testing, it seems more and more like a "stability related issue". My Apple TV is now playing, but the 'State' says 'paused'.

Again; thank you 🤓

postlund commented 3 years ago

@lafferlaffer It should be more or less the same I think. I have not made and significant changes to how state handling works, I merely update based on whatever the device sends to me.

Did you by any chance use play_media (or play_url if we are talking pyatv notion)? It will unfortunately break state on the device more or less completely, requiring a reboot to recover.

lafferlaffer commented 3 years ago

@postlund ; just so there's no confusion, I'm using hass-atv-beta because I upgraded to tvOS15. The entity I'm looking at (and use for automation) is the _media_player_.

postlund commented 3 years ago

@lafferlaffer That is correct!

lafferlaffer commented 3 years ago

@postlund Ok, I just thought you'd like to know in case this is a bug in the beta. The state of my Apple TV has been paused for a few hours now, even though we've been pausing and playing.

postlund commented 3 years ago

@lafferlaffer It might be, please check the log. Hopefully there's something there.

lafferlaffer commented 3 years ago

@postlund ; I found this if it's any use:

` Logger: homeassistant Source: custom_components/apple_tv/config_flow.py:111 Integration: Apple TV First occurred: 17:54:33 (16 occurrences) Last logged: 17:54:38 Error doing job: Task exception was never retrieved

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 153, in async_init flow, result = await task File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 179, in _async_init result = await self._async_handle_step(flow, flow.init_step, data, init_done) File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 260, in _async_handle_step result: FlowResult = await getattr(flow, method)(user_input) File "/config/custom_components/apple_tv/config_flow.py", line 177, in async_step_zeroconf return await self.async_find_device_wrapper(self.async_found_zeroconf_device) File "/config/custom_components/apple_tv/config_flow.py", line 203, in async_find_device_wrapper return await next_func() File "/config/custom_components/apple_tv/config_flow.py", line 181, in async_found_zeroconf_device await self.async_set_unique_id(self.device_identifier) File "/config/custom_components/apple_tv/config_flow.py", line 111, in device_identifier if self.atv.identifier in entry.data[CONF_IDENTIFIERS]: KeyError: 'identifiers' `

danwetherald commented 3 years ago

Updating to Beta fixed my MRP pairing failure 🙌🏻

postlund commented 3 years ago

@lafferlaffer Yeah, that's the reason. I should not have left it in this state. You either have to revert to the previous version and remove the integrations before updating or manually remove them from .storage/core.config_entries. Hopefully this will not be a problem in future updates.

RaZer0r commented 3 years ago

I also have my Atv 4K running on a seperate VLAN, and am unable to pair. If you need me to debug, give me a heads up :)

Currently debug is activated and I am running the beta from HACS.

Current error log:

Logger: custom_components.apple_tv.config_flow
Source: custom_components/apple_tv/config_flow.py:285 
Integration: apple_tv 
First occurred: 10:23:50 PM (1 occurrences) 
Last logged: 10:23:50 PM

Authentication problem
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/__init__.py", line 33, in error_handler
    return await func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/auth.py", line 54, in start_pairing
    resp = await self.protocol.exchange_opack(
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/protocol.py", line 80, in exchange_opack
    unpacked_object, _ = opack.unpack(payload)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/opack.py", line 108, in unpack
    return _unpack(data, [])
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/opack.py", line 115, in _unpack
    if data[0] == 0x01:
IndexError: index out of range

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

Traceback (most recent call last):
  File "/config/custom_components/apple_tv/config_flow.py", line 285, in async_pair_next_protocol
    await self.pairing.begin()
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/companion/pairing.py", line 53, in begin
    await error_handler(
  File "/usr/local/lib/python3.9/site-packages/pyatv/support/__init__.py", line 41, in error_handler
    raise fallback(str(ex)) from ex
pyatv.exceptions.PairingError: index out of range

If you need more info, let me know ;)

postlund commented 3 years ago

@RaZer0r It would be great if you could move it so HA and the Apple TV is on the same VLAN. If that works, then we can pretty much assume that Companion won't work cross-VLAN..