-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Debounce calls to Plex server #33560
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
Changes from all commits
e485e0c
2cd6213
d6a58a8
19b776e
280376c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Shared class to maintain Plex server instances.""" | ||
| from functools import partial, wraps | ||
| import logging | ||
| import ssl | ||
| from urllib.parse import urlparse | ||
|
|
@@ -12,13 +13,15 @@ | |
| from homeassistant.components.media_player import DOMAIN as MP_DOMAIN | ||
| from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL | ||
| from homeassistant.helpers.dispatcher import dispatcher_send | ||
| from homeassistant.helpers.event import async_call_later | ||
|
|
||
| from .const import ( | ||
| CONF_CLIENT_IDENTIFIER, | ||
| CONF_IGNORE_NEW_SHARED_USERS, | ||
| CONF_MONITORED_USERS, | ||
| CONF_SERVER, | ||
| CONF_USE_EPISODE_ART, | ||
| DEBOUNCE_TIMEOUT, | ||
| DEFAULT_VERIFY_SSL, | ||
| PLEX_NEW_MP_SIGNAL, | ||
| PLEX_UPDATE_MEDIA_PLAYER_SIGNAL, | ||
|
|
@@ -39,12 +42,37 @@ | |
| plexapi.X_PLEX_VERSION = X_PLEX_VERSION | ||
|
|
||
|
|
||
| def debounce(func): | ||
| """Decorate function to debounce callbacks from Plex websocket.""" | ||
|
|
||
| unsub = None | ||
|
|
||
| async def call_later_listener(self, _): | ||
| """Handle call_later callback.""" | ||
| nonlocal unsub | ||
| unsub = None | ||
| await self.hass.async_add_executor_job(func, self) | ||
|
|
||
| @wraps(func) | ||
| def wrapper(self): | ||
| """Schedule async callback.""" | ||
| nonlocal unsub | ||
| if unsub: | ||
| _LOGGER.debug("Throttling update of %s", self.friendly_name) | ||
| unsub() # pylint: disable=not-callable | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsub functions should only be called from the same context as in which they are set. So if created with Also, because this is running in a thread, you cannot guarantee that nothing else touches it. You should instead run this all inside the event loop.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to turn this the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really sorry about that, hopefully fixed in #33730. |
||
| unsub = async_call_later( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is calling an async method from a sync context. |
||
| self.hass, DEBOUNCE_TIMEOUT, partial(call_later_listener, self), | ||
|
jjlawren marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| class PlexServer: | ||
| """Manages a single Plex server connection.""" | ||
|
|
||
| def __init__(self, hass, server_config, known_server_id=None, options=None): | ||
| """Initialize a Plex server instance.""" | ||
| self._hass = hass | ||
| self.hass = hass | ||
| self._plex_server = None | ||
| self._known_clients = set() | ||
| self._known_idle = set() | ||
|
|
@@ -150,12 +178,13 @@ def refresh_entity(self, machine_identifier, device, session): | |
| unique_id = f"{self.machine_identifier}:{machine_identifier}" | ||
| _LOGGER.debug("Refreshing %s", unique_id) | ||
| dispatcher_send( | ||
| self._hass, | ||
| self.hass, | ||
| PLEX_UPDATE_MEDIA_PLAYER_SIGNAL.format(unique_id), | ||
| device, | ||
| session, | ||
| ) | ||
|
|
||
| @debounce | ||
| def update_platforms(self): | ||
| """Update the platform entities.""" | ||
| _LOGGER.debug("Updating devices") | ||
|
|
@@ -239,13 +268,13 @@ def update_platforms(self): | |
|
|
||
| if new_entity_configs: | ||
| dispatcher_send( | ||
| self._hass, | ||
| self.hass, | ||
| PLEX_NEW_MP_SIGNAL.format(self.machine_identifier), | ||
| new_entity_configs, | ||
| ) | ||
|
|
||
| dispatcher_send( | ||
| self._hass, | ||
| self.hass, | ||
| PLEX_UPDATE_SENSOR_SIGNAL.format(self.machine_identifier), | ||
| sessions, | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| """Common fixtures and functions for Plex tests.""" | ||
| from datetime import timedelta | ||
|
|
||
| from homeassistant.components.plex.const import ( | ||
| DEBOUNCE_TIMEOUT, | ||
| PLEX_UPDATE_PLATFORMS_SIGNAL, | ||
| ) | ||
| from homeassistant.helpers.dispatcher import async_dispatcher_send | ||
| import homeassistant.util.dt as dt_util | ||
|
|
||
| from tests.common import async_fire_time_changed | ||
|
|
||
|
|
||
| async def trigger_plex_update(hass, server_id): | ||
| """Update Plex by sending signal and jumping ahead by debounce timeout.""" | ||
| async_dispatcher_send(hass, PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id)) | ||
| await hass.async_block_till_done() | ||
| next_update = dt_util.utcnow() + timedelta(seconds=DEBOUNCE_TIMEOUT) | ||
| async_fire_time_changed(hass, next_update) | ||
| await hass.async_block_till_done() |
Uh oh!
There was an error while loading. Please reload this page.