openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.87k stars 3.58k forks source link

[mqtt.homeassistant] When a component disappears from MQTT, the channels remain on the Thing in openHAB #17182

Open ccutrer opened 1 month ago

ccutrer commented 1 month ago

Expected Behavior

If I remove something in MQTT from Home Assistant discovery, I expect the channels for that component to disappear in openHAB, otherwise I'm left with cruft, especially if I'm say designing a new device in ESPHome and adding and removing components from it often. It should happen right away if the discovery topic receives a message explicitly deleting it (as opposed just not sending the discovery message again in the future, when not using retained discovery messages).

Current Behavior

I believe the configuration topic gets removed from the Thing configuration, but the channels remain.

Possible Solution

Probably set a time after a thing initializes, and go through all the channels to ensure their component still exists. Can be triggered right away if a topic is removed from the thing config.

Steps to Reproduce (for Bugs)

  1. Have a Home Assistant MQTT Device with multiple components
  2. Add the discovered thing from the Inbox
  3. Delete the discovery config for one of those components
  4. Observe that the channels still exist on the thing

Your Environment

lsiepel commented 4 days ago

Are there besides the merged PR any additional steps needed to finish cleanup when components are removed?

ccutrer commented 4 days ago

Yes. It should automatically remove those components' topics from the Thing configuration (at least for managed things). And it probably needs to do this even if it doesn't see the "delete" message - i.e. if it happens when openHAB isn't running, and then the binding starts, and doesn't receive a new discovery message within some timeout.