HuJohner / homebridge-appletv

Apache License 2.0
22 stars 7 forks source link

Add ‘state’ to Switch with polling interval #4

Open EasyBizz opened 1 year ago

EasyBizz commented 1 year ago

The plugin works great, only the switch state does not represent the ATV state if the ATV is turned on via remote or iphone.

I also use ‘homebridge-cmdswitch2-no-logs’ in combination with pyatv. I get the state with

/home/pi/.local/bin/atvremote --id {ATV ID} --airplay-credentials {AIRPLAY CREDENTIALS} --companion-credentials {COMPANION CREDENTIALS} power_state | grep -i 'PowerState.On'

Grep to get the state only if ‘On’.

Don’t know how the interval is handles though. But with the plugin it can be set.

joelwetzel commented 1 year ago

Hmmm, I have found that the switch state usually does update when I turn on my 1st-gen Apple TV 4K with its remote. Although once or twice, I thought it missed it. Wasn't 100% accurate.

CooperCGN commented 1 year ago

Switch behavior is inconsistent for me too, sometimes it turns itself off again after a short while. Maybe a problem with the new HomeKit architecture I am already on.

sbuchart commented 1 year ago

Power switch is not working on latest Apple TV. Motion sensors for playing and paused work perfectly, but power state does not change.

joelwetzel commented 1 year ago

I think I might understand why the power switch is turning itself off after a short while. I think the plugin does not handle all the events, especially one where homekit can ask for an update of the device state. Since the plugin doesn't handle it, homekit assumes the switch is off.

I'm going to try to work on it in my own fork.

CooperCGN commented 1 year ago

That actually might be it. I deinstalled the plug-in for now because it constantly kept crashing with all sorts of unhandled errors.

joelwetzel commented 1 year ago

I've made an improvement in my own fork and submitted a PR. It tracks the power state locally in the plugin. This definitely is improving the accuracy of the power state in the Home app. However, it can still get out of sync if HomeBridge restarts. Turns out the AppleTV has no way to query its power state! Crazy! The dev who built and maintains PyATV (which this plugin uses) has talked about his struggles against this extensively in these issues and others:

https://github.com/postlund/pyatv/issues/1785 https://github.com/postlund/pyatv/issues/1352

joelwetzel commented 1 year ago

Although I wonder if I could use HomeKit to cache the state, instead of Homebridge? I'll run a test on that this weekend.

CooperCGN commented 1 year ago

Maybe you can pull the power state on startup of Homebridge with atvremote power_state?

joelwetzel commented 1 year ago

Unfortunately, that's what I was referring to. atvremote/pyatv power_state is not reliable. It, also, is returning a cached version, based on what state changes it itself has made. The Apple TV itself has no interface for requesting/receiving power state.

joelwetzel commented 1 year ago

Ok, I got a version persisting the state to disk, using node-persist. That solves the "homebridge reboot problem". I'll test it for a few days for stability.

sbuchart commented 1 year ago

Glad to hear of your progress! I would be more than happy to test for you as well. I uninstalled the plugin since it was having issues, but could install it again if that would be helpful.

joelwetzel commented 1 year ago

Bad news. The node-persist version was less stable at maintaining power state. I'm going to try adjusting some of the node-persist settings. Maybe I got the timeouts/expirations wrong.