Skip to content

Commit

Permalink
Add reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
alengwenus committed Dec 20, 2024
1 parent 92195ff commit 2ac4992
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
28 changes: 28 additions & 0 deletions homeassistant/components/lcn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
PchkLcnNotConnectedError,
PchkLicenseError,
)
from pypck.lcn_defs import LcnEvent

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/components/lcn/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PchkLcnNotConnectedError,
PchkLicenseError,
)
from pypck.lcn_defs import LcnEvent
import pytest

from homeassistant import config_entries
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 2ac4992

Please sign in to comment.