-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Skip parsing Plex session if incomplete #33534
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
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 |
|---|---|---|
|
|
@@ -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( | ||
|
jjlawren marked this conversation as resolved.
|
||
| 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) | ||
|
jjlawren marked this conversation as resolved.
|
||
| 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} | ||
|
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. We normally want to set the state attribute keys explicitly to be able to check formatting and content.
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. This was legacy code copied over as-is. It's worked for a long time, I'd rather not touch it right now. |
||
|
|
||
| @property | ||
| def device_info(self): | ||
| """Return a device description for device registry.""" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.