Skip to content

Commit

Permalink
unload entry
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrike committed Dec 9, 2018
1 parent f2b4d88 commit ea86d0f
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions homeassistant/components/tellduslive/__init__.py
Expand Up @@ -43,6 +43,8 @@
DATA_CONFIG_ENTRY_LOCK = 'tellduslive_config_entry_lock'
CONFIG_ENTRY_IS_SETUP = 'telldus_config_entry_is_setup'

INTERVAL_TRACKER = '{}_INTERVAL'.format(DOMAIN)


async def async_setup_entry(hass, entry):
"""Create a tellduslive session."""
Expand Down Expand Up @@ -73,7 +75,8 @@ async def async_setup_entry(hass, entry):

interval = timedelta(seconds=entry.data[KEY_SCAN_INTERVAL])
_LOGGER.debug('Update interval %s', interval)
async_track_time_interval(hass, client.update, interval)
hass.data[INTERVAL_TRACKER] = async_track_time_interval(
hass, client.update, interval)

return True

Expand All @@ -94,6 +97,19 @@ async def async_setup(hass, config):
return True


async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
interval_tracker = hass.data.pop(INTERVAL_TRACKER)
interval_tracker()
await asyncio.wait([
hass.config_entries.async_forward_entry_unload(config_entry, component)
for component in hass.data.pop(CONFIG_ENTRY_IS_SETUP)
])
del hass.data[DOMAIN]
del hass.data[DATA_CONFIG_ENTRY_LOCK]
return True


class TelldusLiveClient:
"""Get the latest data and update the states."""

Expand Down Expand Up @@ -127,13 +143,11 @@ async def _discover(self, device_id):
"""Discover the component."""
device = self._client.device(device_id)
component = self.identify_device(device)
config_entries_key = '{}.{}'.format(component, DOMAIN)
async with self._hass.data[DATA_CONFIG_ENTRY_LOCK]:
if config_entries_key not in self._hass.data[
CONFIG_ENTRY_IS_SETUP]:
if component not in self._hass.data[CONFIG_ENTRY_IS_SETUP]:
await self._hass.config_entries.async_forward_entry_setup(
self._config_entry, component)
self._hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key)
self._hass.data[CONFIG_ENTRY_IS_SETUP].add(component)
device_ids = []
if device.is_sensor:
for item in device.items:
Expand Down

0 comments on commit ea86d0f

Please sign in to comment.