mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Fix Plex debounce wrapper (#33730)
* Fix debounce wrapper by converting to async * Review suggestions
This commit is contained in:
parent
cedf7e3945
commit
6dfffb23c4
@ -175,7 +175,7 @@ async def async_setup_entry(hass, entry):
|
|||||||
unsub = async_dispatcher_connect(
|
unsub = async_dispatcher_connect(
|
||||||
hass,
|
hass,
|
||||||
PLEX_UPDATE_PLATFORMS_SIGNAL.format(server_id),
|
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].setdefault(server_id, [])
|
||||||
hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
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:
|
if self.device and "playback" in self._device_protocol_capabilities:
|
||||||
self.device.setVolume(int(volume * 100), self._active_media_plexapi_type)
|
self.device.setVolume(int(volume * 100), self._active_media_plexapi_type)
|
||||||
self._volume_level = volume # store since we can't retrieve
|
self._volume_level = volume # store since we can't retrieve
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def volume_level(self):
|
def volume_level(self):
|
||||||
@ -533,31 +532,26 @@ class PlexMediaPlayer(MediaPlayerDevice):
|
|||||||
"""Send play command."""
|
"""Send play command."""
|
||||||
if self.device and "playback" in self._device_protocol_capabilities:
|
if self.device and "playback" in self._device_protocol_capabilities:
|
||||||
self.device.play(self._active_media_plexapi_type)
|
self.device.play(self._active_media_plexapi_type)
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
def media_pause(self):
|
def media_pause(self):
|
||||||
"""Send pause command."""
|
"""Send pause command."""
|
||||||
if self.device and "playback" in self._device_protocol_capabilities:
|
if self.device and "playback" in self._device_protocol_capabilities:
|
||||||
self.device.pause(self._active_media_plexapi_type)
|
self.device.pause(self._active_media_plexapi_type)
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
def media_stop(self):
|
def media_stop(self):
|
||||||
"""Send stop command."""
|
"""Send stop command."""
|
||||||
if self.device and "playback" in self._device_protocol_capabilities:
|
if self.device and "playback" in self._device_protocol_capabilities:
|
||||||
self.device.stop(self._active_media_plexapi_type)
|
self.device.stop(self._active_media_plexapi_type)
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
def media_next_track(self):
|
def media_next_track(self):
|
||||||
"""Send next track command."""
|
"""Send next track command."""
|
||||||
if self.device and "playback" in self._device_protocol_capabilities:
|
if self.device and "playback" in self._device_protocol_capabilities:
|
||||||
self.device.skipNext(self._active_media_plexapi_type)
|
self.device.skipNext(self._active_media_plexapi_type)
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
def media_previous_track(self):
|
def media_previous_track(self):
|
||||||
"""Send previous track command."""
|
"""Send previous track command."""
|
||||||
if self.device and "playback" in self._device_protocol_capabilities:
|
if self.device and "playback" in self._device_protocol_capabilities:
|
||||||
self.device.skipPrevious(self._active_media_plexapi_type)
|
self.device.skipPrevious(self._active_media_plexapi_type)
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
def play_media(self, media_type, media_id, **kwargs):
|
def play_media(self, media_type, media_id, **kwargs):
|
||||||
"""Play a piece of media."""
|
"""Play a piece of media."""
|
||||||
@ -594,8 +588,6 @@ class PlexMediaPlayer(MediaPlayerDevice):
|
|||||||
except requests.exceptions.ConnectTimeout:
|
except requests.exceptions.ConnectTimeout:
|
||||||
_LOGGER.error("Timed out playing on %s", self.name)
|
_LOGGER.error("Timed out playing on %s", self.name)
|
||||||
|
|
||||||
self.plex_server.update_platforms()
|
|
||||||
|
|
||||||
def _get_music_media(self, library_name, src):
|
def _get_music_media(self, library_name, src):
|
||||||
"""Find music media and return a Plex media object."""
|
"""Find music media and return a Plex media object."""
|
||||||
artist_name = src["artist_name"]
|
artist_name = src["artist_name"]
|
||||||
|
@ -12,7 +12,7 @@ import requests.exceptions
|
|||||||
|
|
||||||
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
|
||||||
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL
|
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 homeassistant.helpers.event import async_call_later
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -51,10 +51,10 @@ def debounce(func):
|
|||||||
"""Handle call_later callback."""
|
"""Handle call_later callback."""
|
||||||
nonlocal unsub
|
nonlocal unsub
|
||||||
unsub = None
|
unsub = None
|
||||||
await self.hass.async_add_executor_job(func, self)
|
await func(self)
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(self):
|
async def wrapper(self):
|
||||||
"""Schedule async callback."""
|
"""Schedule async callback."""
|
||||||
nonlocal unsub
|
nonlocal unsub
|
||||||
if unsub:
|
if unsub:
|
||||||
@ -186,8 +186,12 @@ class PlexServer:
|
|||||||
session,
|
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
|
@debounce
|
||||||
def update_platforms(self):
|
async def async_update_platforms(self):
|
||||||
"""Update the platform entities."""
|
"""Update the platform entities."""
|
||||||
_LOGGER.debug("Updating devices")
|
_LOGGER.debug("Updating devices")
|
||||||
|
|
||||||
@ -209,8 +213,9 @@ class PlexServer:
|
|||||||
monitored_users.add(new_user)
|
monitored_users.add(new_user)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
devices = self._plex_server.clients()
|
devices, sessions = await self.hass.async_add_executor_job(
|
||||||
sessions = self._plex_server.sessions()
|
self._fetch_platform_data
|
||||||
|
)
|
||||||
except (
|
except (
|
||||||
plexapi.exceptions.BadRequest,
|
plexapi.exceptions.BadRequest,
|
||||||
requests.exceptions.RequestException,
|
requests.exceptions.RequestException,
|
||||||
@ -271,13 +276,13 @@ class PlexServer:
|
|||||||
self._known_idle.add(client_id)
|
self._known_idle.add(client_id)
|
||||||
|
|
||||||
if new_entity_configs:
|
if new_entity_configs:
|
||||||
dispatcher_send(
|
async_dispatcher_send(
|
||||||
self.hass,
|
self.hass,
|
||||||
PLEX_NEW_MP_SIGNAL.format(self.machine_identifier),
|
PLEX_NEW_MP_SIGNAL.format(self.machine_identifier),
|
||||||
new_entity_configs,
|
new_entity_configs,
|
||||||
)
|
)
|
||||||
|
|
||||||
dispatcher_send(
|
async_dispatcher_send(
|
||||||
self.hass,
|
self.hass,
|
||||||
PLEX_UPDATE_SENSOR_SIGNAL.format(self.machine_identifier),
|
PLEX_UPDATE_SENSOR_SIGNAL.format(self.machine_identifier),
|
||||||
sessions,
|
sessions,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user