Use central polling to update entities (#24059)
* Use central polling to update entities * Fix for line length * Remove unnecessary import * Use interval * Trigger entity refreshes after commands * Lint
This commit is contained in:
parent
5c86a51b45
commit
0ba54ee9b7
1 changed files with 18 additions and 13 deletions
|
@ -6,7 +6,6 @@ import logging
|
|||
import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import util
|
||||
from homeassistant.components.media_player import (
|
||||
MediaPlayerDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.components.media_player.const import (
|
||||
|
@ -16,16 +15,13 @@ from homeassistant.components.media_player.const import (
|
|||
from homeassistant.const import (
|
||||
DEVICE_DEFAULT_NAME, STATE_IDLE, STATE_OFF, STATE_PAUSED, STATE_PLAYING)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.event import track_utc_time_change
|
||||
from homeassistant.helpers.event import track_time_interval
|
||||
from homeassistant.util import dt as dt_util
|
||||
from homeassistant.util.json import load_json, save_json
|
||||
|
||||
_CONFIGURING = {}
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
|
||||
|
||||
NAME_FORMAT = 'Plex {}'
|
||||
PLEX_CONFIG_FILE = 'plex.conf'
|
||||
PLEX_DATA = 'plex'
|
||||
|
@ -131,9 +127,9 @@ def setup_plexserver(
|
|||
|
||||
plex_clients = hass.data[PLEX_DATA]
|
||||
plex_sessions = {}
|
||||
track_utc_time_change(hass, lambda now: update_devices(), second=30)
|
||||
track_time_interval(
|
||||
hass, lambda now: update_devices(), timedelta(seconds=10))
|
||||
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
|
||||
def update_devices():
|
||||
"""Update the devices objects."""
|
||||
try:
|
||||
|
@ -211,6 +207,9 @@ def setup_plexserver(
|
|||
or client.machine_identifier
|
||||
in plex_sessions)
|
||||
|
||||
if client not in new_plex_clients:
|
||||
client.schedule_update_ha_state()
|
||||
|
||||
if not config.get(CONF_REMOVE_UNAVAILABLE_CLIENTS) \
|
||||
or client.available:
|
||||
continue
|
||||
|
@ -226,8 +225,6 @@ def setup_plexserver(
|
|||
if new_plex_clients:
|
||||
add_entities_callback(new_plex_clients)
|
||||
|
||||
update_devices()
|
||||
|
||||
|
||||
def request_configuration(host, hass, config, add_entities_callback):
|
||||
"""Request configuration steps from the user."""
|
||||
|
@ -497,6 +494,11 @@ class PlexClient(MediaPlayerDevice):
|
|||
self._session = None
|
||||
self._clear_media_details()
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return True if entity has to be polled for state."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the id of this plex client."""
|
||||
|
@ -542,10 +544,6 @@ class PlexClient(MediaPlayerDevice):
|
|||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
def update(self):
|
||||
"""Get the latest details."""
|
||||
self.update_devices(no_throttle=True)
|
||||
|
||||
@property
|
||||
def _active_media_plexapi_type(self):
|
||||
"""Get the active media type required by PlexAPI commands."""
|
||||
|
@ -688,6 +686,7 @@ class PlexClient(MediaPlayerDevice):
|
|||
self.device.setVolume(
|
||||
int(volume * 100), self._active_media_plexapi_type)
|
||||
self._volume_level = volume # store since we can't retrieve
|
||||
self.update_devices()
|
||||
|
||||
@property
|
||||
def volume_level(self):
|
||||
|
@ -724,16 +723,19 @@ class PlexClient(MediaPlayerDevice):
|
|||
"""Send play command."""
|
||||
if self.device and 'playback' in self._device_protocol_capabilities:
|
||||
self.device.play(self._active_media_plexapi_type)
|
||||
self.update_devices()
|
||||
|
||||
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.update_devices()
|
||||
|
||||
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.update_devices()
|
||||
|
||||
def turn_off(self):
|
||||
"""Turn the client off."""
|
||||
|
@ -744,11 +746,13 @@ class PlexClient(MediaPlayerDevice):
|
|||
"""Send next track command."""
|
||||
if self.device and 'playback' in self._device_protocol_capabilities:
|
||||
self.device.skipNext(self._active_media_plexapi_type)
|
||||
self.update_devices()
|
||||
|
||||
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.update_devices()
|
||||
|
||||
def play_media(self, media_type, media_id, **kwargs):
|
||||
"""Play a piece of media."""
|
||||
|
@ -852,6 +856,7 @@ class PlexClient(MediaPlayerDevice):
|
|||
'/playQueues/{}?window=100&own=1'.format(
|
||||
playqueue.playQueueID),
|
||||
}, **params))
|
||||
self.update_devices()
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue