Support unpairing homekit accessories from homekit_controller (#65065)

This commit is contained in:
Jc2k 2022-01-27 22:02:30 +00:00 committed by GitHub
parent e92078cf50
commit f49cfe866a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 25 deletions

View file

@ -2,6 +2,7 @@
from __future__ import annotations
import asyncio
import logging
from typing import Any
import aiohomekit
@ -26,6 +27,8 @@ from .connection import HKDevice, valid_serial_number
from .const import CONTROLLER, ENTITY_MAP, KNOWN_DEVICES, TRIGGERS
from .storage import EntityMapStorage
_LOGGER = logging.getLogger(__name__)
def escape_characteristic_name(char_name):
"""Escape any dash or dots in a characteristics name."""
@ -248,4 +251,21 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Cleanup caches before removing config entry."""
hkid = entry.data["AccessoryPairingID"]
# Remove cached type data from .storage/homekit_controller-entity-map
hass.data[ENTITY_MAP].async_delete_map(hkid)
# Remove the pairing on the device, making the device discoverable again.
# Don't reuse any objects in hass.data as they are already unloaded
async_zeroconf_instance = await zeroconf.async_get_async_instance(hass)
controller = aiohomekit.Controller(async_zeroconf_instance=async_zeroconf_instance)
controller.load_pairing(hkid, dict(entry.data))
try:
await controller.remove_pairing(hkid)
except aiohomekit.AccessoryDisconnectedError:
_LOGGER.warning(
"Accessory %s was removed from HomeAssistant but was not reachable "
"to properly unpair. It may need resetting before you can use it with "
"HomeKit again",
entry.title,
)