Reload on connection lost for LCN integration (#133638)

pull/133842/head
Andre Lengwenus 2024-12-22 23:20:01 +01:00 committed by GitHub
parent 0560b634e3
commit 74b425a06e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -14,6 +14,7 @@ from pypck.connection import (
PchkLcnNotConnectedError,
PchkLicenseError,
)
from pypck.lcn_defs import LcnEvent
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -124,10 +125,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
# register for LCN bus messages
device_registry = dr.async_get(hass)
event_received = partial(async_host_event_received, hass, config_entry)
input_received = partial(
async_host_input_received, hass, config_entry, device_registry
)
lcn_connection.register_for_events(event_received)
lcn_connection.register_for_inputs(input_received)
return True
@ -183,6 +186,31 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
return unload_ok
def async_host_event_received(
hass: HomeAssistant, config_entry: ConfigEntry, event: pypck.lcn_defs.LcnEvent
) -> None:
"""Process received event from LCN."""
lcn_connection = hass.data[DOMAIN][config_entry.entry_id][CONNECTION]
async def reload_config_entry() -> None:
"""Close connection and schedule config entry for reload."""
await lcn_connection.async_close()
hass.config_entries.async_schedule_reload(config_entry.entry_id)
if event in (
LcnEvent.CONNECTION_LOST,
LcnEvent.PING_TIMEOUT,
):
_LOGGER.info('The connection to host "%s" has been lost', config_entry.title)
hass.async_create_task(reload_config_entry())
elif event == LcnEvent.BUS_DISCONNECTED:
_LOGGER.info(
'The connection to the LCN bus via host "%s" has been disconnected',
config_entry.title,
)
hass.async_create_task(reload_config_entry())
def async_host_input_received(
hass: HomeAssistant,
config_entry: ConfigEntry,

View File

@ -9,6 +9,7 @@ from pypck.connection import (
PchkLcnNotConnectedError,
PchkLicenseError,
)
from pypck.lcn_defs import LcnEvent
import pytest
from homeassistant import config_entries
@ -116,6 +117,22 @@ async def test_async_setup_entry_fails(
assert entry.state is ConfigEntryState.SETUP_RETRY
@pytest.mark.parametrize(
"event",
[LcnEvent.CONNECTION_LOST, LcnEvent.PING_TIMEOUT, LcnEvent.BUS_DISCONNECTED],
)
async def test_async_entry_reload_on_host_event_received(
hass: HomeAssistant, entry: MockConfigEntry, event: LcnEvent
) -> None:
"""Test for config entry reload on certain host event received."""
lcn_connection = await init_integration(hass, entry)
with patch(
"homeassistant.config_entries.ConfigEntries.async_schedule_reload"
) as async_schedule_reload:
lcn_connection.fire_event(event)
async_schedule_reload.assert_called_with(entry.entry_id)
@patch("homeassistant.components.lcn.PchkConnectionManager", MockPchkConnectionManager)
async def test_migrate_1_1(hass: HomeAssistant, entry) -> None:
"""Test migration config entry."""