Add a service to reload config entries that can easily be called though automations (#46762)

This commit is contained in:
J. Nick Koston 2021-03-17 18:27:21 -10:00 committed by GitHub
parent 6fb0e49335
commit 08db262972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 228 additions and 72 deletions

View file

@ -11,6 +11,7 @@ import yaml
from homeassistant import config
import homeassistant.components as comps
from homeassistant.components.homeassistant import (
ATTR_ENTRY_ID,
SERVICE_CHECK_CONFIG,
SERVICE_RELOAD_CORE_CONFIG,
SERVICE_SET_LOCATION,
@ -34,9 +35,11 @@ from homeassistant.helpers import entity
from homeassistant.setup import async_setup_component
from tests.common import (
MockConfigEntry,
async_capture_events,
async_mock_service,
get_test_home_assistant,
mock_registry,
mock_service,
patch_yaml_files,
)
@ -385,3 +388,62 @@ async def test_not_allowing_recursion(hass, caplog):
f"Called service homeassistant.{service} with invalid entities homeassistant.light"
in caplog.text
), service
async def test_reload_config_entry_by_entity_id(hass):
"""Test being able to reload a config entry by entity_id."""
await async_setup_component(hass, "homeassistant", {})
entity_reg = mock_registry(hass)
entry1 = MockConfigEntry(domain="mockdomain")
entry1.add_to_hass(hass)
entry2 = MockConfigEntry(domain="mockdomain")
entry2.add_to_hass(hass)
reg_entity1 = entity_reg.async_get_or_create(
"binary_sensor", "powerwall", "battery_charging", config_entry=entry1
)
reg_entity2 = entity_reg.async_get_or_create(
"binary_sensor", "powerwall", "battery_status", config_entry=entry2
)
with patch(
"homeassistant.config_entries.ConfigEntries.async_reload",
return_value=None,
) as mock_reload:
await hass.services.async_call(
"homeassistant",
"reload_config_entry",
{"entity_id": f"{reg_entity1.entity_id},{reg_entity2.entity_id}"},
blocking=True,
)
assert len(mock_reload.mock_calls) == 2
assert {mock_reload.mock_calls[0][1][0], mock_reload.mock_calls[1][1][0]} == {
entry1.entry_id,
entry2.entry_id,
}
with pytest.raises(ValueError):
await hass.services.async_call(
"homeassistant",
"reload_config_entry",
{"entity_id": "unknown.entity_id"},
blocking=True,
)
async def test_reload_config_entry_by_entry_id(hass):
"""Test being able to reload a config entry by config entry id."""
await async_setup_component(hass, "homeassistant", {})
with patch(
"homeassistant.config_entries.ConfigEntries.async_reload",
return_value=None,
) as mock_reload:
await hass.services.async_call(
"homeassistant",
"reload_config_entry",
{ATTR_ENTRY_ID: "8955375327824e14ba89e4b29cc3ec9a"},
blocking=True,
)
assert len(mock_reload.mock_calls) == 1
assert mock_reload.mock_calls[0][1][0] == "8955375327824e14ba89e4b29cc3ec9a"