2018-02-16 22:07:38 +00:00
|
|
|
"""Test the config manager."""
|
|
|
|
import asyncio
|
2018-06-25 16:53:49 +00:00
|
|
|
from datetime import timedelta
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2019-06-20 20:22:12 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow, loader
|
2021-04-04 00:00:22 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STARTED
|
|
|
|
from homeassistant.core import CoreState, callback
|
2021-03-22 04:44:29 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
|
2021-03-09 13:24:34 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2018-02-16 22:07:38 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-06-25 16:53:49 +00:00
|
|
|
from homeassistant.util import dt
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-06-25 16:53:49 +00:00
|
|
|
from tests.common import (
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry,
|
|
|
|
MockEntity,
|
2019-12-09 15:52:24 +00:00
|
|
|
MockModule,
|
|
|
|
MockPlatform,
|
|
|
|
async_fire_time_changed,
|
|
|
|
mock_coro,
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_entity_platform,
|
2019-12-09 15:52:24 +00:00
|
|
|
mock_integration,
|
2019-08-23 00:32:43 +00:00
|
|
|
mock_registry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_handlers():
|
|
|
|
"""Mock config flows."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
class MockFlowHandler(config_entries.ConfigFlow):
|
|
|
|
"""Define a mock flow handler."""
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-04-11 08:26:36 +00:00
|
|
|
VERSION = 1
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.dict(
|
|
|
|
config_entries.HANDLERS, {"comp": MockFlowHandler, "test": MockFlowHandler}
|
|
|
|
):
|
2019-04-11 08:26:36 +00:00
|
|
|
yield
|
2019-02-15 17:30:47 +00:00
|
|
|
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def manager(hass):
|
|
|
|
"""Fixture of a loaded config manager."""
|
|
|
|
manager = config_entries.ConfigEntries(hass, {})
|
2021-03-22 04:44:29 +00:00
|
|
|
manager._entries = {}
|
2018-06-25 16:53:49 +00:00
|
|
|
manager._store._async_ensure_stop_listener = lambda: None
|
2018-02-16 22:07:38 +00:00
|
|
|
hass.config_entries = manager
|
|
|
|
return manager
|
|
|
|
|
|
|
|
|
2019-02-15 17:30:47 +00:00
|
|
|
async def test_call_setup_entry(hass):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test we call <component>.setup_entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp")
|
2019-02-15 17:30:47 +00:00
|
|
|
entry.add_to_hass(hass)
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
mock_migrate_entry = AsyncMock(return_value=True)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_migrate_entry=mock_migrate_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2020-08-25 22:59:22 +00:00
|
|
|
with patch("homeassistant.config_entries.support_entry_unload", return_value=True):
|
|
|
|
result = await async_setup_component(hass, "comp", {})
|
|
|
|
await hass.async_block_till_done()
|
2019-02-15 17:30:47 +00:00
|
|
|
assert result
|
|
|
|
assert len(mock_migrate_entry.mock_calls) == 0
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
2020-08-25 22:59:22 +00:00
|
|
|
assert entry.supports_unload
|
|
|
|
|
|
|
|
|
|
|
|
async def test_call_setup_entry_without_reload_support(hass):
|
|
|
|
"""Test we call <component>.setup_entry and the <component> does not support unloading."""
|
|
|
|
entry = MockConfigEntry(domain="comp")
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
assert not entry.supports_unload
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
mock_migrate_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_migrate_entry=mock_migrate_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
with patch("homeassistant.config_entries.support_entry_unload", return_value=False):
|
|
|
|
result = await async_setup_component(hass, "comp", {})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert result
|
|
|
|
assert len(mock_migrate_entry.mock_calls) == 0
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_async_migrate_entry(hass):
|
|
|
|
"""Test we call <component>.async_migrate_entry when version mismatch."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp")
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
entry.version = 2
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_migrate_entry = AsyncMock(return_value=True)
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_migrate_entry=mock_migrate_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2020-08-25 22:59:22 +00:00
|
|
|
with patch("homeassistant.config_entries.support_entry_unload", return_value=True):
|
|
|
|
result = await async_setup_component(hass, "comp", {})
|
|
|
|
await hass.async_block_till_done()
|
2018-02-16 22:07:38 +00:00
|
|
|
assert result
|
2019-02-15 17:30:47 +00:00
|
|
|
assert len(mock_migrate_entry.mock_calls) == 1
|
2018-02-16 22:07:38 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2019-02-15 17:30:47 +00:00
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
2020-08-25 22:59:22 +00:00
|
|
|
assert entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_async_migrate_entry_failure_false(hass):
|
|
|
|
"""Test migration fails if returns false."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp")
|
2019-02-15 17:30:47 +00:00
|
|
|
entry.version = 2
|
|
|
|
entry.add_to_hass(hass)
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_migrate_entry = AsyncMock(return_value=False)
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_migrate_entry=mock_migrate_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(hass, "comp", {})
|
2019-02-15 17:30:47 +00:00
|
|
|
assert result
|
|
|
|
assert len(mock_migrate_entry.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_async_migrate_entry_failure_exception(hass):
|
|
|
|
"""Test migration fails if exception raised."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp")
|
2019-02-15 17:30:47 +00:00
|
|
|
entry.version = 2
|
|
|
|
entry.add_to_hass(hass)
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_migrate_entry = AsyncMock(side_effect=Exception)
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_migrate_entry=mock_migrate_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(hass, "comp", {})
|
2019-02-15 17:30:47 +00:00
|
|
|
assert result
|
|
|
|
assert len(mock_migrate_entry.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_async_migrate_entry_failure_not_bool(hass):
|
|
|
|
"""Test migration fails if boolean not returned."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp")
|
2019-02-15 17:30:47 +00:00
|
|
|
entry.version = 2
|
|
|
|
entry.add_to_hass(hass)
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_migrate_entry = AsyncMock(return_value=None)
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
2019-07-31 19:25:30 +00:00
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_migrate_entry=mock_migrate_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(hass, "comp", {})
|
2019-02-15 17:30:47 +00:00
|
|
|
assert result
|
|
|
|
assert len(mock_migrate_entry.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_call_async_migrate_entry_failure_not_supported(hass):
|
|
|
|
"""Test migration fails if async_migrate_entry not implemented."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp")
|
2019-02-15 17:30:47 +00:00
|
|
|
entry.version = 2
|
|
|
|
entry.add_to_hass(hass)
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-02-15 17:30:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await async_setup_component(hass, "comp", {})
|
2019-02-15 17:30:47 +00:00
|
|
|
assert result
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_MIGRATION_ERROR
|
2020-08-25 22:59:22 +00:00
|
|
|
assert not entry.supports_unload
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
async def test_remove_entry(hass, manager):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test that we can remove an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
async def mock_setup_entry(hass, entry):
|
|
|
|
"""Mock setting up entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, "light")
|
|
|
|
)
|
2018-10-25 17:57:36 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
async def mock_unload_entry(hass, entry):
|
|
|
|
"""Mock unloading an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await hass.config_entries.async_forward_entry_unload(entry, "light")
|
2018-10-25 17:57:36 +00:00
|
|
|
assert result
|
|
|
|
return result
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_remove_entry = AsyncMock(return_value=None)
|
2019-03-02 05:13:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entity = MockEntity(unique_id="1234", name="Test Entity")
|
2018-10-25 17:57:36 +00:00
|
|
|
|
|
|
|
async def mock_setup_entry_platform(hass, entry, async_add_entities):
|
|
|
|
"""Mock setting up platform."""
|
|
|
|
async_add_entities([entity])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"test",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_unload_entry=mock_unload_entry,
|
|
|
|
async_remove_entry=mock_remove_entry,
|
|
|
|
),
|
|
|
|
)
|
2019-04-11 08:26:36 +00:00
|
|
|
mock_entity_platform(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, "light.test", MockPlatform(async_setup_entry=mock_setup_entry_platform)
|
2018-10-25 17:57:36 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
|
|
|
|
|
|
|
MockConfigEntry(domain="test_other", entry_id="test1").add_to_manager(manager)
|
|
|
|
entry = MockConfigEntry(domain="test", entry_id="test2")
|
2018-10-25 17:57:36 +00:00
|
|
|
entry.add_to_manager(manager)
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry(domain="test_other", entry_id="test3").add_to_manager(manager)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
# Check all config entries exist
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == [
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
# Setup entry
|
|
|
|
await entry.async_setup(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Check entity state got added
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("light.test_entity") is not None
|
2020-01-07 16:30:53 +00:00
|
|
|
assert len(hass.states.async_all()) == 1
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
# Check entity got added to entity registry
|
2021-03-09 13:24:34 +00:00
|
|
|
ent_reg = er.async_get(hass)
|
2018-10-25 17:57:36 +00:00
|
|
|
assert len(ent_reg.entities) == 1
|
|
|
|
entity_entry = list(ent_reg.entities.values())[0]
|
|
|
|
assert entity_entry.config_entry_id == entry.entry_id
|
|
|
|
|
|
|
|
# Remove entry
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_remove("test2")
|
2018-10-25 17:57:36 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Check that unload went well and so no need to restart
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result == {"require_restart": False}
|
2018-10-25 17:57:36 +00:00
|
|
|
|
2019-03-02 05:13:55 +00:00
|
|
|
# Check the remove callback was invoked.
|
|
|
|
assert mock_remove_entry.call_count == 1
|
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
# Check that config entry was removed.
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == ["test1", "test3"]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-10-25 17:57:36 +00:00
|
|
|
# Check that entity state has been removed
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.states.get("light.test_entity") is None
|
2020-01-07 16:30:53 +00:00
|
|
|
assert len(hass.states.async_all()) == 0
|
2018-10-25 17:57:36 +00:00
|
|
|
|
2019-05-19 09:41:39 +00:00
|
|
|
# Check that entity registry entry has been removed
|
|
|
|
entity_entry_list = list(ent_reg.entities.values())
|
|
|
|
assert not entity_entry_list
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2019-03-02 05:13:55 +00:00
|
|
|
async def test_remove_entry_handles_callback_error(hass, manager):
|
|
|
|
"""Test that exceptions in the remove callback are handled."""
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
mock_unload_entry = AsyncMock(return_value=True)
|
|
|
|
mock_remove_entry = AsyncMock(return_value=None)
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"test",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_unload_entry=mock_unload_entry,
|
|
|
|
async_remove_entry=mock_remove_entry,
|
|
|
|
),
|
2019-03-02 05:13:55 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="test", entry_id="test1")
|
2019-03-02 05:13:55 +00:00
|
|
|
entry.add_to_manager(manager)
|
|
|
|
# Check all config entries exist
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == ["test1"]
|
2019-03-02 05:13:55 +00:00
|
|
|
# Setup entry
|
|
|
|
await entry.async_setup(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Remove entry
|
2019-07-31 19:25:30 +00:00
|
|
|
result = await manager.async_remove("test1")
|
2019-03-02 05:13:55 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
# Check that unload went well and so no need to restart
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result == {"require_restart": False}
|
2019-03-02 05:13:55 +00:00
|
|
|
# Check the remove callback was invoked.
|
|
|
|
assert mock_remove_entry.call_count == 1
|
|
|
|
# Check that config entry was removed.
|
|
|
|
assert [item.entry_id for item in manager.async_entries()] == []
|
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_remove_entry_raises(hass, manager):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test if a component raises while removing entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def mock_unload_entry(hass, entry):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Mock unload entry function."""
|
|
|
|
raise Exception("BROKEN")
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_unload_entry=mock_unload_entry))
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry(domain="test", entry_id="test1").add_to_manager(manager)
|
2018-09-25 12:29:13 +00:00
|
|
|
MockConfigEntry(
|
2019-07-31 19:25:30 +00:00
|
|
|
domain="comp", entry_id="test2", state=config_entries.ENTRY_STATE_LOADED
|
2018-09-25 12:29:13 +00:00
|
|
|
).add_to_manager(manager)
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry(domain="test", entry_id="test3").add_to_manager(manager)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == [
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
result = await manager.async_remove("test2")
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result == {"require_restart": True}
|
|
|
|
assert [item.entry_id for item in manager.async_entries()] == ["test1", "test3"]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_remove_entry_if_not_loaded(hass, manager):
|
2019-04-15 02:07:05 +00:00
|
|
|
"""Test that we can remove an entry that is not loaded."""
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_unload_entry = AsyncMock(return_value=True)
|
2018-09-25 12:29:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_unload_entry=mock_unload_entry))
|
2018-09-25 12:29:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry(domain="test", entry_id="test1").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="comp", entry_id="test2").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test", entry_id="test3").add_to_manager(manager)
|
2018-09-25 12:29:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == [
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
]
|
2018-09-25 12:29:13 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
result = await manager.async_remove("test2")
|
2018-09-25 12:29:13 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result == {"require_restart": False}
|
2020-05-25 19:40:06 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == ["test1", "test3"]
|
|
|
|
|
|
|
|
assert len(mock_unload_entry.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_remove_entry_if_integration_deleted(hass, manager):
|
|
|
|
"""Test that we can remove an entry when the integration is deleted."""
|
|
|
|
mock_unload_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
MockConfigEntry(domain="test", entry_id="test1").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="comp", entry_id="test2").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test", entry_id="test3").add_to_manager(manager)
|
|
|
|
|
|
|
|
assert [item.entry_id for item in manager.async_entries()] == [
|
|
|
|
"test1",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
]
|
|
|
|
|
|
|
|
result = await manager.async_remove("test2")
|
|
|
|
|
|
|
|
assert result == {"require_restart": False}
|
2019-07-31 19:25:30 +00:00
|
|
|
assert [item.entry_id for item in manager.async_entries()] == ["test1", "test3"]
|
2018-09-25 12:29:13 +00:00
|
|
|
|
2019-04-15 02:07:05 +00:00
|
|
|
assert len(mock_unload_entry.mock_calls) == 0
|
2018-09-25 12:29:13 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_add_entry_calls_setup_entry(hass, manager):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test we call setup_config_entry."""
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title="title", data={"token": "supersecret"})
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
|
2020-04-07 16:33:23 +00:00
|
|
|
await manager.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2020-04-07 16:33:23 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
p_hass, p_entry = mock_setup_entry.mock_calls[0][1]
|
|
|
|
|
|
|
|
assert p_hass is hass
|
2019-07-31 19:25:30 +00:00
|
|
|
assert p_entry.data == {"token": "supersecret"}
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def test_entries_gets_entries(manager):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test entries are filtered by domain."""
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry(domain="test").add_to_manager(manager)
|
|
|
|
entry1 = MockConfigEntry(domain="test2")
|
2018-02-16 22:07:38 +00:00
|
|
|
entry1.add_to_manager(manager)
|
2019-07-31 19:25:30 +00:00
|
|
|
entry2 = MockConfigEntry(domain="test2")
|
2018-02-16 22:07:38 +00:00
|
|
|
entry2.add_to_manager(manager)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert manager.async_entries("test2") == [entry1, entry2]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2021-03-29 11:06:44 +00:00
|
|
|
async def test_domains_gets_domains_uniques(manager):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test we only return each domain once."""
|
2019-07-31 19:25:30 +00:00
|
|
|
MockConfigEntry(domain="test").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test2").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test2").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test3").add_to_manager(manager)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert manager.async_domains() == ["test", "test2", "test3"]
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2021-03-29 11:06:44 +00:00
|
|
|
async def test_domains_gets_domains_excludes_ignore_and_disabled(manager):
|
|
|
|
"""Test we only return each domain once."""
|
|
|
|
MockConfigEntry(domain="test").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test2").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test2").add_to_manager(manager)
|
|
|
|
MockConfigEntry(
|
|
|
|
domain="ignored", source=config_entries.SOURCE_IGNORE
|
|
|
|
).add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="test3").add_to_manager(manager)
|
|
|
|
MockConfigEntry(domain="disabled", disabled_by="user").add_to_manager(manager)
|
|
|
|
assert manager.async_domains() == ["test", "test2", "test3"]
|
|
|
|
assert manager.async_domains(include_ignore=False) == ["test", "test2", "test3"]
|
|
|
|
assert manager.async_domains(include_disabled=False) == ["test", "test2", "test3"]
|
|
|
|
assert manager.async_domains(include_ignore=False, include_disabled=False) == [
|
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
]
|
|
|
|
|
|
|
|
assert manager.async_domains(include_ignore=True) == [
|
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
"ignored",
|
|
|
|
"test3",
|
|
|
|
]
|
|
|
|
assert manager.async_domains(include_disabled=True) == [
|
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
"test3",
|
|
|
|
"disabled",
|
|
|
|
]
|
|
|
|
assert manager.async_domains(include_ignore=True, include_disabled=True) == [
|
|
|
|
"test",
|
|
|
|
"test2",
|
|
|
|
"ignored",
|
|
|
|
"test3",
|
|
|
|
"disabled",
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2018-06-29 02:14:26 +00:00
|
|
|
async def test_saving_and_loading(hass):
|
2018-02-16 22:07:38 +00:00
|
|
|
"""Test that we're saving and loading correctly."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass, MockModule("test", async_setup_entry=lambda *args: mock_coro(True))
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
2018-05-01 18:57:30 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
VERSION = 5
|
2018-09-17 08:12:46 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-12-16 18:45:09 +00:00
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2019-12-16 18:45:09 +00:00
|
|
|
await self.async_set_unique_id("unique")
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title="Test Title", data={"token": "abcd"})
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.dict(config_entries.HANDLERS, {"test": TestFlow}):
|
2018-08-13 09:27:18 +00:00
|
|
|
await hass.config_entries.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
"test", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
class Test2Flow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
VERSION = 3
|
2018-09-17 08:12:46 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_PUSH
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2020-04-07 16:33:23 +00:00
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
2018-02-16 22:07:38 +00:00
|
|
|
return self.async_create_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
title="Test 2 Title", data={"username": "bla"}
|
2018-02-16 22:07:38 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.config_entries.HANDLERS.get", return_value=Test2Flow):
|
2018-08-13 09:27:18 +00:00
|
|
|
await hass.config_entries.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
"test", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2020-03-09 21:07:50 +00:00
|
|
|
assert len(hass.config_entries.async_entries()) == 2
|
|
|
|
|
2018-06-29 02:14:26 +00:00
|
|
|
# To trigger the call_later
|
|
|
|
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=1))
|
|
|
|
# To execute the save
|
|
|
|
await hass.async_block_till_done()
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
# Now load written data in new config manager
|
|
|
|
manager = config_entries.ConfigEntries(hass, {})
|
2019-03-01 04:27:20 +00:00
|
|
|
await manager.async_initialize()
|
2018-02-16 22:07:38 +00:00
|
|
|
|
2020-03-09 21:07:50 +00:00
|
|
|
assert len(manager.async_entries()) == 2
|
|
|
|
|
2018-02-16 22:07:38 +00:00
|
|
|
# Ensure same order
|
2019-07-31 19:25:30 +00:00
|
|
|
for orig, loaded in zip(
|
|
|
|
hass.config_entries.async_entries(), manager.async_entries()
|
|
|
|
):
|
2018-02-16 22:07:38 +00:00
|
|
|
assert orig.version == loaded.version
|
|
|
|
assert orig.domain == loaded.domain
|
|
|
|
assert orig.title == loaded.title
|
|
|
|
assert orig.data == loaded.data
|
|
|
|
assert orig.source == loaded.source
|
2018-09-17 08:12:46 +00:00
|
|
|
assert orig.connection_class == loaded.connection_class
|
2019-12-16 18:45:09 +00:00
|
|
|
assert orig.unique_id == loaded.unique_id
|
2018-02-16 22:07:38 +00:00
|
|
|
|
|
|
|
|
2018-04-09 14:09:08 +00:00
|
|
|
async def test_forward_entry_sets_up_component(hass):
|
|
|
|
"""Test we setup the component entry is forwarded to."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="original")
|
2018-04-09 14:09:08 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_original_setup_entry = AsyncMock(return_value=True)
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, MockModule("original", async_setup_entry=mock_original_setup_entry)
|
|
|
|
)
|
2018-04-09 14:09:08 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_forwarded_setup_entry = AsyncMock(return_value=True)
|
2019-04-15 02:07:05 +00:00
|
|
|
mock_integration(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, MockModule("forwarded", async_setup_entry=mock_forwarded_setup_entry)
|
|
|
|
)
|
2018-04-09 14:09:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setup(entry, "forwarded")
|
2018-04-09 14:09:08 +00:00
|
|
|
assert len(mock_original_setup_entry.mock_calls) == 0
|
|
|
|
assert len(mock_forwarded_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_forward_entry_does_not_setup_entry_if_setup_fails(hass):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Test we do not set up entry if component setup fails."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="original")
|
2018-04-09 14:09:08 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup = AsyncMock(return_value=False)
|
|
|
|
mock_setup_entry = AsyncMock()
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"forwarded", async_setup=mock_setup, async_setup_entry=mock_setup_entry
|
|
|
|
),
|
|
|
|
)
|
2018-04-09 14:09:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setup(entry, "forwarded")
|
2018-04-09 14:09:08 +00:00
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
2018-04-22 19:00:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_notification(hass):
|
|
|
|
"""Test that we create/dismiss a notification when source is discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("test"))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
|
|
|
await async_setup_component(hass, "persistent_notification", {})
|
2018-04-22 19:00:24 +00:00
|
|
|
|
2019-08-20 17:46:51 +00:00
|
|
|
with patch.dict(config_entries.HANDLERS):
|
2018-04-22 19:00:24 +00:00
|
|
|
|
2019-08-20 17:46:51 +00:00
|
|
|
class TestFlow(config_entries.ConfigFlow, domain="test"):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
|
|
|
|
2019-08-20 17:46:51 +00:00
|
|
|
VERSION = 5
|
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_discovery(self, discovery_info):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test discovery step."""
|
2020-06-15 11:38:38 +00:00
|
|
|
return self.async_show_form(step_id="discovery_confirm")
|
|
|
|
|
|
|
|
async def async_step_discovery_confirm(self, discovery_info):
|
|
|
|
"""Test discovery confirm step."""
|
|
|
|
return self.async_create_entry(
|
|
|
|
title="Test Title", data={"token": "abcd"}
|
|
|
|
)
|
2018-04-22 19:00:24 +00:00
|
|
|
|
2020-10-15 20:46:27 +00:00
|
|
|
# Start first discovery flow to assert that reconfigure notification fires
|
|
|
|
flow1 = await hass.config_entries.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
"test", context={"source": config_entries.SOURCE_DISCOVERY}
|
|
|
|
)
|
2018-04-22 19:00:24 +00:00
|
|
|
|
2019-08-20 17:46:51 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_discovery")
|
|
|
|
assert state is not None
|
2018-04-22 19:00:24 +00:00
|
|
|
|
2020-10-15 20:46:27 +00:00
|
|
|
# Start a second discovery flow so we can finish the first and assert that
|
|
|
|
# the discovery notification persists until the second one is complete
|
|
|
|
flow2 = await hass.config_entries.flow.async_init(
|
|
|
|
"test", context={"source": config_entries.SOURCE_DISCOVERY}
|
|
|
|
)
|
|
|
|
|
|
|
|
flow1 = await hass.config_entries.flow.async_configure(flow1["flow_id"], {})
|
|
|
|
assert flow1["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_discovery")
|
|
|
|
assert state is not None
|
|
|
|
|
|
|
|
flow2 = await hass.config_entries.flow.async_configure(flow2["flow_id"], {})
|
|
|
|
assert flow2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2018-04-22 19:00:24 +00:00
|
|
|
|
2019-08-20 17:46:51 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_discovery")
|
|
|
|
assert state is None
|
2018-05-24 18:24:14 +00:00
|
|
|
|
|
|
|
|
2020-10-15 20:46:27 +00:00
|
|
|
async def test_reauth_notification(hass):
|
|
|
|
"""Test that we create/dismiss a notification when source is reauth."""
|
|
|
|
mock_integration(hass, MockModule("test"))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
|
|
|
await async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS):
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow, domain="test"):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 5
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input):
|
|
|
|
"""Test user step."""
|
|
|
|
return self.async_show_form(step_id="user_confirm")
|
|
|
|
|
|
|
|
async def async_step_user_confirm(self, user_input):
|
|
|
|
"""Test user confirm step."""
|
|
|
|
return self.async_show_form(step_id="user_confirm")
|
|
|
|
|
|
|
|
async def async_step_reauth(self, user_input):
|
|
|
|
"""Test reauth step."""
|
|
|
|
return self.async_show_form(step_id="reauth_confirm")
|
|
|
|
|
|
|
|
async def async_step_reauth_confirm(self, user_input):
|
|
|
|
"""Test reauth confirm step."""
|
|
|
|
return self.async_abort(reason="test")
|
|
|
|
|
|
|
|
# Start user flow to assert that reconfigure notification doesn't fire
|
|
|
|
await hass.config_entries.flow.async_init(
|
|
|
|
"test", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_reconfigure")
|
|
|
|
assert state is None
|
|
|
|
|
|
|
|
# Start first reauth flow to assert that reconfigure notification fires
|
|
|
|
flow1 = await hass.config_entries.flow.async_init(
|
|
|
|
"test", context={"source": config_entries.SOURCE_REAUTH}
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_reconfigure")
|
|
|
|
assert state is not None
|
|
|
|
|
|
|
|
# Start a second reauth flow so we can finish the first and assert that
|
|
|
|
# the reconfigure notification persists until the second one is complete
|
|
|
|
flow2 = await hass.config_entries.flow.async_init(
|
|
|
|
"test", context={"source": config_entries.SOURCE_REAUTH}
|
|
|
|
)
|
|
|
|
|
|
|
|
flow1 = await hass.config_entries.flow.async_configure(flow1["flow_id"], {})
|
|
|
|
assert flow1["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_reconfigure")
|
|
|
|
assert state is not None
|
|
|
|
|
|
|
|
flow2 = await hass.config_entries.flow.async_configure(flow2["flow_id"], {})
|
|
|
|
assert flow2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_reconfigure")
|
|
|
|
assert state is None
|
|
|
|
|
|
|
|
|
2018-05-24 18:24:14 +00:00
|
|
|
async def test_discovery_notification_not_created(hass):
|
|
|
|
"""Test that we not create a notification when discovery is aborted."""
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("test"))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
|
|
|
await async_setup_component(hass, "persistent_notification", {})
|
2018-05-24 18:24:14 +00:00
|
|
|
|
2018-09-17 08:12:46 +00:00
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
|
|
|
|
2018-05-24 18:24:14 +00:00
|
|
|
VERSION = 5
|
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_discovery(self, discovery_info):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test discovery step."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="test")
|
2018-05-24 18:24:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.dict(config_entries.HANDLERS, {"test": TestFlow}):
|
2018-05-24 18:24:14 +00:00
|
|
|
await hass.config_entries.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
"test", context={"source": config_entries.SOURCE_DISCOVERY}
|
|
|
|
)
|
2018-05-24 18:24:14 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state = hass.states.get("persistent_notification.config_entry_discovery")
|
2018-05-24 18:24:14 +00:00
|
|
|
assert state is None
|
2018-06-25 21:21:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_loading_default_config(hass):
|
|
|
|
"""Test loading the default config."""
|
|
|
|
manager = config_entries.ConfigEntries(hass, {})
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.util.json.open", side_effect=FileNotFoundError):
|
2019-03-01 04:27:20 +00:00
|
|
|
await manager.async_initialize()
|
2018-06-25 21:21:38 +00:00
|
|
|
|
|
|
|
assert len(manager.async_entries()) == 0
|
2018-09-25 10:21:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_updating_entry_data(manager):
|
|
|
|
"""Test that we can update an entry data."""
|
|
|
|
entry = MockConfigEntry(
|
2019-07-31 19:25:30 +00:00
|
|
|
domain="test",
|
|
|
|
data={"first": True},
|
2019-02-14 04:36:06 +00:00
|
|
|
state=config_entries.ENTRY_STATE_SETUP_ERROR,
|
2018-09-25 10:21:11 +00:00
|
|
|
)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
2020-08-08 18:23:56 +00:00
|
|
|
assert manager.async_update_entry(entry) is False
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry.data == {"first": True}
|
2019-02-14 04:36:06 +00:00
|
|
|
|
2020-08-08 18:23:56 +00:00
|
|
|
assert manager.async_update_entry(entry, data={"second": True}) is True
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry.data == {"second": True}
|
2018-10-04 13:53:50 +00:00
|
|
|
|
|
|
|
|
2019-08-18 04:34:11 +00:00
|
|
|
async def test_updating_entry_system_options(manager):
|
|
|
|
"""Test that we can update an entry data."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="test",
|
|
|
|
data={"first": True},
|
|
|
|
state=config_entries.ENTRY_STATE_SETUP_ERROR,
|
|
|
|
system_options={"disable_new_entities": True},
|
|
|
|
)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
assert entry.system_options.disable_new_entities
|
|
|
|
|
|
|
|
entry.system_options.update(disable_new_entities=False)
|
|
|
|
assert not entry.system_options.disable_new_entities
|
|
|
|
|
|
|
|
|
2019-02-22 16:59:43 +00:00
|
|
|
async def test_update_entry_options_and_trigger_listener(hass, manager):
|
|
|
|
"""Test that we can update entry options and trigger listener."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="test", options={"first": True})
|
2019-02-22 16:59:43 +00:00
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
async def update_listener(hass, entry):
|
|
|
|
"""Test function."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry.options == {"second": True}
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
entry.add_update_listener(update_listener)
|
|
|
|
|
2020-08-08 18:23:56 +00:00
|
|
|
assert manager.async_update_entry(entry, options={"second": True}) is True
|
2019-02-22 16:59:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry.options == {"second": True}
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
|
2018-10-04 13:53:50 +00:00
|
|
|
async def test_setup_raise_not_ready(hass, caplog):
|
|
|
|
"""Test a setup raising not ready."""
|
2021-01-22 17:13:23 +00:00
|
|
|
entry = MockConfigEntry(title="test_title", domain="test")
|
2018-10-04 13:53:50 +00:00
|
|
|
|
2021-03-29 10:25:40 +00:00
|
|
|
mock_setup_entry = AsyncMock(
|
|
|
|
side_effect=ConfigEntryNotReady("The internet connection is offline")
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
2018-10-04 13:53:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.event.async_call_later") as mock_call:
|
2018-10-04 13:53:50 +00:00
|
|
|
await entry.async_setup(hass)
|
|
|
|
|
|
|
|
assert len(mock_call.mock_calls) == 1
|
2021-03-29 10:25:40 +00:00
|
|
|
assert (
|
|
|
|
"Config entry 'test_title' for test integration not ready yet: The internet connection is offline"
|
|
|
|
in caplog.text
|
|
|
|
)
|
2018-10-04 13:53:50 +00:00
|
|
|
p_hass, p_wait_time, p_setup = mock_call.mock_calls[0][1]
|
|
|
|
|
|
|
|
assert p_hass is hass
|
|
|
|
assert p_wait_time == 5
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_SETUP_RETRY
|
|
|
|
|
|
|
|
mock_setup_entry.side_effect = None
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry.return_value = True
|
2018-10-04 13:53:50 +00:00
|
|
|
|
|
|
|
await p_setup(None)
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
|
2021-03-29 10:25:40 +00:00
|
|
|
async def test_setup_raise_not_ready_from_exception(hass, caplog):
|
|
|
|
"""Test a setup raising not ready from another exception."""
|
|
|
|
entry = MockConfigEntry(title="test_title", domain="test")
|
|
|
|
|
|
|
|
original_exception = HomeAssistantError("The device dropped the connection")
|
|
|
|
config_entry_exception = ConfigEntryNotReady()
|
|
|
|
config_entry_exception.__cause__ = original_exception
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(side_effect=config_entry_exception)
|
|
|
|
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
|
|
|
|
|
|
|
with patch("homeassistant.helpers.event.async_call_later") as mock_call:
|
|
|
|
await entry.async_setup(hass)
|
|
|
|
|
|
|
|
assert len(mock_call.mock_calls) == 1
|
|
|
|
assert (
|
|
|
|
"Config entry 'test_title' for test integration not ready yet: The device dropped the connection"
|
|
|
|
in caplog.text
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-04 13:53:50 +00:00
|
|
|
async def test_setup_retrying_during_unload(hass):
|
|
|
|
"""Test if we unload an entry that is in retry mode."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="test")
|
2018-10-04 13:53:50 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(side_effect=ConfigEntryNotReady)
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
2018-10-04 13:53:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.event.async_call_later") as mock_call:
|
2018-10-04 13:53:50 +00:00
|
|
|
await entry.async_setup(hass)
|
|
|
|
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_SETUP_RETRY
|
|
|
|
assert len(mock_call.return_value.mock_calls) == 0
|
|
|
|
|
|
|
|
await entry.async_unload(hass)
|
|
|
|
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
|
|
|
assert len(mock_call.return_value.mock_calls) == 1
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
|
2021-04-04 00:00:22 +00:00
|
|
|
async def test_setup_retrying_during_unload_before_started(hass):
|
|
|
|
"""Test if we unload an entry that is in retry mode before started."""
|
|
|
|
entry = MockConfigEntry(domain="test")
|
|
|
|
hass.state = CoreState.starting
|
|
|
|
initial_listeners = hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED]
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(side_effect=ConfigEntryNotReady)
|
|
|
|
mock_integration(hass, MockModule("test", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.test", None)
|
|
|
|
|
|
|
|
await entry.async_setup(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_SETUP_RETRY
|
|
|
|
assert (
|
|
|
|
hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED] == initial_listeners + 1
|
|
|
|
)
|
|
|
|
|
|
|
|
await entry.async_unload(hass)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
|
|
|
assert (
|
|
|
|
hass.bus.async_listeners()[EVENT_HOMEASSISTANT_STARTED] == initial_listeners + 0
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-02-22 16:59:43 +00:00
|
|
|
async def test_entry_options(hass, manager):
|
|
|
|
"""Test that we can set options on an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="test", data={"first": True}, options=None)
|
2019-02-22 16:59:43 +00:00
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
class TestFlow:
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
|
|
|
|
2019-02-22 16:59:43 +00:00
|
|
|
@staticmethod
|
|
|
|
@callback
|
2019-08-15 21:11:55 +00:00
|
|
|
def async_get_options_flow(config_entry):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test options flow."""
|
|
|
|
|
2019-02-22 16:59:43 +00:00
|
|
|
class OptionsFlowHandler(data_entry_flow.FlowHandler):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test options flow handler."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-08-15 21:11:55 +00:00
|
|
|
return OptionsFlowHandler()
|
2019-02-22 16:59:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config_entries.HANDLERS["test"] = TestFlow()
|
2020-01-03 10:52:01 +00:00
|
|
|
flow = await manager.options.async_create_flow(
|
2019-07-31 19:25:30 +00:00
|
|
|
entry.entry_id, context={"source": "test"}, data=None
|
|
|
|
)
|
2019-02-22 16:59:43 +00:00
|
|
|
|
|
|
|
flow.handler = entry.entry_id # Used to keep reference to config entry
|
|
|
|
|
2020-11-09 07:59:42 +00:00
|
|
|
await manager.options.async_finish_flow(
|
|
|
|
flow,
|
|
|
|
{"data": {"second": True}, "type": data_entry_flow.RESULT_TYPE_CREATE_ENTRY},
|
|
|
|
)
|
2019-02-22 16:59:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entry.data == {"first": True}
|
|
|
|
assert entry.options == {"second": True}
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
|
2020-11-09 07:59:42 +00:00
|
|
|
async def test_entry_options_abort(hass, manager):
|
|
|
|
"""Test that we can abort options flow."""
|
|
|
|
entry = MockConfigEntry(domain="test", data={"first": True}, options=None)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
class TestFlow:
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(config_entry):
|
|
|
|
"""Test options flow."""
|
|
|
|
|
|
|
|
class OptionsFlowHandler(data_entry_flow.FlowHandler):
|
|
|
|
"""Test options flow handler."""
|
|
|
|
|
|
|
|
return OptionsFlowHandler()
|
|
|
|
|
|
|
|
config_entries.HANDLERS["test"] = TestFlow()
|
|
|
|
flow = await manager.options.async_create_flow(
|
|
|
|
entry.entry_id, context={"source": "test"}, data=None
|
|
|
|
)
|
|
|
|
|
|
|
|
flow.handler = entry.entry_id # Used to keep reference to config entry
|
|
|
|
|
|
|
|
assert await manager.options.async_finish_flow(
|
|
|
|
flow, {"type": data_entry_flow.RESULT_TYPE_ABORT, "reason": "test"}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-03-01 04:27:20 +00:00
|
|
|
async def test_entry_setup_succeed(hass, manager):
|
|
|
|
"""Test that we can setup an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=config_entries.ENTRY_STATE_NOT_LOADED)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup = AsyncMock(return_value=True)
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule("comp", async_setup=mock_setup, async_setup_entry=mock_setup_entry),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
assert await manager.async_setup(entry.entry_id)
|
|
|
|
assert len(mock_setup.mock_calls) == 1
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"state",
|
|
|
|
(
|
|
|
|
config_entries.ENTRY_STATE_LOADED,
|
|
|
|
config_entries.ENTRY_STATE_SETUP_ERROR,
|
|
|
|
config_entries.ENTRY_STATE_MIGRATION_ERROR,
|
|
|
|
config_entries.ENTRY_STATE_SETUP_RETRY,
|
|
|
|
config_entries.ENTRY_STATE_FAILED_UNLOAD,
|
|
|
|
),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
async def test_entry_setup_invalid_state(hass, manager, state):
|
|
|
|
"""Test that we cannot setup an entry with invalid state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=state)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup = AsyncMock(return_value=True)
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule("comp", async_setup=mock_setup, async_setup_entry=mock_setup_entry),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
with pytest.raises(config_entries.OperationNotAllowed):
|
|
|
|
assert await manager.async_setup(entry.entry_id)
|
|
|
|
|
|
|
|
assert len(mock_setup.mock_calls) == 0
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == state
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entry_unload_succeed(hass, manager):
|
|
|
|
"""Test that we can unload an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=config_entries.ENTRY_STATE_LOADED)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_unload_entry=async_unload_entry))
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
assert await manager.async_unload(entry.entry_id)
|
|
|
|
assert len(async_unload_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"state",
|
|
|
|
(
|
|
|
|
config_entries.ENTRY_STATE_NOT_LOADED,
|
|
|
|
config_entries.ENTRY_STATE_SETUP_ERROR,
|
|
|
|
config_entries.ENTRY_STATE_SETUP_RETRY,
|
|
|
|
),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
async def test_entry_unload_failed_to_load(hass, manager, state):
|
|
|
|
"""Test that we can unload an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=state)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_unload_entry=async_unload_entry))
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
assert await manager.async_unload(entry.entry_id)
|
|
|
|
assert len(async_unload_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"state",
|
|
|
|
(
|
|
|
|
config_entries.ENTRY_STATE_MIGRATION_ERROR,
|
|
|
|
config_entries.ENTRY_STATE_FAILED_UNLOAD,
|
|
|
|
),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
async def test_entry_unload_invalid_state(hass, manager, state):
|
|
|
|
"""Test that we cannot unload an entry with invalid state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=state)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_unload_entry=async_unload_entry))
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
with pytest.raises(config_entries.OperationNotAllowed):
|
|
|
|
assert await manager.async_unload(entry.entry_id)
|
|
|
|
|
|
|
|
assert len(async_unload_entry.mock_calls) == 0
|
|
|
|
assert entry.state == state
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entry_reload_succeed(hass, manager):
|
|
|
|
"""Test that we can reload an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=config_entries.ENTRY_STATE_LOADED)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup = AsyncMock(return_value=True)
|
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup=async_setup,
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
async_unload_entry=async_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
assert await manager.async_reload(entry.entry_id)
|
|
|
|
assert len(async_unload_entry.mock_calls) == 1
|
|
|
|
assert len(async_setup.mock_calls) == 1
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"state",
|
|
|
|
(
|
|
|
|
config_entries.ENTRY_STATE_NOT_LOADED,
|
|
|
|
config_entries.ENTRY_STATE_SETUP_ERROR,
|
|
|
|
config_entries.ENTRY_STATE_SETUP_RETRY,
|
|
|
|
),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
async def test_entry_reload_not_loaded(hass, manager, state):
|
|
|
|
"""Test that we can reload an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=state)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup = AsyncMock(return_value=True)
|
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup=async_setup,
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
async_unload_entry=async_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
assert await manager.async_reload(entry.entry_id)
|
|
|
|
assert len(async_unload_entry.mock_calls) == 0
|
|
|
|
assert len(async_setup.mock_calls) == 1
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"state",
|
|
|
|
(
|
|
|
|
config_entries.ENTRY_STATE_MIGRATION_ERROR,
|
|
|
|
config_entries.ENTRY_STATE_FAILED_UNLOAD,
|
|
|
|
),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
async def test_entry_reload_error(hass, manager, state):
|
|
|
|
"""Test that we can reload an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(domain="comp", state=state)
|
2019-03-01 04:27:20 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup = AsyncMock(return_value=True)
|
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup=async_setup,
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
async_unload_entry=async_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
2019-03-01 04:27:20 +00:00
|
|
|
|
|
|
|
with pytest.raises(config_entries.OperationNotAllowed):
|
|
|
|
assert await manager.async_reload(entry.entry_id)
|
|
|
|
|
|
|
|
assert len(async_unload_entry.mock_calls) == 0
|
|
|
|
assert len(async_setup.mock_calls) == 0
|
|
|
|
assert len(async_setup_entry.mock_calls) == 0
|
|
|
|
|
|
|
|
assert entry.state == state
|
2019-06-20 20:22:12 +00:00
|
|
|
|
|
|
|
|
2021-02-21 03:21:39 +00:00
|
|
|
async def test_entry_disable_succeed(hass, manager):
|
|
|
|
"""Test that we can disable an entry."""
|
|
|
|
entry = MockConfigEntry(domain="comp", state=config_entries.ENTRY_STATE_LOADED)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
async_setup = AsyncMock(return_value=True)
|
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup=async_setup,
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
async_unload_entry=async_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
# Disable
|
|
|
|
assert await manager.async_set_disabled_by(
|
|
|
|
entry.entry_id, config_entries.DISABLED_USER
|
|
|
|
)
|
|
|
|
assert len(async_unload_entry.mock_calls) == 1
|
|
|
|
assert len(async_setup.mock_calls) == 0
|
|
|
|
assert len(async_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_NOT_LOADED
|
|
|
|
|
|
|
|
# Enable
|
|
|
|
assert await manager.async_set_disabled_by(entry.entry_id, None)
|
|
|
|
assert len(async_unload_entry.mock_calls) == 1
|
|
|
|
assert len(async_setup.mock_calls) == 1
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entry_disable_without_reload_support(hass, manager):
|
|
|
|
"""Test that we can disable an entry without reload support."""
|
|
|
|
entry = MockConfigEntry(domain="comp", state=config_entries.ENTRY_STATE_LOADED)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
async_setup = AsyncMock(return_value=True)
|
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup=async_setup,
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
# Disable
|
|
|
|
assert not await manager.async_set_disabled_by(
|
|
|
|
entry.entry_id, config_entries.DISABLED_USER
|
|
|
|
)
|
|
|
|
assert len(async_setup.mock_calls) == 0
|
|
|
|
assert len(async_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_FAILED_UNLOAD
|
|
|
|
|
|
|
|
# Enable
|
|
|
|
with pytest.raises(config_entries.OperationNotAllowed):
|
|
|
|
await manager.async_set_disabled_by(entry.entry_id, None)
|
|
|
|
assert len(async_setup.mock_calls) == 0
|
|
|
|
assert len(async_setup_entry.mock_calls) == 0
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_FAILED_UNLOAD
|
|
|
|
|
|
|
|
|
|
|
|
async def test_entry_enable_without_reload_support(hass, manager):
|
|
|
|
"""Test that we can disable an entry without reload support."""
|
|
|
|
entry = MockConfigEntry(domain="comp", disabled_by=config_entries.DISABLED_USER)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
async_setup = AsyncMock(return_value=True)
|
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup=async_setup,
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
# Enable
|
|
|
|
assert await manager.async_set_disabled_by(entry.entry_id, None)
|
|
|
|
assert len(async_setup.mock_calls) == 1
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_LOADED
|
|
|
|
|
|
|
|
# Disable
|
|
|
|
assert not await manager.async_set_disabled_by(
|
|
|
|
entry.entry_id, config_entries.DISABLED_USER
|
|
|
|
)
|
|
|
|
assert len(async_setup.mock_calls) == 1
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
assert entry.state == config_entries.ENTRY_STATE_FAILED_UNLOAD
|
|
|
|
|
|
|
|
|
2019-06-20 20:22:12 +00:00
|
|
|
async def test_init_custom_integration(hass):
|
|
|
|
"""Test initializing flow for custom integration."""
|
2019-07-31 19:25:30 +00:00
|
|
|
integration = loader.Integration(
|
|
|
|
hass,
|
|
|
|
"custom_components.hue",
|
|
|
|
None,
|
|
|
|
{"name": "Hue", "dependencies": [], "requirements": [], "domain": "hue"},
|
|
|
|
)
|
2021-03-27 08:17:15 +00:00
|
|
|
with pytest.raises(data_entry_flow.UnknownHandler), patch(
|
|
|
|
"homeassistant.loader.async_get_integration",
|
|
|
|
return_value=integration,
|
|
|
|
):
|
|
|
|
await hass.config_entries.flow.async_init("bla")
|
2019-08-23 00:32:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_support_entry_unload(hass):
|
|
|
|
"""Test unloading entry."""
|
|
|
|
assert await config_entries.support_entry_unload(hass, "light")
|
|
|
|
assert not await config_entries.support_entry_unload(hass, "auth")
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reload_entry_entity_registry_ignores_no_entry(hass):
|
|
|
|
"""Test reloading entry in entity registry skips if no config entry linked."""
|
|
|
|
handler = config_entries.EntityRegistryDisabledHandler(hass)
|
|
|
|
registry = mock_registry(hass)
|
|
|
|
|
|
|
|
# Test we ignore entities without config entry
|
|
|
|
entry = registry.async_get_or_create("light", "hue", "123")
|
|
|
|
registry.async_update_entity(entry.entity_id, disabled_by="user")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert not handler.changed
|
|
|
|
assert handler._remove_call_later is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_reload_entry_entity_registry_works(hass):
|
|
|
|
"""Test we schedule an entry to be reloaded if disabled_by is updated."""
|
|
|
|
handler = config_entries.EntityRegistryDisabledHandler(hass)
|
|
|
|
handler.async_setup()
|
|
|
|
registry = mock_registry(hass)
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain="comp", state=config_entries.ENTRY_STATE_LOADED
|
|
|
|
)
|
2020-08-25 22:59:22 +00:00
|
|
|
config_entry.supports_unload = True
|
2019-08-23 00:32:43 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
mock_unload_entry = AsyncMock(return_value=True)
|
2019-08-23 00:32:43 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=mock_setup_entry,
|
|
|
|
async_unload_entry=mock_unload_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
# Only changing disabled_by should update trigger
|
|
|
|
entity_entry = registry.async_get_or_create(
|
|
|
|
"light", "hue", "123", config_entry=config_entry
|
|
|
|
)
|
|
|
|
registry.async_update_entity(entity_entry.entity_id, name="yo")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert not handler.changed
|
|
|
|
assert handler._remove_call_later is None
|
|
|
|
|
|
|
|
# Disable entity, we should not do anything, only act when enabled.
|
|
|
|
registry.async_update_entity(entity_entry.entity_id, disabled_by="user")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert not handler.changed
|
|
|
|
assert handler._remove_call_later is None
|
|
|
|
|
|
|
|
# Enable entity, check we are reloading config entry.
|
|
|
|
registry.async_update_entity(entity_entry.entity_id, disabled_by=None)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert handler.changed == {config_entry.entry_id}
|
|
|
|
assert handler._remove_call_later is not None
|
|
|
|
|
|
|
|
async_fire_time_changed(
|
|
|
|
hass,
|
2020-11-09 18:47:45 +00:00
|
|
|
dt.utcnow() + timedelta(seconds=config_entries.RELOAD_AFTER_UPDATE_DELAY + 1),
|
2019-08-23 00:32:43 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(mock_unload_entry.mock_calls) == 1
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unqiue_id_persisted(hass, manager):
|
|
|
|
"""Test that a unique ID is stored in the config entry."""
|
2020-04-30 20:29:50 +00:00
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2019-12-16 11:27:43 +00:00
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
|
|
|
return self.async_create_entry(title="mock-title", data={})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
p_hass, p_entry = mock_setup_entry.mock_calls[0][1]
|
|
|
|
|
|
|
|
assert p_hass is hass
|
|
|
|
assert p_entry.unique_id == "mock-unique-id"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unique_id_existing_entry(hass, manager):
|
|
|
|
"""Test that we remove an entry if there already is an entry with unique ID."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
unique_id="mock-unique-id",
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
|
|
|
async_unload_entry = AsyncMock(return_value=True)
|
|
|
|
async_remove_entry = AsyncMock(return_value=True)
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule(
|
|
|
|
"comp",
|
|
|
|
async_setup_entry=async_setup_entry,
|
|
|
|
async_unload_entry=async_unload_entry,
|
|
|
|
async_remove_entry=async_remove_entry,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2019-12-16 11:27:43 +00:00
|
|
|
existing_entry = await self.async_set_unique_id("mock-unique-id")
|
|
|
|
|
|
|
|
assert existing_entry is not None
|
|
|
|
|
|
|
|
return self.async_create_entry(title="mock-title", data={"via": "flow"})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
entries = hass.config_entries.async_entries("comp")
|
|
|
|
assert len(entries) == 1
|
|
|
|
assert entries[0].data == {"via": "flow"}
|
|
|
|
|
|
|
|
assert len(async_setup_entry.mock_calls) == 1
|
|
|
|
assert len(async_unload_entry.mock_calls) == 1
|
|
|
|
assert len(async_remove_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2021-03-22 04:44:29 +00:00
|
|
|
async def test_entry_id_existing_entry(hass, manager):
|
|
|
|
"""Test that we throw when the entry id collides."""
|
|
|
|
collide_entry_id = "collide"
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
MockConfigEntry(
|
|
|
|
entry_id=collide_entry_id,
|
|
|
|
domain="comp",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
unique_id="mock-unique-id",
|
|
|
|
).add_to_hass(hass)
|
|
|
|
|
2021-03-22 05:29:48 +00:00
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule("comp"),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
2021-03-22 04:44:29 +00:00
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
|
|
|
return self.async_create_entry(title="mock-title", data={"via": "flow"})
|
|
|
|
|
|
|
|
with pytest.raises(HomeAssistantError), patch.dict(
|
|
|
|
config_entries.HANDLERS, {"comp": TestFlow}
|
|
|
|
), patch(
|
|
|
|
"homeassistant.config_entries.uuid_util.random_uuid_hex",
|
|
|
|
return_value=collide_entry_id,
|
|
|
|
):
|
|
|
|
await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-08-08 18:23:56 +00:00
|
|
|
async def test_unique_id_update_existing_entry_without_reload(hass, manager):
|
2020-01-23 19:21:19 +00:00
|
|
|
"""Test that we update an entry if there already is an entry with unique ID."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
data={"additional": "data", "host": "0.0.0.0"},
|
|
|
|
unique_id="mock-unique-id",
|
2020-08-24 10:43:31 +00:00
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
2020-01-23 19:21:19 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
MockModule("comp"),
|
2020-01-23 19:21:19 +00:00
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2020-01-23 19:21:19 +00:00
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
2020-08-08 18:23:56 +00:00
|
|
|
await self._abort_if_unique_id_configured(
|
|
|
|
updates={"host": "1.1.1.1"}, reload_on_update=False
|
|
|
|
)
|
2020-01-23 19:21:19 +00:00
|
|
|
|
2020-08-08 18:23:56 +00:00
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_reload"
|
|
|
|
) as async_reload:
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data["host"] == "1.1.1.1"
|
|
|
|
assert entry.data["additional"] == "data"
|
|
|
|
assert len(async_reload.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unique_id_update_existing_entry_with_reload(hass, manager):
|
|
|
|
"""Test that we update an entry if there already is an entry with unique ID and we reload on changes."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
data={"additional": "data", "host": "0.0.0.0"},
|
|
|
|
unique_id="mock-unique-id",
|
2020-08-24 08:54:26 +00:00
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
2020-08-08 18:23:56 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
MockModule("comp"),
|
2020-08-08 18:23:56 +00:00
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
2020-08-24 08:54:26 +00:00
|
|
|
updates = {"host": "1.1.1.1"}
|
2020-08-08 18:23:56 +00:00
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
|
|
|
await self._abort_if_unique_id_configured(
|
2020-08-24 08:54:26 +00:00
|
|
|
updates=updates, reload_on_update=True
|
2020-08-08 18:23:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_reload"
|
|
|
|
) as async_reload:
|
2020-01-23 19:21:19 +00:00
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2020-08-08 18:23:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data["host"] == "1.1.1.1"
|
|
|
|
assert entry.data["additional"] == "data"
|
2020-08-08 18:23:56 +00:00
|
|
|
assert len(async_reload.mock_calls) == 1
|
2020-01-23 19:21:19 +00:00
|
|
|
|
2020-08-24 08:54:26 +00:00
|
|
|
# Test we don't reload if entry not started
|
|
|
|
updates["host"] = "2.2.2.2"
|
|
|
|
entry.state = config_entries.ENTRY_STATE_NOT_LOADED
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_reload"
|
|
|
|
) as async_reload:
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data["host"] == "2.2.2.2"
|
|
|
|
assert entry.data["additional"] == "data"
|
|
|
|
assert len(async_reload.mock_calls) == 0
|
|
|
|
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
async def test_unique_id_not_update_existing_entry(hass, manager):
|
|
|
|
"""Test that we do not update an entry if existing entry has the data."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
data={"additional": "data", "host": "0.0.0.0"},
|
|
|
|
unique_id="mock-unique-id",
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
MockModule("comp"),
|
2020-01-23 19:21:19 +00:00
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2020-01-23 19:21:19 +00:00
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
2020-08-08 18:23:56 +00:00
|
|
|
await self._abort_if_unique_id_configured(
|
|
|
|
updates={"host": "0.0.0.0"}, reload_on_update=True
|
|
|
|
)
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch(
|
2020-08-08 18:23:56 +00:00
|
|
|
"homeassistant.config_entries.ConfigEntries.async_reload"
|
|
|
|
) as async_reload:
|
2020-01-23 19:21:19 +00:00
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2020-08-08 18:23:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
assert result["type"] == "abort"
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data["host"] == "0.0.0.0"
|
|
|
|
assert entry.data["additional"] == "data"
|
2020-08-08 18:23:56 +00:00
|
|
|
assert len(async_reload.mock_calls) == 0
|
2020-01-23 19:21:19 +00:00
|
|
|
|
|
|
|
|
2019-12-16 11:27:43 +00:00
|
|
|
async def test_unique_id_in_progress(hass, manager):
|
|
|
|
"""Test that we abort if there is already a flow in progress with same unique id."""
|
|
|
|
mock_integration(hass, MockModule("comp"))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-16 11:27:43 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2019-12-16 11:27:43 +00:00
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
|
|
|
return self.async_show_form(step_id="discovery")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
# Create one to be in progress
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Will be canceled
|
|
|
|
result2 = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result2["reason"] == "already_in_progress"
|
2019-12-16 18:45:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_finish_flow_aborts_progress(hass, manager):
|
|
|
|
"""Test that when finishing a flow, we abort other flows in progress with unique ID."""
|
|
|
|
mock_integration(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
MockModule("comp", async_setup_entry=AsyncMock(return_value=True)),
|
2019-12-16 18:45:09 +00:00
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-16 18:45:09 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user step."""
|
2019-12-16 18:45:09 +00:00
|
|
|
await self.async_set_unique_id("mock-unique-id", raise_on_progress=False)
|
|
|
|
|
|
|
|
if user_input is None:
|
|
|
|
return self.async_show_form(step_id="discovery")
|
|
|
|
|
|
|
|
return self.async_create_entry(title="yo", data={})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
# Create one to be in progress
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Will finish and cancel other one.
|
|
|
|
result2 = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}, data={}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
2019-12-18 06:41:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unique_id_ignore(hass, manager):
|
|
|
|
"""Test that we can ignore flows that are in progress and have a unique ID."""
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=False)
|
2019-12-18 06:41:01 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=async_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-18 06:41:01 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test user flow."""
|
2019-12-18 06:41:01 +00:00
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
|
|
|
return self.async_show_form(step_id="discovery")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
# Create one to be in progress
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
result2 = await manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_IGNORE},
|
2021-01-12 08:26:20 +00:00
|
|
|
data={"unique_id": "mock-unique-id", "title": "Ignored Title"},
|
2019-12-18 06:41:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
# assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
|
|
|
|
# We should never set up an ignored entry.
|
|
|
|
assert len(async_setup_entry.mock_calls) == 0
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries("comp")[0]
|
|
|
|
|
|
|
|
assert entry.source == "ignore"
|
|
|
|
assert entry.unique_id == "mock-unique-id"
|
2021-01-12 08:26:20 +00:00
|
|
|
assert entry.title == "Ignored Title"
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
|
2020-12-07 08:25:04 +00:00
|
|
|
async def test_manual_add_overrides_ignored_entry(hass, manager):
|
|
|
|
"""Test that we can ignore manually add entry, overriding ignored entry."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
data={"additional": "data", "host": "0.0.0.0"},
|
|
|
|
unique_id="mock-unique-id",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(
|
|
|
|
hass,
|
|
|
|
MockModule("comp"),
|
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
|
|
|
self._abort_if_unique_id_configured(
|
|
|
|
updates={"host": "1.1.1.1"}, reload_on_update=False
|
|
|
|
)
|
|
|
|
return self.async_show_form(step_id="step2")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}), patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_reload"
|
|
|
|
) as async_reload:
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert entry.data["host"] == "1.1.1.1"
|
|
|
|
assert entry.data["additional"] == "data"
|
|
|
|
assert len(async_reload.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2021-02-04 10:08:10 +00:00
|
|
|
async def test_manual_add_overrides_ignored_entry_singleton(hass, manager):
|
|
|
|
"""Test that we can ignore manually add entry, overriding ignored entry."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
return self.async_create_entry(title="title", data={"token": "supersecret"})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
|
|
|
|
await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
p_hass, p_entry = mock_setup_entry.mock_calls[0][1]
|
|
|
|
|
|
|
|
assert p_hass is hass
|
|
|
|
assert p_entry.data == {"token": "supersecret"}
|
|
|
|
|
|
|
|
|
2021-03-22 04:57:49 +00:00
|
|
|
async def test__async_current_entries_does_not_skip_ignore_non_user(hass, manager):
|
|
|
|
"""Test that _async_current_entries does not skip ignore by default for non user step."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Test not the user step."""
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
return self.async_create_entry(title="title", data={"token": "supersecret"})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
|
|
|
|
await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_IMPORT}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test__async_current_entries_explict_skip_ignore(hass, manager):
|
|
|
|
"""Test that _async_current_entries can explicitly include ignore."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Test not the user step."""
|
|
|
|
if self._async_current_entries(include_ignore=False):
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
return self.async_create_entry(title="title", data={"token": "supersecret"})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
|
|
|
|
await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_IMPORT}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
p_hass, p_entry = mock_setup_entry.mock_calls[0][1]
|
|
|
|
|
|
|
|
assert p_hass is hass
|
|
|
|
assert p_entry.data == {"token": "supersecret"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test__async_current_entries_explict_include_ignore(hass, manager):
|
|
|
|
"""Test that _async_current_entries can explicitly include ignore."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="comp",
|
|
|
|
state=config_entries.ENTRY_STATE_LOADED,
|
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_setup_entry = AsyncMock(return_value=True)
|
|
|
|
|
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=mock_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Test not the user step."""
|
|
|
|
if self._async_current_entries(include_ignore=True):
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
return self.async_create_entry(title="title", data={"token": "supersecret"})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow, "beer": 5}):
|
|
|
|
await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_IMPORT}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 0
|
|
|
|
|
|
|
|
|
2019-12-21 10:22:07 +00:00
|
|
|
async def test_unignore_step_form(hass, manager):
|
|
|
|
"""Test that we can ignore flows that are in progress and have a unique ID, then rediscover them."""
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
2019-12-21 10:22:07 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=async_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_unignore(self, user_input):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test unignore step."""
|
2019-12-21 10:22:07 +00:00
|
|
|
unique_id = user_input["unique_id"]
|
|
|
|
await self.async_set_unique_id(unique_id)
|
|
|
|
return self.async_show_form(step_id="discovery")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_IGNORE},
|
2021-01-12 08:26:20 +00:00
|
|
|
data={"unique_id": "mock-unique-id", "title": "Ignored Title"},
|
2019-12-21 10:22:07 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries("comp")[0]
|
|
|
|
assert entry.source == "ignore"
|
|
|
|
assert entry.unique_id == "mock-unique-id"
|
|
|
|
assert entry.domain == "comp"
|
2021-01-12 08:26:20 +00:00
|
|
|
assert entry.title == "Ignored Title"
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
await manager.async_remove(entry.entry_id)
|
|
|
|
|
|
|
|
# Right after removal there shouldn't be an entry or active flows
|
|
|
|
assert len(hass.config_entries.async_entries("comp")) == 0
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
|
|
|
|
# But after a 'tick' the unignore step has run and we can see an active flow again.
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 1
|
|
|
|
|
|
|
|
# and still not config entries
|
|
|
|
assert len(hass.config_entries.async_entries("comp")) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unignore_create_entry(hass, manager):
|
|
|
|
"""Test that we can ignore flows that are in progress and have a unique ID, then rediscover them."""
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
2019-12-21 10:22:07 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=async_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_unignore(self, user_input):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test unignore step."""
|
2019-12-21 10:22:07 +00:00
|
|
|
unique_id = user_input["unique_id"]
|
|
|
|
await self.async_set_unique_id(unique_id)
|
|
|
|
return self.async_create_entry(title="yo", data={})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_IGNORE},
|
2021-01-12 08:26:20 +00:00
|
|
|
data={"unique_id": "mock-unique-id", "title": "Ignored Title"},
|
2019-12-21 10:22:07 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries("comp")[0]
|
|
|
|
assert entry.source == "ignore"
|
|
|
|
assert entry.unique_id == "mock-unique-id"
|
|
|
|
assert entry.domain == "comp"
|
2021-01-12 08:26:20 +00:00
|
|
|
assert entry.title == "Ignored Title"
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
await manager.async_remove(entry.entry_id)
|
|
|
|
|
|
|
|
# Right after removal there shouldn't be an entry or flow
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
assert len(hass.config_entries.async_entries("comp")) == 0
|
|
|
|
|
|
|
|
# But after a 'tick' the unignore step has run and we can see a config entry.
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
entry = hass.config_entries.async_entries("comp")[0]
|
|
|
|
assert entry.source == "unignore"
|
|
|
|
assert entry.unique_id == "mock-unique-id"
|
|
|
|
assert entry.title == "yo"
|
|
|
|
|
|
|
|
# And still no active flow
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unignore_default_impl(hass, manager):
|
|
|
|
"""Test that resdicovery is a no-op by default."""
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
2019-12-21 10:22:07 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=async_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_IGNORE},
|
2021-01-12 08:26:20 +00:00
|
|
|
data={"unique_id": "mock-unique-id", "title": "Ignored Title"},
|
2019-12-21 10:22:07 +00:00
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries("comp")[0]
|
|
|
|
assert entry.source == "ignore"
|
|
|
|
assert entry.unique_id == "mock-unique-id"
|
|
|
|
assert entry.domain == "comp"
|
2021-01-12 08:26:20 +00:00
|
|
|
assert entry.title == "Ignored Title"
|
2019-12-21 10:22:07 +00:00
|
|
|
|
|
|
|
await manager.async_remove(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(hass.config_entries.async_entries("comp")) == 0
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
2020-01-03 16:28:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_partial_flows_hidden(hass, manager):
|
|
|
|
"""Test that flows that don't have a cur_step and haven't finished initing are hidden."""
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
2020-01-03 16:28:05 +00:00
|
|
|
mock_integration(hass, MockModule("comp", async_setup_entry=async_setup_entry))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
await async_setup_component(hass, "persistent_notification", {})
|
|
|
|
|
|
|
|
# A flag to test our assertion that `async_step_discovery` was called and is in its blocked state
|
|
|
|
# This simulates if the step was e.g. doing network i/o
|
|
|
|
discovery_started = asyncio.Event()
|
|
|
|
|
|
|
|
# A flag to allow `async_step_discovery` to resume after we have verified the uninited flow is not
|
|
|
|
# visible and has not triggered a discovery alert. This lets us control when the mocked network
|
|
|
|
# i/o is complete.
|
|
|
|
pause_discovery = asyncio.Event()
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test flow."""
|
2020-01-03 16:28:05 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2020-06-15 11:38:38 +00:00
|
|
|
async def async_step_discovery(self, discovery_info):
|
2020-04-07 16:33:23 +00:00
|
|
|
"""Test discovery step."""
|
2020-01-03 16:28:05 +00:00
|
|
|
discovery_started.set()
|
|
|
|
await pause_discovery.wait()
|
|
|
|
return self.async_show_form(step_id="someform")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
# Start a config entry flow and wait for it to be blocked
|
|
|
|
init_task = asyncio.ensure_future(
|
|
|
|
manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_DISCOVERY},
|
|
|
|
data={"unique_id": "mock-unique-id"},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
await discovery_started.wait()
|
|
|
|
|
|
|
|
# While it's blocked it shouldn't be visible or trigger discovery notifications
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_discovery")
|
|
|
|
assert state is None
|
|
|
|
|
|
|
|
# Let the flow init complete
|
|
|
|
pause_discovery.set()
|
|
|
|
|
|
|
|
# When it's complete it should now be visible in async_progress and have triggered
|
|
|
|
# discovery notifications
|
|
|
|
result = await init_task
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 1
|
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get("persistent_notification.config_entry_discovery")
|
|
|
|
assert state is not None
|
2020-04-15 01:46:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
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(
|
2020-08-27 11:56:20 +00:00
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={},
|
2020-04-15 01:46:41 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
async_setup_entry = AsyncMock(return_value=True)
|
2020-04-15 01:46:41 +00:00
|
|
|
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(
|
2020-08-27 11:56:20 +00:00
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={},
|
2020-04-15 01:46:41 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
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."""
|
2020-08-08 18:23:56 +00:00
|
|
|
assert (
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
entry, data={"value": "updated"}
|
|
|
|
)
|
|
|
|
is True
|
2020-04-15 01:46:41 +00:00
|
|
|
)
|
|
|
|
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"}
|
2020-06-15 11:38:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"discovery_source",
|
|
|
|
(
|
|
|
|
config_entries.SOURCE_DISCOVERY,
|
|
|
|
config_entries.SOURCE_SSDP,
|
|
|
|
config_entries.SOURCE_HOMEKIT,
|
|
|
|
config_entries.SOURCE_ZEROCONF,
|
|
|
|
config_entries.SOURCE_HASSIO,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
async def test_flow_with_default_discovery(hass, manager, discovery_source):
|
|
|
|
"""Test that finishing a default discovery flow removes the unique ID in the entry."""
|
|
|
|
mock_integration(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass,
|
|
|
|
MockModule("comp", async_setup_entry=AsyncMock(return_value=True)),
|
2020-06-15 11:38:38 +00:00
|
|
|
)
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Test user step."""
|
|
|
|
if user_input is None:
|
|
|
|
return self.async_show_form(step_id="user")
|
|
|
|
|
|
|
|
return self.async_create_entry(title="yo", data={})
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
# Create one to be in progress
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": discovery_source}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
assert (
|
|
|
|
flows[0]["context"]["unique_id"]
|
|
|
|
== config_entries.DEFAULT_DISCOVERY_UNIQUE_ID
|
|
|
|
)
|
|
|
|
|
|
|
|
# Finish flow
|
|
|
|
result2 = await manager.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"fake": "data"}
|
|
|
|
)
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
|
|
|
|
|
|
|
assert len(hass.config_entries.flow.async_progress()) == 0
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries("comp")[0]
|
|
|
|
assert entry.title == "yo"
|
|
|
|
assert entry.source == discovery_source
|
|
|
|
assert entry.unique_id is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_with_default_discovery_with_unique_id(hass, manager):
|
|
|
|
"""Test discovery flow using the default discovery is ignored when unique ID is set."""
|
|
|
|
mock_integration(hass, MockModule("comp"))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_discovery(self, discovery_info):
|
|
|
|
"""Test discovery step."""
|
|
|
|
await self.async_set_unique_id("mock-unique-id")
|
|
|
|
# This call should make no difference, as a unique ID is set
|
|
|
|
await self._async_handle_discovery_without_unique_id()
|
|
|
|
return self.async_show_form(step_id="mock")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_DISCOVERY}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_discovery_abort_existing_entries(hass, manager):
|
|
|
|
"""Test that a flow without discovery implementation aborts when a config entry exists."""
|
|
|
|
hass.config.components.add("comp")
|
|
|
|
entry = MockConfigEntry(domain="comp", data={}, unique_id="mock-unique-id")
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
mock_integration(hass, MockModule("comp"))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_DISCOVERY}
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_discovery_in_progress(hass, manager):
|
|
|
|
"""Test that a flow using default discovery can only be triggered once."""
|
|
|
|
mock_integration(hass, MockModule("comp"))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_discovery(self, discovery_info):
|
|
|
|
"""Test discovery step."""
|
|
|
|
await self.async_set_unique_id(discovery_info.get("unique_id"))
|
|
|
|
await self._async_handle_discovery_without_unique_id()
|
|
|
|
return self.async_show_form(step_id="mock")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_DISCOVERY},
|
|
|
|
data={"unique_id": "mock-unique-id"},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Second discovery without a unique ID
|
|
|
|
result2 = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
|
|
|
|
)
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_discovery_abort_on_new_unique_flow(hass, manager):
|
|
|
|
"""Test that a flow using default discovery is aborted when a second flow with unique ID is created."""
|
|
|
|
mock_integration(hass, MockModule("comp"))
|
|
|
|
mock_entity_platform(hass, "config_flow.comp", None)
|
|
|
|
|
|
|
|
class TestFlow(config_entries.ConfigFlow):
|
|
|
|
"""Test flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
async def async_step_discovery(self, discovery_info):
|
|
|
|
"""Test discovery step."""
|
|
|
|
await self.async_set_unique_id(discovery_info.get("unique_id"))
|
|
|
|
await self._async_handle_discovery_without_unique_id()
|
|
|
|
return self.async_show_form(step_id="mock")
|
|
|
|
|
|
|
|
with patch.dict(config_entries.HANDLERS, {"comp": TestFlow}):
|
|
|
|
# First discovery with default, no unique ID
|
|
|
|
result2 = await manager.flow.async_init(
|
|
|
|
"comp", context={"source": config_entries.SOURCE_DISCOVERY}, data={}
|
|
|
|
)
|
|
|
|
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Second discovery brings in a unique ID
|
|
|
|
result = await manager.flow.async_init(
|
|
|
|
"comp",
|
|
|
|
context={"source": config_entries.SOURCE_DISCOVERY},
|
|
|
|
data={"unique_id": "mock-unique-id"},
|
|
|
|
)
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
|
|
|
|
# Ensure the first one is cancelled and we end up with just the last one
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
assert flows[0]["context"]["unique_id"] == "mock-unique-id"
|
2020-08-08 18:23:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_updating_entry_with_and_without_changes(manager):
|
|
|
|
"""Test that we can update an entry data."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain="test",
|
|
|
|
data={"first": True},
|
|
|
|
title="thetitle",
|
|
|
|
options={"option": True},
|
|
|
|
unique_id="abc123",
|
|
|
|
state=config_entries.ENTRY_STATE_SETUP_ERROR,
|
|
|
|
)
|
|
|
|
entry.add_to_manager(manager)
|
|
|
|
|
|
|
|
assert manager.async_update_entry(entry) is False
|
|
|
|
assert manager.async_update_entry(entry, data={"second": True}) is True
|
|
|
|
assert manager.async_update_entry(entry, data={"second": True}) is False
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(entry, data={"second": True, "third": 456}) is True
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(entry, data={"second": True, "third": 456}) is False
|
|
|
|
)
|
|
|
|
assert manager.async_update_entry(entry, options={"second": True}) is True
|
|
|
|
assert manager.async_update_entry(entry, options={"second": True}) is False
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(entry, options={"second": True, "third": "123"})
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(entry, options={"second": True, "third": "123"})
|
|
|
|
is False
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(entry, system_options={"disable_new_entities": True})
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(entry, system_options={"disable_new_entities": True})
|
|
|
|
is False
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(
|
|
|
|
entry, system_options={"disable_new_entities": False}
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
manager.async_update_entry(
|
|
|
|
entry, system_options={"disable_new_entities": False}
|
|
|
|
)
|
|
|
|
is False
|
|
|
|
)
|
|
|
|
assert manager.async_update_entry(entry, title="thetitle") is False
|
|
|
|
assert manager.async_update_entry(entry, title="newtitle") is True
|
|
|
|
assert manager.async_update_entry(entry, unique_id="abc123") is False
|
|
|
|
assert manager.async_update_entry(entry, unique_id="abc1234") is True
|