Migrate homekit to use async_at_started (#113102)

This commit is contained in:
J. Nick Koston
2024-03-11 14:08:02 -10:00
committed by GitHub
parent 5dc44500c3
commit 6e59d1cb29
4 changed files with 112 additions and 103 deletions
+98 -88
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,62 +428,68 @@ async def test_options_flow_devices(
demo_config_entry = MockConfigEntry(domain="domain")
demo_config_entry.add_to_hass(hass)
assert await async_setup_component(hass, "homeassistant", {})
assert await async_setup_component(hass, "demo", {"demo": {}})
assert await async_setup_component(hass, "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, "homeassistant", {})
assert await async_setup_component(hass, "demo", {"demo": {}})
assert await async_setup_component(hass, "homekit", {"homekit": {}})
hass.states.async_set("climate.old", "off")
await hass.async_block_till_done()
hass.states.async_set("climate.old", "off")
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(
config_entry.entry_id, context={"show_advanced_options": True}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"domains": ["fan", "vacuum", "climate"],
"include_exclude_mode": "exclude",
},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "exclude"
entry = entity_registry.async_get("light.ceiling_lights")
assert entry is not None
device_id = entry.device_id
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"entities": ["climate.old"],
},
)
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]},
result = await hass.config_entries.options.async_init(
config_entry.entry_id, context={"show_advanced_options": True}
)
assert result3["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {
"devices": [device_id],
"mode": "bridge",
"filter": {
"exclude_domains": [],
"exclude_entities": ["climate.old"],
"include_domains": ["fan", "vacuum", "climate"],
"include_entities": [],
},
}
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
await hass.async_block_till_done()
await hass.config_entries.async_unload(config_entry.entry_id)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"domains": ["fan", "vacuum", "climate"],
"include_exclude_mode": "exclude",
},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "exclude"
entry = entity_registry.async_get("light.ceiling_lights")
assert entry is not None
device_id = entry.device_id
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"entities": ["climate.old"],
},
)
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]},
)
assert result3["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {
"devices": [device_id],
"mode": "bridge",
"filter": {
"exclude_domains": [],
"exclude_entities": ["climate.old"],
"include_domains": ["fan", "vacuum", "climate"],
"include_entities": [],
},
}
await hass.async_block_till_done()
await hass.config_entries.async_unload(config_entry.entry_id)
@patch(f"{PATH_HOMEKIT}.async_port_is_available", return_value=True)
@@ -514,49 +520,53 @@ async def test_options_flow_devices_preserved_when_advanced_off(
demo_config_entry = MockConfigEntry(domain="domain")
demo_config_entry.add_to_hass(hass)
assert await async_setup_component(hass, "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": {}})
hass.states.async_set("climate.old", "off")
await hass.async_block_till_done()
hass.states.async_set("climate.old", "off")
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(
config_entry.entry_id, context={"show_advanced_options": False}
)
result = await hass.config_entries.options.async_init(
config_entry.entry_id, context={"show_advanced_options": False}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"domains": ["fan", "vacuum", "climate"],
"include_exclude_mode": "exclude",
},
)
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"domains": ["fan", "vacuum", "climate"],
"include_exclude_mode": "exclude",
},
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "exclude"
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "exclude"
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"entities": ["climate.old"],
},
)
result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
"entities": ["climate.old"],
},
)
assert result2["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {
"devices": ["1fabcabcabcabcabcabcabcabcabc"],
"mode": "bridge",
"filter": {
"exclude_domains": [],
"exclude_entities": ["climate.old"],
"include_domains": ["fan", "vacuum", "climate"],
"include_entities": [],
},
}
await hass.async_block_till_done()
await hass.config_entries.async_unload(config_entry.entry_id)
assert result2["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert config_entry.options == {
"devices": ["1fabcabcabcabcabcabcabcabcabc"],
"mode": "bridge",
"filter": {
"exclude_domains": [],
"exclude_entities": ["climate.old"],
"include_domains": ["fan", "vacuum", "climate"],
"include_entities": [],
},
}
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(
+6 -6
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,
+4 -2
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()