postlund / pyatv

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

Failed to find device with identifier #292

Closed Veldkornet closed 4 years ago

Veldkornet commented 4 years ago

Describe the bug After initially setting up the AppleTV in HomeAssistant 0.102.2 in Hass.io, you restart HomeAssistant, and the AppleTV no longer works. 2019-11-27 22:57:08 ERROR (MainThread) [custom_components.apple_tv] Failed to find device with identifier xxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx The only way to make it work again is to remove the AppleTv and re-add it again.

To Reproduce

Expected behavior

System Setup (please complete the following information):

Additional context

jayfbeast commented 4 years ago

I'm not sure to be honest, it just happens at seemingless random times. Usually zeroconf stops working at all, at least for atvremote. I have seen this behavior several times when I have Home Assistant running with my component connected to ATV. When running atvremote on the same machine usually return zero result. My best guess is that it has to do with port re-using (since you have to specify that a UDP can be re-used when binding to it), but I haven't investigated that further. It's also hard to tell if it's the same issue.

hmm hmm... how often the port usage check is made? I mean, when the port is being re-used, atvremote isn't aware of that change and everything stop working at that point?

Also, I've been running your component for a few hours only and so far, it is working flawlessly. After how much time you end up having issues? a few days? weeks?

postlund commented 4 years ago

With mDNS/Zeroconf, the request is multicasted out on the network to a specific address (224.0.0.251). All clients must listen on port 5353 UDP on this address. When a request is received a response is sent back to the requester at the same port, 5353. If an application already listens on this port you can't bind to it, that wouldn't normally make sense. Since UDP is datagram based and has no retransmissions, segmentation or other complex mechanisms, it works two have several listeners anyways and that they receive the same data. But you have to say that you want to reuse a port when binding to it. So you only do it initially.

Last time I saw it it happened instantly, more or less. I started Home Assistant and shortly thereafter I tried atvremote in another shell and it didn't find any hosts.

jayfbeast commented 4 years ago

So chances are the solution might reside somewhere else in mDNS/Zeronconf? I mean, you won't be able to take care of that issue from within pyatv or atvremote ?

postlund commented 4 years ago

Yes, I see that as an inherent risk. But that doesn't mean it's not solvable, we just have to make a PR somewhere else or point to some issue stating where it's potentially fixed. If it's a bug in python for instance, then it's not much more we can do then require a specific version of python in case it's fixed.

postlund commented 4 years ago

I'm trying to create a workaround for the scanning issues, please have a look at #343.

postlund commented 4 years ago

I have managed to complete #343 and integrate it with the component, but I need to do some clean up and additional testing of it. To summarize how it works:

This should work good for all users. My assumption is that scanning works sometimes, even for users that have problems. This approach hopefully makes the connection recover at some point in time. If it doesn't, then the integration must be removed and added again for now. I will have to look at an approach to edit IP-address manually in the future.

My intention is to release an update to the component today/tonight. I would be extremely happy to get some testers on this, especially if you have said problems.

This will be a breaking change: you must remove and re-add your integration when updating!

postlund commented 4 years ago

Updated version of the component is out now!

aidanblack commented 4 years ago

Do you have an updated version of the beta add-on? If so, could you post a link?

postlund commented 4 years ago

Latest commit in https://github.com/postlund/hass-atv-beta is always latest "version".

aidanblack commented 4 years ago

Thank you!

nikolaykasyanov commented 4 years ago

@postlund seems to be working great, thank you!

postlund commented 4 years ago

@nikolaykasyanov Great, thanks for trying! 👍 Did you by any chance test unicast scanning, I.e. adding device by specifying IP-address?

nikolaykasyanov commented 4 years ago

@postlund yes, I'm adding the device via IP and it survives HASS restarts.

postlund commented 4 years ago

@nikolaykasyanov Perfect, thanks again! 👍 Seems to be a problem lurking somewhere, as seen in #363, so it is good so see that it works for at least someone else.

postlund commented 4 years ago

Problem is partially solve via unicast scanning (entering IP-address). A foolproof solution is something we probably won's see near time. But I hope this is good enough.