Migrate homekit to use async_at_started (#113102)

pull/113119/head
J. Nick Koston 2024-03-11 14:08:02 -10:00 committed by GitHub
parent 5dc44500c3
commit 6e59d1cb29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 112 additions and 103 deletions

View File

@ -45,13 +45,11 @@ from homeassistant.const import (
CONF_IP_ADDRESS,
CONF_NAME,
CONF_PORT,
EVENT_HOMEASSISTANT_STARTED,
EVENT_HOMEASSISTANT_STOP,
SERVICE_RELOAD,
)
from homeassistant.core import (
CALLBACK_TYPE,
CoreState,
HomeAssistant,
ServiceCall,
State,
@ -75,6 +73,7 @@ from homeassistant.helpers.service import (
async_extract_referenced_entity_ids,
async_register_admin_service,
)
from homeassistant.helpers.start import async_at_started
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import IntegrationNotFound, async_get_integration
@ -358,10 +357,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)
hass.data[DOMAIN][entry.entry_id] = entry_data
if hass.state is CoreState.running:
async def _async_start_homekit(hass: HomeAssistant) -> None:
await homekit.async_start()
else:
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, homekit.async_start)
entry.async_on_unload(async_at_started(hass, _async_start_homekit))
return True
@ -553,7 +552,6 @@ class HomeKit:
"""Set up bridge and accessory driver."""
assert self.iid_storage is not None
persist_file = get_persist_fullpath_for_entry_id(self.hass, self._entry_id)
self.driver = HomeDriver(
self.hass,
self._entry_id,
@ -569,7 +567,6 @@ class HomeKit:
loader=get_loader(),
iid_storage=self.iid_storage,
)
# If we do not load the mac address will be wrong
# as pyhap uses a random one until state is restored
if os.path.exists(persist_file):

View File

@ -1,6 +1,6 @@
"""Test the HomeKit config flow."""
from unittest.mock import patch
from unittest.mock import AsyncMock, Mock, patch
import pytest
import voluptuous as vol
@ -428,6 +428,10 @@ async def test_options_flow_devices(
demo_config_entry = MockConfigEntry(domain="domain")
demo_config_entry.add_to_hass(hass)
with patch("homeassistant.components.homekit.HomeKit") as mock_homekit:
mock_homekit.return_value = homekit = Mock()
type(homekit).async_start = AsyncMock()
assert await async_setup_component(hass, "homekit", {"homekit": {}})
assert await async_setup_component(hass, "homeassistant", {})
assert await async_setup_component(hass, "demo", {"demo": {}})
assert await async_setup_component(hass, "homekit", {"homekit": {}})
@ -464,7 +468,9 @@ async def test_options_flow_devices(
},
)
with patch("homeassistant.components.homekit.async_setup_entry", return_value=True):
with patch(
"homeassistant.components.homekit.async_setup_entry", return_value=True
):
result3 = await hass.config_entries.options.async_configure(
result2["flow_id"],
user_input={"devices": [device_id]},
@ -514,6 +520,9 @@ async def test_options_flow_devices_preserved_when_advanced_off(
demo_config_entry = MockConfigEntry(domain="domain")
demo_config_entry.add_to_hass(hass)
with patch("homeassistant.components.homekit.HomeKit") as mock_homekit:
mock_homekit.return_value = homekit = Mock()
type(homekit).async_start = AsyncMock()
assert await async_setup_component(hass, "homekit", {"homekit": {}})
hass.states.async_set("climate.old", "off")
@ -557,6 +566,7 @@ async def test_options_flow_devices_preserved_when_advanced_off(
}
await hass.async_block_till_done()
await hass.config_entries.async_unload(config_entry.entry_id)
await hass.async_block_till_done()
async def test_options_flow_include_mode_with_non_existant_entity(

View File

@ -347,7 +347,7 @@ async def test_homekit_with_single_advertise_ips(
)
entry.add_to_hass(hass)
with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver:
mock_driver.async_start = AsyncMock()
hk_driver.async_start = AsyncMock()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
@ -386,7 +386,7 @@ async def test_homekit_with_many_advertise_ips(
)
entry.add_to_hass(hass)
with patch(f"{PATH_HOMEKIT}.HomeDriver", return_value=hk_driver) as mock_driver:
mock_driver.async_start = AsyncMock()
hk_driver.async_start = AsyncMock()
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
@ -2043,6 +2043,7 @@ async def test_reload(hass: HomeAssistant, mock_async_zeroconf: None) -> None:
"homeassistant.components.network.async_get_source_ip", return_value="1.2.3.4"
):
mock_homekit.return_value = homekit = Mock()
type(homekit).async_start = AsyncMock()
assert await async_setup_component(
hass, "homekit", {"homekit": {CONF_NAME: "reloadable", CONF_PORT: 12345}}
)
@ -2065,16 +2066,15 @@ async def test_reload(hass: HomeAssistant, mock_async_zeroconf: None) -> None:
yaml_path = get_fixture_path("configuration.yaml", "homekit")
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path), patch(
f"{PATH_HOMEKIT}.HomeKit"
) as mock_homekit2, patch.object(homekit.bridge, "add_accessory"), patch(
f"{PATH_HOMEKIT}.async_show_setup_message"
), patch(
) as mock_homekit2, patch(f"{PATH_HOMEKIT}.async_show_setup_message"), patch(
f"{PATH_HOMEKIT}.get_accessory",
), patch(
), patch(f"{PATH_HOMEKIT}.async_port_is_available", return_value=True), patch(
"pyhap.accessory_driver.AccessoryDriver.async_start",
), patch(
"homeassistant.components.network.async_get_source_ip", return_value="1.2.3.4"
):
mock_homekit2.return_value = homekit = Mock()
type(homekit).async_start = AsyncMock()
await hass.services.async_call(
"homekit",
SERVICE_RELOAD,

View File

@ -1,6 +1,6 @@
"""Test HomeKit initialization."""
from unittest.mock import patch
from unittest.mock import AsyncMock, Mock, patch
import pytest
@ -31,7 +31,9 @@ async def test_humanify_homekit_changed_event(
) -> None:
"""Test humanifying HomeKit changed event."""
hass.config.components.add("recorder")
with patch("homeassistant.components.homekit.HomeKit"):
with patch("homeassistant.components.homekit.HomeKit") as mock_homekit:
mock_homekit.return_value = homekit = Mock()
type(homekit).async_start = AsyncMock()
assert await async_setup_component(hass, "homekit", {"homekit": {}})
assert await async_setup_component(hass, "logbook", {})
await hass.async_block_till_done()