Register on HA stop event to gracefully shutdown HomematicIP Cloud connections (#31289)

* Register on HA stop event to gracefully shutdown HomematicIP Cloud connections

* fixes after review

* Fix lint

* switch to unload_entry

* Save listener

* Switch back to hap.async_reset()
This commit is contained in:
SukramJ 2020-01-30 22:14:43 +01:00 committed by GitHub
parent 73ea34e417
commit cd1aa46404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

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

View File

@ -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."""