Add reload service to KNX (#95489)

This commit is contained in:
Matthias Alphart 2023-06-29 03:45:17 +02:00 committed by GitHub
parent b86b41ebe5
commit 1615f3e1fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -30,6 +30,7 @@ from homeassistant.const import (
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

View File

@ -106,3 +106,6 @@ exposure_register:
default: false
selector:
boolean:
reload:
name: Reload
description: Reload the KNX integration.

View File

@ -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()