Unload Plex config entries (#26771)

* Unload config entries

* Await coroutines

* Unnecessary ensure
This commit is contained in:
jjlawren 2019-09-22 16:47:41 -05:00 committed by Martin Hjelmare
parent 49fef9a6a0
commit f82f30dc62
3 changed files with 27 additions and 2 deletions

View File

@ -1,4 +1,5 @@
"""Support to embed Plex.""" """Support to embed Plex."""
import asyncio
import logging import logging
import plexapi.exceptions import plexapi.exceptions
@ -21,6 +22,7 @@ from .const import (
CONF_USE_EPISODE_ART, CONF_USE_EPISODE_ART,
CONF_SHOW_ALL_CONTROLS, CONF_SHOW_ALL_CONTROLS,
CONF_SERVER, CONF_SERVER,
CONF_SERVER_IDENTIFIER,
DEFAULT_PORT, DEFAULT_PORT,
DEFAULT_SSL, DEFAULT_SSL,
DEFAULT_VERIFY_SSL, DEFAULT_VERIFY_SSL,
@ -28,6 +30,7 @@ from .const import (
PLATFORMS, PLATFORMS,
PLEX_MEDIA_PLAYER_OPTIONS, PLEX_MEDIA_PLAYER_OPTIONS,
PLEX_SERVER_CONFIG, PLEX_SERVER_CONFIG,
REFRESH_LISTENERS,
SERVERS, SERVERS,
) )
from .server import PlexServer from .server import PlexServer
@ -61,7 +64,7 @@ _LOGGER = logging.getLogger(__package__)
def setup(hass, config): def setup(hass, config):
"""Set up the Plex component.""" """Set up the Plex component."""
hass.data.setdefault(PLEX_DOMAIN, {SERVERS: {}}) hass.data.setdefault(PLEX_DOMAIN, {SERVERS: {}, REFRESH_LISTENERS: {}})
plex_config = config.get(PLEX_DOMAIN, {}) plex_config = config.get(PLEX_DOMAIN, {})
if plex_config: if plex_config:
@ -129,3 +132,21 @@ async def async_setup_entry(hass, entry):
) )
return True return True
async def async_unload_entry(hass, entry):
"""Unload a config entry."""
server_id = entry.data[CONF_SERVER_IDENTIFIER]
cancel = hass.data[PLEX_DOMAIN][REFRESH_LISTENERS].pop(server_id)
await hass.async_add_executor_job(cancel)
tasks = [
hass.config_entries.async_forward_entry_unload(entry, platform)
for platform in PLATFORMS
]
await asyncio.gather(*tasks)
hass.data[PLEX_DOMAIN][SERVERS].pop(server_id)
return True

View File

@ -7,6 +7,7 @@ DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = True DEFAULT_VERIFY_SSL = True
PLATFORMS = ["media_player", "sensor"] PLATFORMS = ["media_player", "sensor"]
REFRESH_LISTENERS = "refresh_listeners"
SERVERS = "servers" SERVERS = "servers"
PLEX_CONFIG_FILE = "plex.conf" PLEX_CONFIG_FILE = "plex.conf"

View File

@ -39,6 +39,7 @@ from .const import (
DOMAIN as PLEX_DOMAIN, DOMAIN as PLEX_DOMAIN,
NAME_FORMAT, NAME_FORMAT,
PLEX_MEDIA_PLAYER_OPTIONS, PLEX_MEDIA_PLAYER_OPTIONS,
REFRESH_LISTENERS,
SERVERS, SERVERS,
) )
@ -71,7 +72,9 @@ def _setup_platform(hass, config_entry, add_entities_callback):
plexserver = hass.data[PLEX_DOMAIN][SERVERS][server_id] plexserver = hass.data[PLEX_DOMAIN][SERVERS][server_id]
plex_clients = {} plex_clients = {}
plex_sessions = {} plex_sessions = {}
track_time_interval(hass, lambda now: update_devices(), timedelta(seconds=10)) hass.data[PLEX_DOMAIN][REFRESH_LISTENERS][server_id] = track_time_interval(
hass, lambda now: update_devices(), timedelta(seconds=10)
)
def update_devices(): def update_devices():
"""Update the devices objects.""" """Update the devices objects."""