diff --git a/homeassistant/components/homematicip_cloud/__init__.py b/homeassistant/components/homematicip_cloud/__init__.py index 46bf300753f..63d1f82071b 100644 --- a/homeassistant/components/homematicip_cloud/__init__.py +++ b/homeassistant/components/homematicip_cloud/__init__.py @@ -11,7 +11,7 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ENTITY_ID, CONF_NAME +from homeassistant.const import ATTR_ENTITY_ID, CONF_NAME, EVENT_HOMEASSISTANT_STOP from homeassistant.helpers import device_registry as dr import homeassistant.helpers.config_validation as cv from homeassistant.helpers.config_validation import comp_entity_ids @@ -348,6 +348,16 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool if not await hap.async_setup(): return False + async def async_reset_hap_connection(): + """Reset hmip hap connection.""" + await hap.async_reset() + _LOGGER.debug("Reset connection to access point id %s", entry.unique_id) + + # Register on HA stop event to gracefully shutdown HomematicIP Cloud connection + hap.reset_connection_listener = hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_STOP, async_reset_hap_connection() + ) + # Register hap as device in registry. device_registry = await dr.async_get_registry(hass) home = hap.home @@ -367,4 +377,5 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool: """Unload a config entry.""" hap = hass.data[DOMAIN].pop(entry.unique_id) + hap.reset_connection_listener() return await hap.async_reset() diff --git a/homeassistant/components/homematicip_cloud/hap.py b/homeassistant/components/homematicip_cloud/hap.py index 3c97cc1af9f..8a4b7ed5fae 100644 --- a/homeassistant/components/homematicip_cloud/hap.py +++ b/homeassistant/components/homematicip_cloud/hap.py @@ -81,6 +81,7 @@ class HomematicipHAP: self._tries = 0 self._accesspoint_connected = True self.hmip_device_by_entity_id = {} + self.reset_connection_listener = None async def async_setup(self, tries: int = 0) -> bool: """Initialize connection."""