diff --git a/homeassistant/components/plex/sensor.py b/homeassistant/components/plex/sensor.py index b1e93aec8c00da..65d1ba0371bd95 100644 --- a/homeassistant/components/plex/sensor.py +++ b/homeassistant/components/plex/sensor.py @@ -2,14 +2,16 @@ import logging from homeassistant.core import callback -from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send from homeassistant.helpers.entity import Entity +from homeassistant.helpers.event import async_call_later from .const import ( CONF_SERVER_IDENTIFIER, DISPATCHERS, DOMAIN as PLEX_DOMAIN, NAME_FORMAT, + PLEX_UPDATE_PLATFORMS_SIGNAL, PLEX_UPDATE_SENSOR_SIGNAL, SERVERS, ) @@ -55,55 +57,30 @@ async def async_added_to_hass(self): ) self.hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub) - @callback - def async_refresh_sensor(self, sessions): + async def async_refresh_sensor(self, sessions): """Set instance object and trigger an entity state update.""" - self.sessions = sessions - self.async_schedule_update_ha_state(True) - - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def unique_id(self): - """Return the id of this plex client.""" - return self._unique_id - - @property - def should_poll(self): - """Return True if entity has to be polled for state.""" - return False - - @property - def state(self): - """Return the state of the sensor.""" - return self._state - - @property - def unit_of_measurement(self): - """Return the unit this state is expressed in.""" - return "Watching" + _LOGGER.debug("Refreshing sensor [%s]", self.unique_id) - @property - def icon(self): - """Return the icon of the sensor.""" - return "mdi:plex" + self.sessions = sessions - @property - def device_state_attributes(self): - """Return the state attributes.""" - return {content[0]: content[1] for content in self._now_playing} + @callback + def update_plex(_): + dispatcher_send( + self.hass, + PLEX_UPDATE_PLATFORMS_SIGNAL.format(self._server.machine_identifier), + ) - def update(self): - """Update method for Plex sensor.""" - _LOGGER.debug("Refreshing sensor [%s]", self.unique_id) now_playing = [] for sess in self.sessions: if sess.TYPE == "photo": _LOGGER.debug("Photo session detected, skipping: %s", sess) continue + if not sess.usernames: + _LOGGER.debug( + "Session temporarily incomplete, will try again: %s", sess + ) + async_call_later(self.hass, 5, update_plex) + return user = sess.usernames[0] device = sess.players[0].title now_playing_user = f"{user} - {device}" @@ -113,8 +90,9 @@ def update(self): # example: # "Supernatural (2005) - s01e13 - Route 666" season_title = sess.grandparentTitle - if sess.show().year is not None: - season_title += f" ({sess.show().year!s})" + show = await self.hass.async_add_executor_job(sess.show) + if show.year is not None: + season_title += f" ({show.year!s})" season_episode = sess.seasonEpisode episode_title = sess.title now_playing_title = ( @@ -139,6 +117,43 @@ def update(self): self._state = len(self.sessions) self._now_playing = now_playing + self.async_write_ha_state() + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def unique_id(self): + """Return the id of this plex client.""" + return self._unique_id + + @property + def should_poll(self): + """Return True if entity has to be polled for state.""" + return False + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def unit_of_measurement(self): + """Return the unit this state is expressed in.""" + return "Watching" + + @property + def icon(self): + """Return the icon of the sensor.""" + return "mdi:plex" + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return {content[0]: content[1] for content in self._now_playing} + @property def device_info(self): """Return a device description for device registry."""