From eff59e8b00a5712460170c524004db91a548bdee Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 11 Sep 2021 19:00:26 -0700 Subject: [PATCH] Use the same server name for all HomeKit bridges (#55860) --- homeassistant/components/homekit/__init__.py | 6 ++++-- tests/components/homekit/test_homekit.py | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index b293f1f542d..7d2e60a53b9 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -519,7 +519,7 @@ class HomeKit: self.bridge = None self.driver = None - def setup(self, async_zeroconf_instance): + def setup(self, async_zeroconf_instance, uuid): """Set up bridge and accessory driver.""" persist_file = get_persist_fullpath_for_entry_id(self.hass, self._entry_id) @@ -534,6 +534,7 @@ class HomeKit: persist_file=persist_file, advertised_address=self._advertise_ip, async_zeroconf_instance=async_zeroconf_instance, + zeroconf_server=f"{uuid}-hap.local.", ) # If we do not load the mac address will be wrong @@ -713,7 +714,8 @@ class HomeKit: return self.status = STATUS_WAIT async_zc_instance = await zeroconf.async_get_async_instance(self.hass) - await self.hass.async_add_executor_job(self.setup, async_zc_instance) + uuid = await self.hass.helpers.instance_id.async_get() + await self.hass.async_add_executor_job(self.setup, async_zc_instance, uuid) self.aid_storage = AccessoryAidStorage(self.hass, self._entry_id) await self.aid_storage.async_initialize() if not await self._async_create_accessories(): diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index e9c9ad6662b..9b4ae477704 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -265,8 +265,9 @@ async def test_homekit_setup(hass, hk_driver, mock_zeroconf): hass.states.async_set("light.demo", "on") hass.states.async_set("light.demo2", "on") zeroconf_mock = MagicMock() + uuid = await hass.helpers.instance_id.async_get() with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver: - await hass.async_add_executor_job(homekit.setup, zeroconf_mock) + await hass.async_add_executor_job(homekit.setup, zeroconf_mock, uuid) path = get_persist_fullpath_for_entry_id(hass, entry.entry_id) mock_driver.assert_called_with( @@ -280,6 +281,7 @@ async def test_homekit_setup(hass, hk_driver, mock_zeroconf): persist_file=path, advertised_address=None, async_zeroconf_instance=zeroconf_mock, + zeroconf_server=f"{uuid}-hap.local.", ) assert homekit.driver.safe_mode is False @@ -307,8 +309,9 @@ async def test_homekit_setup_ip_address(hass, hk_driver, mock_zeroconf): mock_zeroconf = MagicMock() path = get_persist_fullpath_for_entry_id(hass, entry.entry_id) + uuid = await hass.helpers.instance_id.async_get() with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver: - await hass.async_add_executor_job(homekit.setup, mock_zeroconf) + await hass.async_add_executor_job(homekit.setup, mock_zeroconf, uuid) mock_driver.assert_called_with( hass, entry.entry_id, @@ -320,6 +323,7 @@ async def test_homekit_setup_ip_address(hass, hk_driver, mock_zeroconf): persist_file=path, advertised_address=None, async_zeroconf_instance=mock_zeroconf, + zeroconf_server=f"{uuid}-hap.local.", ) @@ -346,8 +350,9 @@ async def test_homekit_setup_advertise_ip(hass, hk_driver, mock_zeroconf): async_zeroconf_instance = MagicMock() path = get_persist_fullpath_for_entry_id(hass, entry.entry_id) + uuid = await hass.helpers.instance_id.async_get() with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver: - await hass.async_add_executor_job(homekit.setup, async_zeroconf_instance) + await hass.async_add_executor_job(homekit.setup, async_zeroconf_instance, uuid) mock_driver.assert_called_with( hass, entry.entry_id, @@ -359,6 +364,7 @@ async def test_homekit_setup_advertise_ip(hass, hk_driver, mock_zeroconf): persist_file=path, advertised_address="192.168.1.100", async_zeroconf_instance=async_zeroconf_instance, + zeroconf_server=f"{uuid}-hap.local.", )