diff --git a/homeassistant/components/tradfri/__init__.py b/homeassistant/components/tradfri/__init__.py index 7074532e097..af63fe192cd 100644 --- a/homeassistant/components/tradfri/__init__.py +++ b/homeassistant/components/tradfri/__init__.py @@ -1,4 +1,5 @@ """Support for IKEA Tradfri.""" +import asyncio import logging from pytradfri import Gateway, RequestError @@ -27,11 +28,13 @@ from .const import ( DOMAIN, KEY_API, KEY_GATEWAY, - TRADFRI_DEVICE_TYPES, + PLATFORMS, ) _LOGGER = logging.getLogger(__name__) +FACTORY = "tradfri_factory" + CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.Schema( @@ -96,7 +99,7 @@ async def async_setup_entry(hass, entry): """Create a gateway.""" # host, identity, key, allow_tradfri_groups - factory = APIFactory( + factory = await APIFactory.init( entry.data[CONF_HOST], psk_id=entry.data[CONF_IDENTITY], psk=entry.data[CONF_KEY], @@ -119,6 +122,8 @@ async def async_setup_entry(hass, entry): hass.data.setdefault(KEY_API, {})[entry.entry_id] = api hass.data.setdefault(KEY_GATEWAY, {})[entry.entry_id] = gateway + tradfri_data = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {} + tradfri_data[FACTORY] = factory dev_reg = await hass.helpers.device_registry.async_get_registry() dev_reg.async_get_or_create( @@ -132,9 +137,29 @@ async def async_setup_entry(hass, entry): sw_version=gateway_info.firmware_version, ) - for device in TRADFRI_DEVICE_TYPES: + for component in PLATFORMS: hass.async_create_task( - hass.config_entries.async_forward_entry_setup(entry, device) + hass.config_entries.async_forward_entry_setup(entry, component) ) return True + + +async def async_unload_entry(hass, entry): + """Unload a config entry.""" + unload_ok = all( + await asyncio.gather( + *[ + hass.config_entries.async_forward_entry_unload(entry, component) + for component in PLATFORMS + ] + ) + ) + if unload_ok: + hass.data[KEY_API].pop(entry.entry_id) + hass.data[KEY_GATEWAY].pop(entry.entry_id) + tradfri_data = hass.data[DOMAIN].pop(entry.entry_id) + factory = tradfri_data[FACTORY] + await factory.shutdown() + + return unload_ok diff --git a/homeassistant/components/tradfri/config_flow.py b/homeassistant/components/tradfri/config_flow.py index e72291c8d88..7947c3ad6de 100644 --- a/homeassistant/components/tradfri/config_flow.py +++ b/homeassistant/components/tradfri/config_flow.py @@ -90,7 +90,7 @@ class FlowHandler(config_entries.ConfigFlow): host = discovery_info["host"] for entry in self._async_current_entries(): - if entry.data[CONF_HOST] != host: + if entry.data.get(CONF_HOST) != host: continue # Backwards compat, we update old entries @@ -107,7 +107,7 @@ class FlowHandler(config_entries.ConfigFlow): async def async_step_import(self, user_input): """Import a config entry.""" for entry in self._async_current_entries(): - if entry.data[CONF_HOST] == user_input["host"]: + if entry.data.get(CONF_HOST) == user_input["host"]: return self.async_abort(reason="already_configured") # Happens if user has host directly in configuration.yaml @@ -141,8 +141,8 @@ class FlowHandler(config_entries.ConfigFlow): same_hub_entries = [ entry.entry_id for entry in self._async_current_entries() - if entry.data[CONF_GATEWAY_ID] == gateway_id - or entry.data[CONF_HOST] == host + if entry.data.get(CONF_GATEWAY_ID) == gateway_id + or entry.data.get(CONF_HOST) == host ] if same_hub_entries: @@ -161,7 +161,7 @@ async def authenticate(hass, host, security_code): identity = uuid4().hex - api_factory = APIFactory(host, psk_id=identity) + api_factory = await APIFactory.init(host, psk_id=identity) try: with async_timeout.timeout(5): @@ -170,6 +170,8 @@ async def authenticate(hass, host, security_code): raise AuthError("invalid_security_code") from err except asyncio.TimeoutError as err: raise AuthError("timeout") from err + finally: + await api_factory.shutdown() return await get_gateway_info(hass, host, identity, key) @@ -178,7 +180,7 @@ async def get_gateway_info(hass, host, identity, key): """Return info for the gateway.""" try: - factory = APIFactory(host, psk_id=identity, psk=key) + factory = await APIFactory.init(host, psk_id=identity, psk=key) api = factory.request gateway = Gateway() diff --git a/homeassistant/components/tradfri/const.py b/homeassistant/components/tradfri/const.py index ffb5d64f6d7..423620ecb2b 100644 --- a/homeassistant/components/tradfri/const.py +++ b/homeassistant/components/tradfri/const.py @@ -23,4 +23,4 @@ KEY_GATEWAY = "tradfri_gateway" KEY_SECURITY_CODE = "security_code" SUPPORTED_GROUP_FEATURES = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION SUPPORTED_LIGHT_FEATURES = SUPPORT_TRANSITION -TRADFRI_DEVICE_TYPES = ["cover", "light", "sensor", "switch"] +PLATFORMS = ["cover", "light", "sensor", "switch"] diff --git a/homeassistant/components/tradfri/manifest.json b/homeassistant/components/tradfri/manifest.json index 12457975eee..05cfdcdfeee 100644 --- a/homeassistant/components/tradfri/manifest.json +++ b/homeassistant/components/tradfri/manifest.json @@ -3,7 +3,7 @@ "name": "IKEA TRÅDFRI", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/tradfri", - "requirements": ["pytradfri[async]==6.4.0"], + "requirements": ["pytradfri[async]==7.0.0"], "homekit": { "models": ["TRADFRI"] }, diff --git a/requirements_all.txt b/requirements_all.txt index 584c6805eb4..4bca4967ed7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1805,7 +1805,7 @@ pytraccar==0.9.0 pytrackr==0.0.5 # homeassistant.components.tradfri -pytradfri[async]==6.4.0 +pytradfri[async]==7.0.0 # homeassistant.components.trafikverket_train # homeassistant.components.trafikverket_weatherstation diff --git a/requirements_test_all.txt b/requirements_test_all.txt index ccda38fb51c..4f767ff8552 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -848,7 +848,7 @@ pytile==4.0.0 pytraccar==0.9.0 # homeassistant.components.tradfri -pytradfri[async]==6.4.0 +pytradfri[async]==7.0.0 # homeassistant.components.vera pyvera==0.3.9