home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.33k stars 30.63k forks source link

HomeKit entity state desyncs after unsuccessful action #124338

Open bob-lobster opened 2 months ago

bob-lobster commented 2 months ago

The problem

When changing the state of an entity through HomeKit but the operation fails and the entity remains in the previous state in Home Assistant the correct state is not sent back to HomeKit and HomeKit will show the wrong state until its state changes again.

To replicate:

  1. Create a template switch that follows some other entity but without a turn on action.
  2. Expose the template switch to HomeKit.
  3. Turn the switch on through HomeKit.
  4. In Home Assistant the state will remain off and no change will be registered.
  5. HomeKit state will remain on even though it is off.
  6. Turn the other entity on in Home Assistant and back off.
  7. The state in HomeKit will correctly switch to off.

I would expect the behaviour to be as in the Home Assistant UI: The switch will show as on for a second and then off again.

I noticed the behaviour originally when a TV and receiver media player exposed to HomeKit was unable to be turned on and the state remained on indefinitely even though the entity was off the entire time. If this is the same issue it seems to be an easy way to replicate it.

https://github.com/user-attachments/assets/8aad2034-6053-4358-9471-55a5e4061f2f

What version of Home Assistant Core has the issue?

core-2024.8.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

homekit

Link to integration documentation on our website

https://www.home-assistant.io/integrations/homekit/

Diagnostics information

config_entry-homekit-01J5RBG023BC0JXA0XTA2TPYFE.json

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 2 months ago

Hey there @bdraco, mind taking a look at this issue as it has been labeled with an integration (homekit) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `homekit` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign homekit` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


homekit documentation homekit source (message by IssueLinks)

bdraco commented 2 months ago

The Home Assistant frontend client polls the state after ~3s to see if the state change actually happened after you flip a switch.

The iOS HomeKit client doesn't do that, and it always assumes the turn on action was successful.

There are no settings we can pass to iOS that would enable the kind of behavior you want.

bdraco commented 2 months ago

You could implement a 3s re-check of the state with something like this https://github.com/home-assistant/core/commit/d87267cef9366b0951638a283333d760e20bc8cb

bdraco commented 2 months ago

https://github.com/home-assistant/core/tree/hk_resync

I'm not so sure its a good idea to make this the default behavior though as it will likely be a breaking change for people who relied on it not resyncing state

bob-lobster commented 2 months ago

I‘m sorry I know nothing of the inner workings but with that change would the behaviour be like this?

  1. HK sends „turn on“ to HA and makes its own assumed state on
  2. HA tries to execute that command and after 3s checks if the command worked and assumed state == actual state and if not sends „entity is off“ / „turned off“ back to HK

I absolutely believe though that the default behaviour should be that when I open HK I will see the same as in HA.