2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Plex media server monitoring."""
|
2016-06-15 06:07:00 +00:00
|
|
|
import logging
|
2019-09-09 21:28:20 +00:00
|
|
|
|
2019-10-19 21:31:15 +00:00
|
|
|
from homeassistant.core import callback
|
2020-04-07 17:17:16 +00:00
|
|
|
from homeassistant.helpers.dispatcher import (
|
|
|
|
async_dispatcher_connect,
|
|
|
|
async_dispatcher_send,
|
|
|
|
)
|
2016-06-15 06:07:00 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2020-04-06 17:15:11 +00:00
|
|
|
from homeassistant.helpers.event import async_call_later
|
2016-06-15 06:07:00 +00:00
|
|
|
|
2019-10-19 21:31:15 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_SERVER_IDENTIFIER,
|
|
|
|
DISPATCHERS,
|
|
|
|
DOMAIN as PLEX_DOMAIN,
|
|
|
|
NAME_FORMAT,
|
2020-04-06 17:15:11 +00:00
|
|
|
PLEX_UPDATE_PLATFORMS_SIGNAL,
|
2019-10-19 21:31:15 +00:00
|
|
|
PLEX_UPDATE_SENSOR_SIGNAL,
|
|
|
|
SERVERS,
|
|
|
|
)
|
2016-06-15 06:07:00 +00:00
|
|
|
|
2019-09-05 17:50:26 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-06-15 06:07:00 +00:00
|
|
|
|
|
|
|
|
2019-09-19 21:29:26 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Plex sensor from a config entry."""
|
|
|
|
server_id = config_entry.data[CONF_SERVER_IDENTIFIER]
|
2019-10-19 21:31:15 +00:00
|
|
|
plexserver = hass.data[PLEX_DOMAIN][SERVERS][server_id]
|
|
|
|
sensor = PlexSensor(plexserver)
|
|
|
|
async_add_entities([sensor])
|
2019-09-05 17:50:26 +00:00
|
|
|
|
2016-06-15 06:07:00 +00:00
|
|
|
|
|
|
|
class PlexSensor(Entity):
|
2016-08-20 22:40:16 +00:00
|
|
|
"""Representation of a Plex now playing sensor."""
|
2016-06-15 06:07:00 +00:00
|
|
|
|
2019-09-09 21:28:20 +00:00
|
|
|
def __init__(self, plex_server):
|
2016-06-15 06:07:00 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-10-19 21:31:15 +00:00
|
|
|
self.sessions = []
|
2019-09-05 17:50:26 +00:00
|
|
|
self._state = None
|
2016-06-15 06:07:00 +00:00
|
|
|
self._now_playing = []
|
2019-09-05 17:50:26 +00:00
|
|
|
self._server = plex_server
|
2019-10-19 21:31:15 +00:00
|
|
|
self._name = NAME_FORMAT.format(plex_server.friendly_name)
|
2019-09-09 21:28:20 +00:00
|
|
|
self._unique_id = f"sensor-{plex_server.machine_identifier}"
|
2016-06-15 06:07:00 +00:00
|
|
|
|
2019-10-19 21:31:15 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when about to be added to hass."""
|
|
|
|
server_id = self._server.machine_identifier
|
|
|
|
unsub = async_dispatcher_connect(
|
2019-10-21 08:44:07 +00:00
|
|
|
self.hass,
|
|
|
|
PLEX_UPDATE_SENSOR_SIGNAL.format(server_id),
|
|
|
|
self.async_refresh_sensor,
|
2019-10-19 21:31:15 +00:00
|
|
|
)
|
|
|
|
self.hass.data[PLEX_DOMAIN][DISPATCHERS][server_id].append(unsub)
|
|
|
|
|
2020-04-06 17:15:11 +00:00
|
|
|
async def async_refresh_sensor(self, sessions):
|
2019-10-19 21:31:15 +00:00
|
|
|
"""Set instance object and trigger an entity state update."""
|
2020-04-06 17:15:11 +00:00
|
|
|
_LOGGER.debug("Refreshing sensor [%s]", self.unique_id)
|
2016-06-15 06:07:00 +00:00
|
|
|
|
2020-04-06 17:15:11 +00:00
|
|
|
self.sessions = sessions
|
2020-04-06 23:18:13 +00:00
|
|
|
update_failed = False
|
2019-12-23 16:25:57 +00:00
|
|
|
|
2020-04-06 17:15:11 +00:00
|
|
|
@callback
|
|
|
|
def update_plex(_):
|
2020-04-07 17:17:16 +00:00
|
|
|
async_dispatcher_send(
|
2020-04-06 17:15:11 +00:00
|
|
|
self.hass,
|
|
|
|
PLEX_UPDATE_PLATFORMS_SIGNAL.format(self._server.machine_identifier),
|
|
|
|
)
|
2016-06-15 06:07:00 +00:00
|
|
|
|
2016-08-08 15:55:58 +00:00
|
|
|
now_playing = []
|
2019-10-19 21:31:15 +00:00
|
|
|
for sess in self.sessions:
|
2020-02-06 22:26:34 +00:00
|
|
|
if sess.TYPE == "photo":
|
|
|
|
_LOGGER.debug("Photo session detected, skipping: %s", sess)
|
|
|
|
continue
|
2020-04-06 17:15:11 +00:00
|
|
|
if not sess.usernames:
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Session temporarily incomplete, will try again: %s", sess
|
|
|
|
)
|
2020-04-06 23:18:13 +00:00
|
|
|
update_failed = True
|
|
|
|
continue
|
2018-03-18 16:25:25 +00:00
|
|
|
user = sess.usernames[0]
|
|
|
|
device = sess.players[0].title
|
2019-09-03 18:35:00 +00:00
|
|
|
now_playing_user = f"{user} - {device}"
|
2018-03-18 16:25:25 +00:00
|
|
|
now_playing_title = ""
|
|
|
|
|
2020-08-03 10:41:24 +00:00
|
|
|
if sess.TYPE == "episode":
|
2018-03-18 16:25:25 +00:00
|
|
|
# example:
|
2020-02-20 06:33:38 +00:00
|
|
|
# "Supernatural (2005) - s01e13 - Route 666"
|
2020-05-09 08:59:34 +00:00
|
|
|
|
|
|
|
def sync_io_attributes(session):
|
2020-06-19 15:12:47 +00:00
|
|
|
year = None
|
|
|
|
try:
|
|
|
|
year = session.show().year
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
return (year, session.seasonEpisode)
|
|
|
|
|
|
|
|
year, season_episode = await self.hass.async_add_executor_job(
|
2020-05-09 08:59:34 +00:00
|
|
|
sync_io_attributes, sess
|
|
|
|
)
|
2018-03-18 16:25:25 +00:00
|
|
|
season_title = sess.grandparentTitle
|
2020-06-19 15:12:47 +00:00
|
|
|
if year is not None:
|
|
|
|
season_title += f" ({year!s})"
|
2018-03-18 16:25:25 +00:00
|
|
|
episode_title = sess.title
|
2020-02-20 06:33:38 +00:00
|
|
|
now_playing_title = (
|
|
|
|
f"{season_title} - {season_episode} - {episode_title}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
elif sess.TYPE == "track":
|
2018-03-18 16:25:25 +00:00
|
|
|
# example:
|
|
|
|
# "Billy Talent - Afraid of Heights - Afraid of Heights"
|
|
|
|
track_artist = sess.grandparentTitle
|
|
|
|
track_album = sess.parentTitle
|
|
|
|
track_title = sess.title
|
2020-02-20 06:33:38 +00:00
|
|
|
now_playing_title = f"{track_artist} - {track_album} - {track_title}"
|
2020-08-03 10:41:24 +00:00
|
|
|
elif sess.TYPE == "movie":
|
2018-03-18 16:25:25 +00:00
|
|
|
# example:
|
|
|
|
# "picture_of_last_summer_camp (2015)"
|
|
|
|
# "The Incredible Hulk (2008)"
|
|
|
|
now_playing_title = sess.title
|
2020-07-12 22:09:47 +00:00
|
|
|
year = await self.hass.async_add_executor_job(getattr, sess, "year")
|
|
|
|
if year is not None:
|
|
|
|
now_playing_title += f" ({year})"
|
2020-08-03 10:41:24 +00:00
|
|
|
else:
|
|
|
|
now_playing_title = sess.title
|
2018-03-18 16:25:25 +00:00
|
|
|
|
|
|
|
now_playing.append((now_playing_user, now_playing_title))
|
2019-10-19 21:31:15 +00:00
|
|
|
self._state = len(self.sessions)
|
2016-06-15 06:07:00 +00:00
|
|
|
self._now_playing = now_playing
|
2020-02-14 00:37:52 +00:00
|
|
|
|
2020-04-06 17:15:11 +00:00
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2020-04-06 23:18:13 +00:00
|
|
|
if update_failed:
|
|
|
|
async_call_later(self.hass, 5, update_plex)
|
|
|
|
|
2020-04-06 17:15:11 +00:00
|
|
|
@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}
|
|
|
|
|
2020-02-14 00:37:52 +00:00
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return a device description for device registry."""
|
|
|
|
if self.unique_id is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return {
|
|
|
|
"identifiers": {(PLEX_DOMAIN, self._server.machine_identifier)},
|
|
|
|
"manufacturer": "Plex",
|
|
|
|
"model": "Plex Media Server",
|
|
|
|
"name": "Activity Sensor",
|
|
|
|
"sw_version": self._server.version,
|
|
|
|
}
|