Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 58 additions & 43 deletions homeassistant/components/plex/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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(_):
Comment thread
jjlawren marked this conversation as resolved.
dispatcher_send(
Comment thread
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)
Comment thread
jjlawren marked this conversation as resolved.
return
user = sess.usernames[0]
device = sess.players[0].title
now_playing_user = f"{user} - {device}"
Expand All @@ -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 = (
Expand All @@ -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}
Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare Apr 7, 2020

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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."""
Expand Down