Exclude homekit accessories created by the homekit integration from homekit_controller (#48201)

This commit is contained in:
J. Nick Koston 2021-03-21 19:18:24 -10:00 committed by GitHub
parent e5893ca42c
commit 7a447c4209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 16 deletions

View File

@ -18,8 +18,6 @@ from .const import DOMAIN, KNOWN_DEVICES
HOMEKIT_DIR = ".homekit" HOMEKIT_DIR = ".homekit"
HOMEKIT_BRIDGE_DOMAIN = "homekit" HOMEKIT_BRIDGE_DOMAIN = "homekit"
HOMEKIT_BRIDGE_SERIAL_NUMBER = "homekit.bridge"
HOMEKIT_BRIDGE_MODEL = "Home Assistant HomeKit Bridge"
HOMEKIT_IGNORE = [ HOMEKIT_IGNORE = [
# eufy Indoor Cam 2K and 2K Pan & Tilt # eufy Indoor Cam 2K and 2K Pan & Tilt
@ -181,8 +179,8 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
return self.async_abort(reason="no_devices") return self.async_abort(reason="no_devices")
async def _hkid_is_homekit_bridge(self, hkid): async def _hkid_is_homekit(self, hkid):
"""Determine if the device is a homekit bridge.""" """Determine if the device is a homekit bridge or accessory."""
dev_reg = await async_get_device_registry(self.hass) dev_reg = await async_get_device_registry(self.hass)
device = dev_reg.async_get_device( device = dev_reg.async_get_device(
identifiers=set(), connections={(CONNECTION_NETWORK_MAC, hkid)} identifiers=set(), connections={(CONNECTION_NETWORK_MAC, hkid)}
@ -190,7 +188,13 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
if device is None: if device is None:
return False return False
return device.model == HOMEKIT_BRIDGE_MODEL
for entry_id in device.config_entries:
entry = self.hass.config_entries.async_get_entry(entry_id)
if entry and entry.domain == HOMEKIT_BRIDGE_DOMAIN:
return True
return False
async def async_step_zeroconf(self, discovery_info): async def async_step_zeroconf(self, discovery_info):
"""Handle a discovered HomeKit accessory. """Handle a discovered HomeKit accessory.
@ -266,8 +270,8 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
if model in HOMEKIT_IGNORE: if model in HOMEKIT_IGNORE:
return self.async_abort(reason="ignored_model") return self.async_abort(reason="ignored_model")
# If this is a HomeKit bridge exported by *this* HA instance ignore it. # If this is a HomeKit bridge/accessory exported by *this* HA instance ignore it.
if await self._hkid_is_homekit_bridge(hkid): if await self._hkid_is_homekit(hkid):
return self.async_abort(reason="ignored_model") return self.async_abort(reason="ignored_model")
self.name = name self.name = name

View File

@ -269,25 +269,18 @@ async def test_discovery_ignored_model(hass, controller):
async def test_discovery_ignored_hk_bridge(hass, controller): async def test_discovery_ignored_hk_bridge(hass, controller):
"""Already paired.""" """Ensure we ignore homekit bridges and accessories created by the homekit integration."""
device = setup_mock_accessory(controller) device = setup_mock_accessory(controller)
discovery_info = get_device_discovery_info(device) discovery_info = get_device_discovery_info(device)
config_entry = MockConfigEntry(domain=config_flow.HOMEKIT_BRIDGE_DOMAIN, data={}) config_entry = MockConfigEntry(domain=config_flow.HOMEKIT_BRIDGE_DOMAIN, data={})
config_entry.add_to_hass(hass)
formatted_mac = device_registry.format_mac("AA:BB:CC:DD:EE:FF") formatted_mac = device_registry.format_mac("AA:BB:CC:DD:EE:FF")
dev_reg = mock_device_registry(hass) dev_reg = mock_device_registry(hass)
dev_reg.async_get_or_create( dev_reg.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
identifiers={
(
config_flow.HOMEKIT_BRIDGE_DOMAIN,
config_entry.entry_id,
config_flow.HOMEKIT_BRIDGE_SERIAL_NUMBER,
)
},
connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)}, connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)},
model=config_flow.HOMEKIT_BRIDGE_MODEL,
) )
discovery_info["properties"]["id"] = "AA:BB:CC:DD:EE:FF" discovery_info["properties"]["id"] = "AA:BB:CC:DD:EE:FF"
@ -300,6 +293,30 @@ async def test_discovery_ignored_hk_bridge(hass, controller):
assert result["reason"] == "ignored_model" assert result["reason"] == "ignored_model"
async def test_discovery_does_not_ignore_non_homekit(hass, controller):
"""Do not ignore devices that are not from the homekit integration."""
device = setup_mock_accessory(controller)
discovery_info = get_device_discovery_info(device)
config_entry = MockConfigEntry(domain="not_homekit", data={})
config_entry.add_to_hass(hass)
formatted_mac = device_registry.format_mac("AA:BB:CC:DD:EE:FF")
dev_reg = mock_device_registry(hass)
dev_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={(device_registry.CONNECTION_NETWORK_MAC, formatted_mac)},
)
discovery_info["properties"]["id"] = "AA:BB:CC:DD:EE:FF"
# Device is discovered
result = await hass.config_entries.flow.async_init(
"homekit_controller", context={"source": "zeroconf"}, data=discovery_info
)
assert result["type"] == "form"
async def test_discovery_invalid_config_entry(hass, controller): async def test_discovery_invalid_config_entry(hass, controller):
"""There is already a config entry for the pairing id but it's invalid.""" """There is already a config entry for the pairing id but it's invalid."""
MockConfigEntry( MockConfigEntry(