From 1615f3e1fde45b9658edababa24cddfff1870ad4 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Thu, 29 Jun 2023 03:45:17 +0200 Subject: [PATCH] Add reload service to KNX (#95489) --- homeassistant/components/knx/__init__.py | 8 ++++++++ homeassistant/components/knx/services.yaml | 3 +++ tests/components/knx/test_services.py | 24 ++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/homeassistant/components/knx/__init__.py b/homeassistant/components/knx/__init__.py index cc713d1034c4e9..e8c237114b51ab 100644 --- a/homeassistant/components/knx/__init__.py +++ b/homeassistant/components/knx/__init__.py @@ -30,6 +30,7 @@ CONF_PORT, CONF_TYPE, EVENT_HOMEASSISTANT_STOP, + SERVICE_RELOAD, Platform, ) from homeassistant.core import Event, HomeAssistant, ServiceCall @@ -312,6 +313,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: schema=SERVICE_KNX_EXPOSURE_REGISTER_SCHEMA, ) + async def _reload_integration(call: ServiceCall) -> None: + """Reload the integration.""" + await hass.config_entries.async_reload(entry.entry_id) + hass.bus.async_fire(f"event_{DOMAIN}_reloaded", context=call.context) + + async_register_admin_service(hass, DOMAIN, SERVICE_RELOAD, _reload_integration) + await register_panel(hass) return True diff --git a/homeassistant/components/knx/services.yaml b/homeassistant/components/knx/services.yaml index d95a15738722f2..0ad497a30a261c 100644 --- a/homeassistant/components/knx/services.yaml +++ b/homeassistant/components/knx/services.yaml @@ -106,3 +106,6 @@ exposure_register: default: false selector: boolean: +reload: + name: Reload + description: Reload the KNX integration. diff --git a/tests/components/knx/test_services.py b/tests/components/knx/test_services.py index 5e6f856ee44413..5796eae8393e6c 100644 --- a/tests/components/knx/test_services.py +++ b/tests/components/knx/test_services.py @@ -1,7 +1,10 @@ """Test KNX services.""" +from unittest.mock import patch + import pytest from xknx.telegram.apci import GroupValueResponse, GroupValueWrite +from homeassistant.components.knx import async_unload_entry as knx_async_unload_entry from homeassistant.const import STATE_OFF, STATE_ON from homeassistant.core import HomeAssistant @@ -250,3 +253,24 @@ async def test_exposure_register(hass: HomeAssistant, knx: KNXTestKit) -> None: hass.states.async_set(test_entity, STATE_OFF, {test_attribute: 25}) await knx.assert_telegram_count(1) await knx.assert_write(test_address, (25,)) + + +async def test_reload_service( + hass: HomeAssistant, + knx: KNXTestKit, +) -> None: + """Test reload service.""" + await knx.setup_integration({}) + + with patch( + "homeassistant.components.knx.async_unload_entry", wraps=knx_async_unload_entry + ) as mock_unload_entry, patch( + "homeassistant.components.knx.async_setup_entry" + ) as mock_setup_entry: + await hass.services.async_call( + "knx", + "reload", + blocking=True, + ) + mock_unload_entry.assert_called_once() + mock_setup_entry.assert_called_once()