From 9c5df9cd3d30c5194ca04f30ea5a32c2ff23d0f0 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:52:08 +1300 Subject: [PATCH] Fix rest_command reload when the rest_command key is removed (#187262) --- .../components/rest_command/__init__.py | 2 +- tests/components/rest_command/test_init.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/rest_command/__init__.py b/homeassistant/components/rest_command/__init__.py index b7f102f73a28..0a802cf936df 100644 --- a/homeassistant/components/rest_command/__init__.py +++ b/homeassistant/components/rest_command/__init__.py @@ -99,7 +99,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: continue hass.services.async_remove(DOMAIN, existing_service) - for name, command_config in conf[DOMAIN].items(): + for name, command_config in conf.get(DOMAIN, {}).items(): async_register_rest_command(name, command_config) @callback diff --git a/tests/components/rest_command/test_init.py b/tests/components/rest_command/test_init.py index d8b3af2ef806..b488e01c0bbb 100644 --- a/tests/components/rest_command/test_init.py +++ b/tests/components/rest_command/test_init.py @@ -48,6 +48,25 @@ async def test_reload(hass: HomeAssistant, setup_component: ComponentSetup) -> N assert not hass.services.has_service(DOMAIN, "get_test") +async def test_reload_without_domain_config( + hass: HomeAssistant, setup_component: ComponentSetup +) -> None: + """Verify reload removes all commands when the domain key is gone.""" + await setup_component() + + assert hass.services.has_service(DOMAIN, "get_test") + + with patch( + "homeassistant.config.load_yaml_config_file", + autospec=True, + return_value={}, + ): + await hass.services.async_call(DOMAIN, SERVICE_RELOAD, blocking=True) + + assert not hass.services.has_service(DOMAIN, "get_test") + assert hass.services.has_service(DOMAIN, SERVICE_RELOAD) + + async def test_setup_tests( hass: HomeAssistant, setup_component: ComponentSetup ) -> None: