Fix Plex debounce wrapper (#33730)
* Fix debounce wrapper by converting to async * Review suggestions
This commit is contained in:
parent
cedf7e3945
commit
6dfffb23c4
3 changed files with 14 additions and 17 deletions
|
@ -175,7 +175,7 @@ async def async_setup_entry(hass, entry):
|
|||
unsub = async_dispatcher_connect(
|
||||
hass,
|
||||
PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id),
|
||||
plex_server.update_platforms,
|
||||
plex_server.async_update_platforms,
|
||||
)
|
||||
hass.data[PLEX_DOMAIN][DISPATCHERS].setdefault(server_id, [])
|
||||
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
||||
|
|
|
@ -494,7 +494,6 @@ class PlexMediaPlayer(MediaPlayerDevice):
|
|||
if self.device and "playback" in self._device_protocol_capabilities:
|
||||
self.device.setVolume(int(volume * 100), self._active_media_plexapi_type)
|
||||
self._volume_level = volume # store since we can't retrieve
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
@property
|
||||
def volume_level(self):
|
||||
|
@ -533,31 +532,26 @@ class PlexMediaPlayer(MediaPlayerDevice):
|
|||
"""Send play command."""
|
||||
if self.device and "playback" in self._device_protocol_capabilities:
|
||||
self.device.play(self._active_media_plexapi_type)
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
def media_pause(self):
|
||||
"""Send pause command."""
|
||||
if self.device and "playback" in self._device_protocol_capabilities:
|
||||
self.device.pause(self._active_media_plexapi_type)
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
def media_stop(self):
|
||||
"""Send stop command."""
|
||||
if self.device and "playback" in self._device_protocol_capabilities:
|
||||
self.device.stop(self._active_media_plexapi_type)
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
def media_next_track(self):
|
||||
"""Send next track command."""
|
||||
if self.device and "playback" in self._device_protocol_capabilities:
|
||||
self.device.skipNext(self._active_media_plexapi_type)
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
def media_previous_track(self):
|
||||
"""Send previous track command."""
|
||||
if self.device and "playback" in self._device_protocol_capabilities:
|
||||
self.device.skipPrevious(self._active_media_plexapi_type)
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
def play_media(self, media_type, media_id, **kwargs):
|
||||
"""Play a piece of media."""
|
||||
|
@ -594,8 +588,6 @@ class PlexMediaPlayer(MediaPlayerDevice):
|
|||
except requests.exceptions.ConnectTimeout:
|
||||
_LOGGER.error("Timed out playing on %s", self.name)
|
||||
|
||||
self.plex_server.update_platforms()
|
||||
|
||||
def _get_music_media(self, library_name, src):
|
||||
"""Find music media and return a Plex media object."""
|
||||
artist_name = src["artist_name"]
|
||||
|
|
|
@ -12,7 +12,7 @@ import requests.exceptions
|
|||
|
||||
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.dispatcher import async_dispatcher_send, dispatcher_send
|
||||
from homeassistant.helpers.event import async_call_later
|
||||
|
||||
from .const import (
|
||||
|
@ -51,10 +51,10 @@ def debounce(func):
|
|||
"""Handle call_later callback."""
|
||||
nonlocal unsub
|
||||
unsub = None
|
||||
await self.hass.async_add_executor_job(func, self)
|
||||
await func(self)
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(self):
|
||||
async def wrapper(self):
|
||||
"""Schedule async callback."""
|
||||
nonlocal unsub
|
||||
if unsub:
|
||||
|
@ -186,8 +186,12 @@ class PlexServer:
|
|||
session,
|
||||
)
|
||||
|
||||
def _fetch_platform_data(self):
|
||||
"""Fetch all data from the Plex server in a single method."""
|
||||
return (self._plex_server.clients(), self._plex_server.sessions())
|
||||
|
||||
@debounce
|
||||
def update_platforms(self):
|
||||
async def async_update_platforms(self):
|
||||
"""Update the platform entities."""
|
||||
_LOGGER.debug("Updating devices")
|
||||
|
||||
|
@ -209,8 +213,9 @@ class PlexServer:
|
|||
monitored_users.add(new_user)
|
||||
|
||||
try:
|
||||
devices = self._plex_server.clients()
|
||||
sessions = self._plex_server.sessions()
|
||||
devices, sessions = await self.hass.async_add_executor_job(
|
||||
self._fetch_platform_data
|
||||
)
|
||||
except (
|
||||
plexapi.exceptions.BadRequest,
|
||||
requests.exceptions.RequestException,
|
||||
|
@ -271,13 +276,13 @@ class PlexServer:
|
|||
self._known_idle.add(client_id)
|
||||
|
||||
if new_entity_configs:
|
||||
dispatcher_send(
|
||||
async_dispatcher_send(
|
||||
self.hass,
|
||||
PLEX_NEW_MP_SIGNAL.format(self.machine_identifier),
|
||||
new_entity_configs,
|
||||
)
|
||||
|
||||
dispatcher_send(
|
||||
async_dispatcher_send(
|
||||
self.hass,
|
||||
PLEX_UPDATE_SENSOR_SIGNAL.format(self.machine_identifier),
|
||||
sessions,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue