karlg100 / homebridge-frigidaire

homebridge Platform plugin for Frigidaire connected appliances
29 stars 10 forks source link

Optimistic polling / stale polling issues #37

Open amaisano opened 3 years ago

amaisano commented 3 years ago

My AC units are connected to Home Assistant via the HomeKit Controller integration.

I set "Cool" in HA, and the AC beeps to the correct mode and HA shows the new mode, but then HA switches back to the previous mode. Only after another 15-30 seconds or so does HA update to the actual AC state. Same for temperature adjustments, etc.

This results in my smart home seeing the AC turn on, turn off, then turn back on again, which breaks nearly all of my automations.

This glitch is also visible directly inside Homebridge, but it's a delay instead (the status doesn't change until the next API poll).

TL;DR: Prevent in-between or stale data from causing update events after writing data. Be optimistic about the data write.

I have some feedback on why this could be happening / how it might be solved from one of the Home Assistant integration developers:

So theres a couple of different mechanisms at play, but it probably boils down to homebridge. So first of all the UI gives the backend a couple of seconds. So if you toggle a light, the UI will show the new state for 2-5s to give the backend chance to update, then flip back.

The next mechanism is that when a homekit accessory accepts a change, it is expected to be consistent - you should be able to read it straight back out. so homekit_controller will apply all successful writes as though they were poll results or notifications

so as soon as target device accepts the "cool me plz" state, homekit_controller acts as thought the device send a push notification to say its in the "cool me plz" state.

like i say, spec compliant certified devices - its consistent. you write a value, you can read it back. it's actually expected that you are "optimistic" in the spec - it explicitly doesn't send events for changes that you made your self. so if you turn a light on, you dont get a state change event to say its on, its expected that you know its on if you weren't optimistic you'd have to poll instead

now we get to homebridge every plugin is different [...] some run a 60s poll loop remember that HA does too before events in homekit_controller that meant [...] [a] random val from 1 to 60 + another random val from 1 to 60, and thats how much lag you would see before updates were visible we are still subject to the polling in homebridge though

so if the plugin isn't optimistic, it might accept a write but then serve stale information for a minute (or even longer i guess, its up to the plugin)

that shouldn't matter of course, because it shouldn't generate events for stale data

but.. HA still polls as sort of a backup in case events don't work [...] so the HA poll can revert to the sale information served by homebridge then when the homebridge plugin polls and gets the right state, it generates events that correct HA

[...]