diff --git a/homeassistant/components/config/automation.py b/homeassistant/components/config/automation.py index f1a2d9aab84..5a39b786e27 100644 --- a/homeassistant/components/config/automation.py +++ b/homeassistant/components/config/automation.py @@ -23,7 +23,7 @@ async def async_setup(hass): if action != ACTION_DELETE: return - ent_reg = await entity_registry.async_get_registry(hass) + ent_reg = entity_registry.async_get(hass) entity_id = ent_reg.async_get_entity_id(DOMAIN, DOMAIN, config_key) diff --git a/homeassistant/components/config/scene.py b/homeassistant/components/config/scene.py index 6523ff84158..862c8c46f4d 100644 --- a/homeassistant/components/config/scene.py +++ b/homeassistant/components/config/scene.py @@ -20,7 +20,7 @@ async def async_setup(hass): if action != ACTION_DELETE: return - ent_reg = await entity_registry.async_get_registry(hass) + ent_reg = entity_registry.async_get(hass) entity_id = ent_reg.async_get_entity_id(DOMAIN, HA_DOMAIN, config_key) diff --git a/homeassistant/components/config/script.py b/homeassistant/components/config/script.py index 7adc766a1ab..45a7a6dc227 100644 --- a/homeassistant/components/config/script.py +++ b/homeassistant/components/config/script.py @@ -6,9 +6,9 @@ from homeassistant.components.script.config import ( ) from homeassistant.config import SCRIPT_CONFIG_PATH from homeassistant.const import SERVICE_RELOAD -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import config_validation as cv, entity_registry as er -from . import EditKeyBasedConfigView +from . import ACTION_DELETE, EditKeyBasedConfigView async def async_setup(hass): @@ -18,6 +18,18 @@ async def async_setup(hass): """post_write_hook for Config View that reloads scripts.""" await hass.services.async_call(DOMAIN, SERVICE_RELOAD) + if action != ACTION_DELETE: + return + + ent_reg = er.async_get(hass) + + entity_id = ent_reg.async_get_entity_id(DOMAIN, DOMAIN, config_key) + + if entity_id is None: + return + + ent_reg.async_remove(entity_id) + hass.http.register_view( EditScriptConfigView( DOMAIN, diff --git a/tests/components/config/test_script.py b/tests/components/config/test_script.py index dca9a8aa8a7..4b6ca1bdc8f 100644 --- a/tests/components/config/test_script.py +++ b/tests/components/config/test_script.py @@ -6,21 +6,34 @@ import pytest from homeassistant.bootstrap import async_setup_component from homeassistant.components import config +from homeassistant.helpers import entity_registry as er from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 @pytest.fixture(autouse=True) -async def setup_script(hass, stub_blueprint_populate): # noqa: F811 +async def setup_script(hass, script_config, stub_blueprint_populate): # noqa: F811 """Set up script integration.""" - assert await async_setup_component(hass, "script", {}) + assert await async_setup_component(hass, "script", {"script": script_config}) +@pytest.mark.parametrize( + "script_config", + ( + { + "one": {"alias": "Light on", "sequence": []}, + "two": {"alias": "Light off", "sequence": []}, + }, + ), +) async def test_delete_script(hass, hass_client): """Test deleting a script.""" with patch.object(config, "SECTIONS", ["script"]): await async_setup_component(hass, "config", {}) + ent_reg = er.async_get(hass) + assert len(ent_reg.entities) == 2 + client = await hass_client() orig_data = {"one": {}, "two": {}} @@ -46,3 +59,5 @@ async def test_delete_script(hass, hass_client): assert len(written) == 1 assert written[0] == {"one": {}} + + assert len(ent_reg.entities) == 1