Allow async_setup changes to config entry data be taken into a… (#34166)
* Allow async_setup changes to config entry data be taken into account * Fix tests * Limit scope try…finally * Update tests/test_config_entries.py Co-Authored-By: Martin Hjelmare <marhje52@gmail.com> * Fix import Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
1 parent
0db1fcca0f
commit
0b90ebf91e
10 files changed
+176
-33
No files matched your search
@@ -9,19 +9,6 @@ from .test_device import MAC, setup_axis_integration
|
||||
from tests.common import MockConfigEntry, mock_coro
|
||||
|
||||
|
||||
async def test_setup_device_already_configured(hass):
|
||||
"""Test already configured device does not configure a second."""
|
||||
with patch.object(hass, "config_entries") as mock_config_entries:
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
axis.DOMAIN,
|
||||
{axis.DOMAIN: {"device_name": {axis.CONF_HOST: "1.2.3.4"}}},
|
||||
)
|
||||
|
||||
assert not mock_config_entries.flow.mock_calls
|
||||
|
||||
|
||||
async def test_setup_no_config(hass):
|
||||
"""Test setup without configuration."""
|
||||
assert await async_setup_component(hass, axis.DOMAIN, {})
|
||||
|
||||
@@ -230,7 +230,7 @@ async def test_setup_with_no_config(hass):
|
||||
assert konnected.YAML_CONFIGS not in hass.data[konnected.DOMAIN]
|
||||
|
||||
|
||||
async def test_setup_defined_hosts_known_auth(hass):
|
||||
async def test_setup_defined_hosts_known_auth(hass, mock_panel):
|
||||
"""Test we don't initiate a config entry if configured panel is known."""
|
||||
MockConfigEntry(
|
||||
domain="konnected",
|
||||
|
||||
@@ -15,7 +15,9 @@ async def test_config_with_sensor_passed_to_config_entry(hass):
|
||||
CONF_SCAN_INTERVAL: 600,
|
||||
}
|
||||
|
||||
with patch.object(hass, "config_entries") as mock_config_entries, patch.object(
|
||||
with patch.object(
|
||||
hass.config_entries.flow, "async_init"
|
||||
) as mock_config_entries, patch.object(
|
||||
luftdaten, "configured_sensors", return_value=[]
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, conf) is True
|
||||
@@ -27,7 +29,9 @@ async def test_config_already_registered_not_passed_to_config_entry(hass):
|
||||
"""Test that an already registered sensor does not initiate an import."""
|
||||
conf = {CONF_SENSOR_ID: "12345abcde"}
|
||||
|
||||
with patch.object(hass, "config_entries") as mock_config_entries, patch.object(
|
||||
with patch.object(
|
||||
hass.config_entries.flow, "async_init"
|
||||
) as mock_config_entries, patch.object(
|
||||
luftdaten, "configured_sensors", return_value=["12345abcde"]
|
||||
):
|
||||
assert await async_setup_component(hass, DOMAIN, conf) is True
|
||||
|
||||
@@ -55,7 +55,9 @@ def get_homekit_info_mock(model):
|
||||
|
||||
async def test_setup(hass, mock_zeroconf):
|
||||
"""Test configured options for a device are loaded via config entry."""
|
||||
with patch.object(hass.config_entries, "flow") as mock_config_flow, patch.object(
|
||||
with patch.object(
|
||||
hass.config_entries.flow, "async_init"
|
||||
) as mock_config_flow, patch.object(
|
||||
zeroconf, "ServiceBrowser", side_effect=service_update_mock
|
||||
) as mock_service_browser:
|
||||
mock_zeroconf.get_service_info.side_effect = get_service_info_mock
|
||||
@@ -72,7 +74,9 @@ async def test_homekit_match_partial_space(hass, mock_zeroconf):
|
||||
"""Test configured options for a device are loaded via config entry."""
|
||||
with patch.dict(
|
||||
zc_gen.ZEROCONF, {zeroconf.HOMEKIT_TYPE: ["homekit_controller"]}, clear=True
|
||||
), patch.object(hass.config_entries, "flow") as mock_config_flow, patch.object(
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init"
|
||||
) as mock_config_flow, patch.object(
|
||||
zeroconf, "ServiceBrowser", side_effect=service_update_mock
|
||||
) as mock_service_browser:
|
||||
mock_zeroconf.get_service_info.side_effect = get_homekit_info_mock("LIFX bulb")
|
||||
@@ -87,7 +91,9 @@ async def test_homekit_match_partial_dash(hass, mock_zeroconf):
|
||||
"""Test configured options for a device are loaded via config entry."""
|
||||
with patch.dict(
|
||||
zc_gen.ZEROCONF, {zeroconf.HOMEKIT_TYPE: ["homekit_controller"]}, clear=True
|
||||
), patch.object(hass.config_entries, "flow") as mock_config_flow, patch.object(
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init"
|
||||
) as mock_config_flow, patch.object(
|
||||
zeroconf, "ServiceBrowser", side_effect=service_update_mock
|
||||
) as mock_service_browser:
|
||||
mock_zeroconf.get_service_info.side_effect = get_homekit_info_mock(
|
||||
@@ -104,7 +110,9 @@ async def test_homekit_match_full(hass, mock_zeroconf):
|
||||
"""Test configured options for a device are loaded via config entry."""
|
||||
with patch.dict(
|
||||
zc_gen.ZEROCONF, {zeroconf.HOMEKIT_TYPE: ["homekit_controller"]}, clear=True
|
||||
), patch.object(hass.config_entries, "flow") as mock_config_flow, patch.object(
|
||||
), patch.object(
|
||||
hass.config_entries.flow, "async_init"
|
||||
) as mock_config_flow, patch.object(
|
||||
zeroconf, "ServiceBrowser", side_effect=service_update_mock
|
||||
) as mock_service_browser:
|
||||
mock_zeroconf.get_service_info.side_effect = get_homekit_info_mock("BSB002")
|
||||
|
||||
@@ -3,6 +3,7 @@ import asyncio
|
||||
from datetime import timedelta
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from asynctest import CoroutineMock
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries, data_entry_flow, loader
|
||||
@@ -1463,3 +1464,97 @@ async def test_partial_flows_hidden(hass, manager):
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("persistent_notification.config_entry_discovery")
|
||||
assert state is not None
|
||||
|
||||
|
||||
async def test_async_setup_init_entry(hass):
|
||||
"""Test a config entry being initialized during integration setup."""
|
||||
|
||||
async def mock_async_setup(hass, config):
|
||||
"""Mock setup."""
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_IMPORT}, data={},
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
async_setup_entry = CoroutineMock(return_value=True)
|
||||
mock_integration(
|
||||
hass,
|
||||
MockModule(
|
||||
"comp", async_setup=mock_async_setup, async_setup_entry=async_setup_entry
|
||||
),
|
||||
)
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_import(self, user_input):
|
||||
"""Test import step creating entry."""
|
||||
return self.async_create_entry(title="title", data={})
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
assert await async_setup_component(hass, "comp", {})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(async_setup_entry.mock_calls) == 1
|
||||
|
||||
entries = hass.config_entries.async_entries("comp")
|
||||
assert len(entries) == 1
|
||||
assert entries[0].state == config_entries.ENTRY_STATE_LOADED
|
||||
|
||||
|
||||
async def test_async_setup_update_entry(hass):
|
||||
"""Test a config entry being updated during integration setup."""
|
||||
entry = MockConfigEntry(domain="comp", data={"value": "initial"})
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
async def mock_async_setup(hass, config):
|
||||
"""Mock setup."""
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
"comp", context={"source": config_entries.SOURCE_IMPORT}, data={},
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
async def mock_async_setup_entry(hass, entry):
|
||||
"""Mock setting up an entry."""
|
||||
assert entry.data["value"] == "updated"
|
||||
return True
|
||||
|
||||
mock_integration(
|
||||
hass,
|
||||
MockModule(
|
||||
"comp",
|
||||
async_setup=mock_async_setup,
|
||||
async_setup_entry=mock_async_setup_entry,
|
||||
),
|
||||
)
|
||||
mock_entity_platform(hass, "config_flow.comp", None)
|
||||
await async_setup_component(hass, "persistent_notification", {})
|
||||
|
||||
class TestFlow(config_entries.ConfigFlow):
|
||||
"""Test flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_import(self, user_input):
|
||||
"""Test import step updating existing entry."""
|
||||
self.hass.config_entries.async_update_entry(
|
||||
entry, data={"value": "updated"}
|
||||
)
|
||||
return self.async_abort(reason="yo")
|
||||
|
||||
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
||||
assert await async_setup_component(hass, "comp", {})
|
||||
|
||||
entries = hass.config_entries.async_entries("comp")
|
||||
assert len(entries) == 1
|
||||
assert entries[0].state == config_entries.ENTRY_STATE_LOADED
|
||||
assert entries[0].data == {"value": "updated"}
|
||||
Reference in new issue
Block a user