I have written a custom sensor for a platform, and I’d like to code the update part so that it’s asynchronous: the update includes listening to UDP messages sent by the device every 60 seconds, so it is slow by design - and HA correctly issues warnings if I do the update synchronously.
I tried to replace the update() at L117 with async_update() decorated by @asyncio.coroutine, but the net result is that HA started to log many errors of other sensors not updating. With the current code, everything works except the regular HA warnings about my platform taking more than 30 seconds to update…
Hi! Thanks for the suggestions, looks like a promising direction - and as I also have to properly get my way around asyncio in python, this can be an interesting challenge.
No promises for now but I hope to report back some positive experience.
For the record, I add some time to play around with create_datagram_endpoint() and the direction was right: I have my sensor asynchronously receiving data, and HA does not show any warning any longer
I’ve put the updated code on my repo (linked above), maybe this is useful for the community.
Update, for the benefit of the community: it turns out that appending your own listener/updater class to HA’s main loop leads to race conditions and bad surprises, and using locks or loop.call_soon_threadsafe() does not help either, HA keeps crashing and rebooting itself
In lots of cases (but not always…) I get Bad file descriptor errors logged in a very tight loop, quite similarly to the issue below:
I’m back to synch updates for now. I’m sure I don’t understand enough of the core HA engine, but I wonder if there’s any fundamental issue here that makes this approach break.