diff --git a/homeassistant/components/lcn/__init__.py b/homeassistant/components/lcn/__init__.py index a10d08ad073..7fbe7e7ac0e 100644 --- a/homeassistant/components/lcn/__init__.py +++ b/homeassistant/components/lcn/__init__.py @@ -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, diff --git a/tests/components/lcn/test_init.py b/tests/components/lcn/test_init.py index bffa91d14ef..4bb8d023d3f 100644 --- a/tests/components/lcn/test_init.py +++ b/tests/components/lcn/test_init.py @@ -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."""