mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Fix Plex setup race condition v2 (#28943)
* Connect websocket when platforms ready, not when HA is ready * Use callbacks from platform setup tasks instead * Convert setup to async * Apply suggestions from code review Co-Authored-By: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
614842bcf2
commit
2c00a9ec68
@ -1,5 +1,6 @@
|
|||||||
"""Support to embed Plex."""
|
"""Support to embed Plex."""
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import plexapi.exceptions
|
import plexapi.exceptions
|
||||||
@ -16,7 +17,6 @@ from homeassistant.const import (
|
|||||||
CONF_TOKEN,
|
CONF_TOKEN,
|
||||||
CONF_URL,
|
CONF_URL,
|
||||||
CONF_VERIFY_SSL,
|
CONF_VERIFY_SSL,
|
||||||
EVENT_HOMEASSISTANT_START,
|
|
||||||
EVENT_HOMEASSISTANT_STOP,
|
EVENT_HOMEASSISTANT_STOP,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
@ -37,6 +37,7 @@ from .const import (
|
|||||||
DISPATCHERS,
|
DISPATCHERS,
|
||||||
DOMAIN as PLEX_DOMAIN,
|
DOMAIN as PLEX_DOMAIN,
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
|
PLATFORMS_COMPLETED,
|
||||||
PLEX_MEDIA_PLAYER_OPTIONS,
|
PLEX_MEDIA_PLAYER_OPTIONS,
|
||||||
PLEX_SERVER_CONFIG,
|
PLEX_SERVER_CONFIG,
|
||||||
PLEX_UPDATE_PLATFORMS_SIGNAL,
|
PLEX_UPDATE_PLATFORMS_SIGNAL,
|
||||||
@ -72,18 +73,21 @@ CONFIG_SCHEMA = vol.Schema({PLEX_DOMAIN: SERVER_CONFIG_SCHEMA}, extra=vol.ALLOW_
|
|||||||
_LOGGER = logging.getLogger(__package__)
|
_LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
"""Set up the Plex component."""
|
"""Set up the Plex component."""
|
||||||
hass.data.setdefault(PLEX_DOMAIN, {SERVERS: {}, DISPATCHERS: {}, WEBSOCKETS: {}})
|
hass.data.setdefault(
|
||||||
|
PLEX_DOMAIN,
|
||||||
|
{SERVERS: {}, DISPATCHERS: {}, WEBSOCKETS: {}, PLATFORMS_COMPLETED: {}},
|
||||||
|
)
|
||||||
|
|
||||||
plex_config = config.get(PLEX_DOMAIN, {})
|
plex_config = config.get(PLEX_DOMAIN, {})
|
||||||
if plex_config:
|
if plex_config:
|
||||||
_setup_plex(hass, plex_config)
|
_async_setup_plex(hass, plex_config)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _setup_plex(hass, config):
|
def _async_setup_plex(hass, config):
|
||||||
"""Pass configuration to a config flow."""
|
"""Pass configuration to a config flow."""
|
||||||
server_config = dict(config)
|
server_config = dict(config)
|
||||||
if MP_DOMAIN in server_config:
|
if MP_DOMAIN in server_config:
|
||||||
@ -141,11 +145,7 @@ async def async_setup_entry(hass, entry):
|
|||||||
)
|
)
|
||||||
server_id = plex_server.machine_identifier
|
server_id = plex_server.machine_identifier
|
||||||
hass.data[PLEX_DOMAIN][SERVERS][server_id] = plex_server
|
hass.data[PLEX_DOMAIN][SERVERS][server_id] = plex_server
|
||||||
|
hass.data[PLEX_DOMAIN][PLATFORMS_COMPLETED][server_id] = set()
|
||||||
for platform in PLATFORMS:
|
|
||||||
hass.async_create_task(
|
|
||||||
hass.config_entries.async_forward_entry_setup(entry, platform)
|
|
||||||
)
|
|
||||||
|
|
||||||
entry.add_update_listener(async_options_updated)
|
entry.add_update_listener(async_options_updated)
|
||||||
|
|
||||||
@ -167,19 +167,25 @@ async def async_setup_entry(hass, entry):
|
|||||||
)
|
)
|
||||||
hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket
|
hass.data[PLEX_DOMAIN][WEBSOCKETS][server_id] = websocket
|
||||||
|
|
||||||
async def async_start_websocket_session(_):
|
def start_websocket_session(platform, _):
|
||||||
await websocket.listen()
|
hass.data[PLEX_DOMAIN][PLATFORMS_COMPLETED][server_id].add(platform)
|
||||||
|
if hass.data[PLEX_DOMAIN][PLATFORMS_COMPLETED][server_id] == PLATFORMS:
|
||||||
|
hass.loop.create_task(websocket.listen())
|
||||||
|
|
||||||
def close_websocket_session(_):
|
def close_websocket_session(_):
|
||||||
websocket.close()
|
websocket.close()
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, async_start_websocket_session)
|
|
||||||
|
|
||||||
unsub = hass.bus.async_listen_once(
|
unsub = hass.bus.async_listen_once(
|
||||||
EVENT_HOMEASSISTANT_STOP, close_websocket_session
|
EVENT_HOMEASSISTANT_STOP, close_websocket_session
|
||||||
)
|
)
|
||||||
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
||||||
|
|
||||||
|
for platform in PLATFORMS:
|
||||||
|
task = hass.async_create_task(
|
||||||
|
hass.config_entries.async_forward_entry_setup(entry, platform)
|
||||||
|
)
|
||||||
|
task.add_done_callback(functools.partial(start_websocket_session, platform))
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ DEFAULT_SSL = False
|
|||||||
DEFAULT_VERIFY_SSL = True
|
DEFAULT_VERIFY_SSL = True
|
||||||
|
|
||||||
DISPATCHERS = "dispatchers"
|
DISPATCHERS = "dispatchers"
|
||||||
PLATFORMS = ["media_player", "sensor"]
|
PLATFORMS = frozenset(["media_player", "sensor"])
|
||||||
|
PLATFORMS_COMPLETED = "platforms_completed"
|
||||||
SERVERS = "servers"
|
SERVERS = "servers"
|
||||||
WEBSOCKETS = "websockets"
|
WEBSOCKETS = "websockets"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user