HAP-python icon indicating copy to clipboard operation
HAP-python copied to clipboard

Need to remove bridge from iPhone after adding/removing devices?

Open ArjanM83 opened this issue 5 years ago • 5 comments

I'm creating a bridge and adding two accessories,

  • a light (on/off)
  • a light that can be dimmed and has a brightness parameter. When adding the bridge on my iPhone, both lights work fine.

Then I'm shutting down the bridge and change the accessories to

  • a light that can be dimmed and has a brightness parameter
  • a motion sensor.

I did not touch the accessory.state file that was created the first time when adding the bridge. Now the accessories on my iPhone are corrupt. The lights are not working correctly and the motion sensor is missing. I need to remove the bridge from my iPhone and re-add it, then everything is working fine.

When just adding extra lights, or switches, or removing these, the bridge and accessories seem to be updated automatically and correct on my iPhone.

Is it normal/expected to have to remove and re-add the bridge after changing the configuration of accessories?

ArjanM83 avatar Dec 07 '20 11:12 ArjanM83

We handle this in Home Assistant with a reset accessory service

https://github.com/home-assistant/core/blob/dev/homeassistant/components/homekit/init.py#L484

bdraco avatar Feb 12 '21 16:02 bdraco

I'm creating a bridge and adding two accessories,

  • a light (on/off)
  • a light that can be dimmed and has a brightness parameter. When adding the bridge on my iPhone, both lights work fine.

Then I'm shutting down the bridge and change the accessories to

  • a light that can be dimmed and has a brightness parameter
  • a motion sensor.

I did not touch the accessory.state file that was created the first time when adding the bridge. Now the accessories on my iPhone are corrupt. The lights are not working correctly and the motion sensor is missing. I need to remove the bridge from my iPhone and re-add it, then everything is working fine.

When just adding extra lights, or switches, or removing these, the bridge and accessories seem to be updated automatically and correct on my iPhone.

Is it normal/expected to have to remove and re-add the bridge after changing the configuration of accessories?

My understanding is, if you want a full refresh you need to delete the state file , delete the bridge on your phone, then restart/re-pair the bridge and load all definitions (classes/accessories) again.

Pythonaire avatar Feb 21 '21 08:02 Pythonaire

All config_changed

bdraco avatar Feb 21 '21 09:02 bdraco

All config_changed

@bdraco sounds great. Is where an example, how to use config_changed() ?

Pythonaire avatar Mar 08 '21 07:03 Pythonaire

    def reset_accessories(self, entity_ids):
        """Reset the accessory to load the latest configuration."""
        if not self.bridge:
            self.driver.config_changed()
            return

        aid_storage = self.hass.data[DOMAIN][self._entry_id][AID_STORAGE]
        removed = []
        for entity_id in entity_ids:
            aid = aid_storage.get_or_allocate_aid_for_entity_id(entity_id)
            if aid not in self.bridge.accessories:
                continue

            _LOGGER.info(
                "HomeKit Bridge %s will reset accessory with linked entity_id %s",
                self._name,
                entity_id,
            )

            acc = self.remove_bridge_accessory(aid)
            removed.append(acc)

        if not removed:
            # No matched accessories, probably on another bridge
            return

        self.driver.config_changed()

        for acc in removed:
            self.bridge.add_accessory(acc)
        self.driver.config_changed()

bdraco avatar Mar 08 '21 08:03 bdraco

def reset_accessories(self, entity_ids): """Reset the accessory to load the latest configuration.""" if not self.bridge: self.driver.config_changed() return

    aid_storage = self.hass.data[DOMAIN][self._entry_id][AID_STORAGE]
    removed = []
    for entity_id in entity_ids:
        aid = aid_storage.get_or_allocate_aid_for_entity_id(entity_id)
        if aid not in self.bridge.accessories:
            continue

        _LOGGER.info(
            "HomeKit Bridge %s will reset accessory with linked entity_id %s",
            self._name,
            entity_id,
        )

        acc = self.remove_bridge_accessory(aid)
        removed.append(acc)

    if not removed:
        # No matched accessories, probably on another bridge
        return

    self.driver.config_changed()

    for acc in removed:
        self.bridge.add_accessory(acc)
    self.driver.config_changed()

Trying to adopt that to the "normal" HAP-Python scripts (without HASS). Do i have to access the *.state file and read a python list in the same way as the self.hass.data?

Pythonaire avatar Dec 16 '22 10:12 Pythonaire