mirror of
https://github.com/home-assistant/core.git
synced 2026-11-30 09:42:02 -04:00
Add support for disabling config entries (#46779)
This commit is contained in:
@@ -749,6 +749,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
|
||||
system_options={},
|
||||
connection_class=config_entries.CONN_CLASS_UNKNOWN,
|
||||
unique_id=None,
|
||||
disabled_by=None,
|
||||
):
|
||||
"""Initialize a mock config entry."""
|
||||
kwargs = {
|
||||
@@ -761,6 +762,7 @@ class MockConfigEntry(config_entries.ConfigEntry):
|
||||
"title": title,
|
||||
"connection_class": connection_class,
|
||||
"unique_id": unique_id,
|
||||
"disabled_by": disabled_by,
|
||||
}
|
||||
if source is not None:
|
||||
kwargs["source"] = source
|
||||
|
||||
@@ -68,6 +68,12 @@ async def test_get_entries(hass, client):
|
||||
state=core_ce.ENTRY_STATE_LOADED,
|
||||
connection_class=core_ce.CONN_CLASS_ASSUMED,
|
||||
).add_to_hass(hass)
|
||||
MockConfigEntry(
|
||||
domain="comp3",
|
||||
title="Test 3",
|
||||
source="bla3",
|
||||
disabled_by="user",
|
||||
).add_to_hass(hass)
|
||||
|
||||
resp = await client.get("/api/config/config_entries/entry")
|
||||
assert resp.status == 200
|
||||
@@ -83,6 +89,7 @@ async def test_get_entries(hass, client):
|
||||
"connection_class": "local_poll",
|
||||
"supports_options": True,
|
||||
"supports_unload": True,
|
||||
"disabled_by": None,
|
||||
},
|
||||
{
|
||||
"domain": "comp2",
|
||||
@@ -92,6 +99,17 @@ async def test_get_entries(hass, client):
|
||||
"connection_class": "assumed",
|
||||
"supports_options": False,
|
||||
"supports_unload": False,
|
||||
"disabled_by": None,
|
||||
},
|
||||
{
|
||||
"domain": "comp3",
|
||||
"title": "Test 3",
|
||||
"source": "bla3",
|
||||
"state": "not_loaded",
|
||||
"connection_class": "unknown",
|
||||
"supports_options": False,
|
||||
"supports_unload": False,
|
||||
"disabled_by": "user",
|
||||
},
|
||||
]
|
||||
|
||||
@@ -680,6 +698,25 @@ async def test_update_system_options(hass, hass_ws_client):
|
||||
assert entry.system_options.disable_new_entities
|
||||
|
||||
|
||||
async def test_update_system_options_nonexisting(hass, hass_ws_client):
|
||||
"""Test that we can update entry."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/system_options/update",
|
||||
"entry_id": "non_existing",
|
||||
"disable_new_entities": True,
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert not response["success"]
|
||||
assert response["error"]["code"] == "not_found"
|
||||
|
||||
|
||||
async def test_update_entry(hass, hass_ws_client):
|
||||
"""Test that we can update entry."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
@@ -722,6 +759,83 @@ async def test_update_entry_nonexisting(hass, hass_ws_client):
|
||||
assert response["error"]["code"] == "not_found"
|
||||
|
||||
|
||||
async def test_disable_entry(hass, hass_ws_client):
|
||||
"""Test that we can disable entry."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
entry = MockConfigEntry(domain="demo", state="loaded")
|
||||
entry.add_to_hass(hass)
|
||||
assert entry.disabled_by is None
|
||||
|
||||
# Disable
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/disable",
|
||||
"entry_id": entry.entry_id,
|
||||
"disabled_by": "user",
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert response["success"]
|
||||
assert response["result"] == {"require_restart": True}
|
||||
assert entry.disabled_by == "user"
|
||||
assert entry.state == "failed_unload"
|
||||
|
||||
# Enable
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 6,
|
||||
"type": "config_entries/disable",
|
||||
"entry_id": entry.entry_id,
|
||||
"disabled_by": None,
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert response["success"]
|
||||
assert response["result"] == {"require_restart": True}
|
||||
assert entry.disabled_by is None
|
||||
assert entry.state == "failed_unload"
|
||||
|
||||
# Enable again -> no op
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 7,
|
||||
"type": "config_entries/disable",
|
||||
"entry_id": entry.entry_id,
|
||||
"disabled_by": None,
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert response["success"]
|
||||
assert response["result"] == {"require_restart": False}
|
||||
assert entry.disabled_by is None
|
||||
assert entry.state == "failed_unload"
|
||||
|
||||
|
||||
async def test_disable_entry_nonexisting(hass, hass_ws_client):
|
||||
"""Test that we can disable entry."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/disable",
|
||||
"entry_id": "non_existing",
|
||||
"disabled_by": "user",
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert not response["success"]
|
||||
assert response["error"]["code"] == "not_found"
|
||||
|
||||
|
||||
async def test_ignore_flow(hass, hass_ws_client):
|
||||
"""Test we can ignore a flow."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
@@ -763,3 +877,22 @@ async def test_ignore_flow(hass, hass_ws_client):
|
||||
assert entry.source == "ignore"
|
||||
assert entry.unique_id == "mock-unique-id"
|
||||
assert entry.title == "Test Integration"
|
||||
|
||||
|
||||
async def test_ignore_flow_nonexisting(hass, hass_ws_client):
|
||||
"""Test we can ignore a flow."""
|
||||
assert await async_setup_component(hass, "config", {})
|
||||
ws_client = await hass_ws_client(hass)
|
||||
|
||||
await ws_client.send_json(
|
||||
{
|
||||
"id": 5,
|
||||
"type": "config_entries/ignore_flow",
|
||||
"flow_id": "non_existing",
|
||||
"title": "Test Integration",
|
||||
}
|
||||
)
|
||||
response = await ws_client.receive_json()
|
||||
|
||||
assert not response["success"]
|
||||
assert response["error"]["code"] == "not_found"
|
||||
|
||||
@@ -1209,3 +1209,41 @@ async def test_verify_suggested_area_does_not_overwrite_area_id(
|
||||
suggested_area="New Game Room",
|
||||
)
|
||||
assert entry2.area_id == game_room_area.id
|
||||
|
||||
|
||||
async def test_disable_config_entry_disables_devices(hass, registry):
|
||||
"""Test that we disable entities tied to a config entry."""
|
||||
config_entry = MockConfigEntry(domain="light")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
entry1 = registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={("mac", "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
entry2 = registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={("mac", "34:56:AB:CD:EF:12")},
|
||||
disabled_by="user",
|
||||
)
|
||||
|
||||
assert not entry1.disabled
|
||||
assert entry2.disabled
|
||||
|
||||
await hass.config_entries.async_set_disabled_by(config_entry.entry_id, "user")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entry1 = registry.async_get(entry1.id)
|
||||
assert entry1.disabled
|
||||
assert entry1.disabled_by == "config_entry"
|
||||
entry2 = registry.async_get(entry2.id)
|
||||
assert entry2.disabled
|
||||
assert entry2.disabled_by == "user"
|
||||
|
||||
await hass.config_entries.async_set_disabled_by(config_entry.entry_id, None)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entry1 = registry.async_get(entry1.id)
|
||||
assert not entry1.disabled
|
||||
entry2 = registry.async_get(entry2.id)
|
||||
assert entry2.disabled
|
||||
assert entry2.disabled_by == "user"
|
||||
|
||||
@@ -757,9 +757,18 @@ async def test_disable_device_disables_entities(hass, registry):
|
||||
device_id=device_entry.id,
|
||||
disabled_by="user",
|
||||
)
|
||||
entry3 = registry.async_get_or_create(
|
||||
"light",
|
||||
"hue",
|
||||
"EFGH",
|
||||
config_entry=config_entry,
|
||||
device_id=device_entry.id,
|
||||
disabled_by="config_entry",
|
||||
)
|
||||
|
||||
assert not entry1.disabled
|
||||
assert entry2.disabled
|
||||
assert entry3.disabled
|
||||
|
||||
device_registry.async_update_device(device_entry.id, disabled_by="user")
|
||||
await hass.async_block_till_done()
|
||||
@@ -770,6 +779,9 @@ async def test_disable_device_disables_entities(hass, registry):
|
||||
entry2 = registry.async_get(entry2.entity_id)
|
||||
assert entry2.disabled
|
||||
assert entry2.disabled_by == "user"
|
||||
entry3 = registry.async_get(entry3.entity_id)
|
||||
assert entry3.disabled
|
||||
assert entry3.disabled_by == "config_entry"
|
||||
|
||||
device_registry.async_update_device(device_entry.id, disabled_by=None)
|
||||
await hass.async_block_till_done()
|
||||
@@ -779,6 +791,74 @@ async def test_disable_device_disables_entities(hass, registry):
|
||||
entry2 = registry.async_get(entry2.entity_id)
|
||||
assert entry2.disabled
|
||||
assert entry2.disabled_by == "user"
|
||||
entry3 = registry.async_get(entry3.entity_id)
|
||||
assert entry3.disabled
|
||||
assert entry3.disabled_by == "config_entry"
|
||||
|
||||
|
||||
async def test_disable_config_entry_disables_entities(hass, registry):
|
||||
"""Test that we disable entities tied to a config entry."""
|
||||
device_registry = mock_device_registry(hass)
|
||||
config_entry = MockConfigEntry(domain="light")
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=config_entry.entry_id,
|
||||
connections={("mac", "12:34:56:AB:CD:EF")},
|
||||
)
|
||||
|
||||
entry1 = registry.async_get_or_create(
|
||||
"light",
|
||||
"hue",
|
||||
"5678",
|
||||
config_entry=config_entry,
|
||||
device_id=device_entry.id,
|
||||
)
|
||||
entry2 = registry.async_get_or_create(
|
||||
"light",
|
||||
"hue",
|
||||
"ABCD",
|
||||
config_entry=config_entry,
|
||||
device_id=device_entry.id,
|
||||
disabled_by="user",
|
||||
)
|
||||
entry3 = registry.async_get_or_create(
|
||||
"light",
|
||||
"hue",
|
||||
"EFGH",
|
||||
config_entry=config_entry,
|
||||
device_id=device_entry.id,
|
||||
disabled_by="device",
|
||||
)
|
||||
|
||||
assert not entry1.disabled
|
||||
assert entry2.disabled
|
||||
assert entry3.disabled
|
||||
|
||||
await hass.config_entries.async_set_disabled_by(config_entry.entry_id, "user")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entry1 = registry.async_get(entry1.entity_id)
|
||||
assert entry1.disabled
|
||||
assert entry1.disabled_by == "config_entry"
|
||||
entry2 = registry.async_get(entry2.entity_id)
|
||||
assert entry2.disabled
|
||||
assert entry2.disabled_by == "user"
|
||||
entry3 = registry.async_get(entry3.entity_id)
|
||||
assert entry3.disabled
|
||||
assert entry3.disabled_by == "device"
|
||||
|
||||
await hass.config_entries.async_set_disabled_by(config_entry.entry_id, None)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entry1 = registry.async_get(entry1.entity_id)
|
||||
assert not entry1.disabled
|
||||
entry2 = registry.async_get(entry2.entity_id)
|
||||
assert entry2.disabled
|
||||
assert entry2.disabled_by == "user"
|
||||
# The device was re-enabled, so entity disabled by the device will be re-enabled too
|
||||
entry3 = registry.async_get(entry3.entity_id)
|
||||
assert not entry3.disabled_by
|
||||
|
||||
|
||||
async def test_disabled_entities_excluded_from_entity_list(hass, registry):
|
||||
|
||||
@@ -1108,6 +1108,110 @@ async def test_entry_reload_error(hass, manager, state):
|
||||
assert entry.state == state
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
async def test_init_custom_integration(hass):
|
||||
"""Test initializing flow for custom integration."""
|
||||
integration = loader.Integration(
|
||||
|
||||
Reference in New Issue
Block a user