pyatv: Failed to find device with identifier
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
- Configure the AppleTv integration
- Restart HomeAssistant
Expected behavior
System Setup (please complete the following information):
- Os: Hass.io
- Python: 3.7
- pyatv: 5d20516
Additional context
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 65 (31 by maintainers)
My AppleTV seems to have resurrected itself after installing 2f16e2f (missing after reboot initially, installed the update, rebooted, AppleTV was picked up again during the startup). Still need to check if it survives additional reboots though…
Thank you!
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:
pyatv
will scan for that device only by sending a request directly to the device. This should work even when normal scans doesn’t work. Entering an identifier or name will trigger a scan just like before in order to find the device.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!
Home Assistant uses python-zeroconf internally for zeroconf (the one mentioned in pychromecast). I used that library as well until I couple of weeks ago, where I concluded that aiozeroconf worked a lot better for the users involved in the investigation. See issue for #214 for details. I know that there were discussion of switching to aiozeroconf in home assistant as well (since it’s natively asyncio), but there were some issue left. Looking at the issues in the projects GitHub page will reveal some interesting read. Also, all zeroconf implementations in python are forks of the same project. So at their cores, they are more or less the same thing.
Nevertheless, I do believe we will continue see problems like these going forward. It is not the correct way to use zeroconf like this. It is much better to have a daemon (like Anahi) that is always running, maintaining a cache of all discovered services. Applications can then just ask that daemon and get a service list. If a service is not available at one time, it will probably be later. I see missing and delayed discoveries of services all the time when using AirPlay (in iOS). It usually takes some time until devices appear, so not even the “golden standard” of implementations is foolproof. The important part is to have mitigations. The re-connect support should improve the situation, but I hope #293 will make it even better.
I mean, writing this:
or
Should result in the exact same thing as default value for timeout is 5. This is mainly what I’m asking if you did and it worked?
Adding a delay before is of course another matter, as that changes timing a bit. In theory it could mean that mDNS responses from Home Assistants zeroconf implementation are picked up (since it’s multicast traffic). But I still don’t understand why that would matter. I want to know the root cause, just adding s sleep for some random time is not something I’m gonna do as it’s very fragile. Maybe it works now for some people, but the problem likely still exists and will haunt other users.
I assume that it recovers with my latest update to the component?
As long as Home Assistant has network access, it should work. It must have otherwise none of the other integrations would work. You both seem to be running in docker (via hass.io or just plain docker). I haven’t tested it in docker myself yet, just run in plain. So I can try that tomorrow or so and see if I can reproduce. It would be a lot easier to troubleshoot if I can reproduce it myself.
Another data point: my local dev HASS installation running on Mac via venv is not affected by this issue.
@Veldkornet I also added info about VLAN
I have Hass.io running on a RaspberryPi 4 with HA version 0.102.2. RaspberryPi is connected via WiFi Apple TV is connected via cable. Both devices are on the same VLAN. Network is Ubiquiti UniFi.
With Hass.io though, if I’m not mistaken, the device itself stays connected to the network during a HomeAssistant restart. So the Docker container should have network access right from the start.