Skip to content

Commit

Permalink
feat: support unloading of entries
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Sep 15, 2019
1 parent dfe85da commit 75a46c4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
50 changes: 38 additions & 12 deletions custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,12 @@ async def async_setup(hass, config, discovery_info=None):

async def async_setup_entry(hass, config_entry):
"""Set up Alexa Media Player as config entry."""
async def close_alexa_media(event) -> None:
async def close_alexa_media(event=None) -> None:
"""Clean up Alexa connections."""
_LOGGER.debug("Received shutdown request: %s", event)
for email, account_dict in (hass.data
[DATA_ALEXAMEDIA]['accounts'].items()):
login_obj = account_dict['login_obj']
if not login_obj._session.closed:
if login_obj._session._connector_owner:
await login_obj._session._connector.close()
login_obj._session._connector = None
_LOGGER.debug("%s: Connection closed: %s",
hide_email(email),
login_obj._session.closed)
for email, _ in (hass.data
[DATA_ALEXAMEDIA]['accounts'].items()):
await close_connections(hass, email)
_verify_domain_control = verify_domain_control(hass, DOMAIN)
if DATA_ALEXAMEDIA not in hass.data:
hass.data[DATA_ALEXAMEDIA] = {}
Expand Down Expand Up @@ -363,6 +356,8 @@ async def update_devices(login_obj):
"""
from alexapy import AlexaAPI
email: Text = login_obj.email
if email not in hass.data[DATA_ALEXAMEDIA]['accounts']:
return
existing_serials = (hass.data[DATA_ALEXAMEDIA]
['accounts']
[email]
Expand Down Expand Up @@ -796,9 +791,40 @@ async def ws_error_handler(message):
schema=LAST_CALL_UPDATE_SCHEMA)

# Clear configurator. We delay till here to avoid leaving a modal orphan
await clear_configurator(hass, email)
return True


async def async_unload_entry(hass, entry) -> bool:
"""Unload a config entry."""
email = entry.data['email']
await close_connections(hass, email)
await clear_configurator(hass, email)
hass.data[DATA_ALEXAMEDIA]['accounts'].pop(email)
_LOGGER.debug("Unloaded entry for %s", hide_email(email))
return True


async def clear_configurator(hass, email: Text) -> None:
"""Clear open configurators for email."""
if email not in hass.data[DATA_ALEXAMEDIA]['accounts']:
return
if 'configurator' in hass.data[DATA_ALEXAMEDIA]['accounts'][email]:
for config_id in hass.data[DATA_ALEXAMEDIA]['accounts'][email]['configurator']:
configurator = hass.components.configurator
configurator.async_request_done(config_id)
hass.data[DATA_ALEXAMEDIA]['accounts'][email]['configurator'] = []
return True


async def close_connections(hass, email: Text) -> None:
"""Clear open aiohttp connections for email."""
if (email not in hass.data[DATA_ALEXAMEDIA]['accounts'] or
'login_obj' not in hass.data[DATA_ALEXAMEDIA]['accounts'][email]):
return
account_dict = hass.data[DATA_ALEXAMEDIA]['accounts'][email]
login_obj = account_dict['login_obj']
await login_obj.close()
_LOGGER.debug("%s: Connection closed: %s",
hide_email(email),
login_obj._session.closed)
await clear_configurator(hass, email)
7 changes: 6 additions & 1 deletion custom_components/alexa_media/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,16 @@ async def init(self):
async def async_added_to_hass(self):
"""Store register state change callback."""
# Register event handler on bus
self.hass.bus.async_listen(('{}_{}'.format(
self._listener = self.hass.bus.async_listen(('{}_{}'.format(
ALEXA_DOMAIN,
hide_email(self._login.email)))[0:32],
self._handle_event)

async def async_will_remove_from_hass(self):
"""Prepare to remove entity."""
# Register event handler on bus
self._listener()

def _handle_event(self, event):
"""Handle websocket events.
Expand Down
14 changes: 11 additions & 3 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,15 @@ async def init(self, device):
async def async_added_to_hass(self):
"""Store register state change callback."""
# Register event handler on bus
self.hass.bus.async_listen(('{}_{}'.format(ALEXA_DOMAIN,
hide_email(self._login.email)))[0:32],
self._handle_event)
self._listener = self.hass.bus.async_listen(
('{}_{}'.format(ALEXA_DOMAIN,
hide_email(self._login.email)))[0:32],
self._handle_event)

async def async_will_remove_from_hass(self):
"""Prepare to remove entity."""
# Register event handler on bus
self._listener()

async def _handle_event(self, event):
"""Handle events.
Expand Down Expand Up @@ -473,6 +479,8 @@ async def async_update(self):
# Device has not initialized yet
return
email = self._login.email
if email not in hass.data[DATA_ALEXAMEDIA]['accounts']:
return
device = (self.hass.data[DATA_ALEXAMEDIA]
['accounts']
[email]
Expand Down
7 changes: 6 additions & 1 deletion custom_components/alexa_media/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,17 @@ def __init__(self,
async def async_added_to_hass(self):
"""Store register state change callback."""
# Register event handler on bus
self.hass.bus.async_listen(
self._listener = self.hass.bus.async_listen(
('{}_{}'.format(
ALEXA_DOMAIN,
hide_email(self._account)))[0:32],
self._handle_event)

async def async_will_remove_from_hass(self):
"""Prepare to remove entity."""
# Register event handler on bus
self._listener()

def _handle_event(self, event):
"""Handle events.
Expand Down

0 comments on commit 75a46c4

Please sign in to comment.