diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index af21bbdaf84..94a6b2426f5 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -12,6 +12,7 @@ from uuid import UUID from aiohttp import web from pyhap.const import STANDALONE_AID +from pyhap.loader import get_loader import voluptuous as vol from zeroconf.asyncio import AsyncZeroconf @@ -204,6 +205,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the HomeKit from yaml.""" hass.data.setdefault(DOMAIN, {})[PERSIST_LOCK] = asyncio.Lock() + # Initialize the loader before loading entries to ensure + # there is no race where multiple entries try to load it + # at the same time. + await hass.async_add_executor_job(get_loader) + _async_register_events_and_services(hass) if DOMAIN not in config: @@ -516,6 +522,7 @@ class HomeKit: advertised_address=self._advertise_ip, async_zeroconf_instance=async_zeroconf_instance, zeroconf_server=f"{uuid}-hap.local.", + loader=get_loader(), ) # If we do not load the mac address will be wrong diff --git a/tests/components/homekit/test_homekit.py b/tests/components/homekit/test_homekit.py index 8a8c32d3272..ff0f3f4d72b 100644 --- a/tests/components/homekit/test_homekit.py +++ b/tests/components/homekit/test_homekit.py @@ -218,6 +218,7 @@ async def test_homekit_setup(hass, hk_driver, mock_async_zeroconf): advertised_address=None, async_zeroconf_instance=zeroconf_mock, zeroconf_server=f"{uuid}-hap.local.", + loader=ANY, ) assert homekit.driver.safe_mode is False @@ -259,6 +260,7 @@ async def test_homekit_setup_ip_address(hass, hk_driver, mock_async_zeroconf): advertised_address=None, async_zeroconf_instance=mock_async_zeroconf, zeroconf_server=f"{uuid}-hap.local.", + loader=ANY, ) @@ -300,6 +302,7 @@ async def test_homekit_setup_advertise_ip(hass, hk_driver, mock_async_zeroconf): advertised_address="192.168.1.100", async_zeroconf_instance=async_zeroconf_instance, zeroconf_server=f"{uuid}-hap.local.", + loader=ANY, )