Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unload Plex config entries #26771

Merged
merged 3 commits into from Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion homeassistant/components/plex/__init__.py
@@ -1,4 +1,5 @@
"""Support to embed Plex."""
import asyncio
import logging

import plexapi.exceptions
Expand All @@ -21,13 +22,15 @@
CONF_USE_EPISODE_ART,
CONF_SHOW_ALL_CONTROLS,
CONF_SERVER,
CONF_SERVER_IDENTIFIER,
DEFAULT_PORT,
DEFAULT_SSL,
DEFAULT_VERIFY_SSL,
DOMAIN as PLEX_DOMAIN,
PLATFORMS,
PLEX_MEDIA_PLAYER_OPTIONS,
PLEX_SERVER_CONFIG,
REFRESH_LISTENERS,
SERVERS,
)
from .server import PlexServer
Expand Down Expand Up @@ -61,7 +64,7 @@

def setup(hass, config):
"""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, {})
if plex_config:
Expand Down Expand Up @@ -129,3 +132,21 @@ async def async_setup_entry(hass, entry):
)

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
1 change: 1 addition & 0 deletions homeassistant/components/plex/const.py
Expand Up @@ -7,6 +7,7 @@
DEFAULT_VERIFY_SSL = True

PLATFORMS = ["media_player", "sensor"]
REFRESH_LISTENERS = "refresh_listeners"
SERVERS = "servers"

PLEX_CONFIG_FILE = "plex.conf"
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/plex/media_player.py
Expand Up @@ -39,6 +39,7 @@
DOMAIN as PLEX_DOMAIN,
NAME_FORMAT,
PLEX_MEDIA_PLAYER_OPTIONS,
REFRESH_LISTENERS,
SERVERS,
)

Expand Down Expand Up @@ -71,7 +72,9 @@ def _setup_platform(hass, config_entry, add_entities_callback):
plexserver = hass.data[PLEX_DOMAIN][SERVERS][server_id]
plex_clients = {}
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():
"""Update the devices objects."""
Expand Down