Skip to content
Merged
36 changes: 26 additions & 10 deletions homeassistant/components/sensor/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

from homeassistant.components.switch import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_HOST, CONF_PORT, CONF_TOKEN)
CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_HOST, CONF_PORT, CONF_TOKEN,
CONF_SSL)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
Expand All @@ -24,6 +25,7 @@
DEFAULT_HOST = 'localhost'
DEFAULT_NAME = 'Plex'
DEFAULT_PORT = 32400
DEFAULT_SSL = False

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)

Expand All @@ -35,6 +37,7 @@
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_SERVER): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
})


Expand All @@ -48,7 +51,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
plex_host = config.get(CONF_HOST)
plex_port = config.get(CONF_PORT)
plex_token = config.get(CONF_TOKEN)
plex_url = 'http://{}:{}'.format(plex_host, plex_port)

plex_url = '{}://{}:{}'.format('https' if config.get(CONF_SSL) else 'http',
plex_host, plex_port)

add_devices([PlexSensor(
name, plex_url, plex_user, plex_password, plex_server,
Expand All @@ -63,19 +68,30 @@ def __init__(self, name, plex_url, plex_user, plex_password,
"""Initialize the sensor."""
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
import plexapi.exceptions

cert_session = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is always None. Can this be dropped?


self._name = name
self._state = 0
self._now_playing = []

if plex_token:
self._server = PlexServer(plex_url, plex_token)
elif plex_user and plex_password:
user = MyPlexAccount(plex_user, plex_password)
server = plex_server if plex_server else user.resources()[0].name
self._server = user.resource(server).connect()
else:
self._server = PlexServer(plex_url)
try:
if plex_token:
self._server = PlexServer(plex_url, plex_token, cert_session)
elif plex_user and plex_password:
user = MyPlexAccount(plex_user, plex_password)
server = plex_server if plex_server \
else user.resources()[0].name
self._server = user.resource(server).connect()
else:
self._server = PlexServer(plex_url)

_LOGGER.info("Plex Sensor Configuration done")
except (plexapi.exceptions.BadRequest, plexapi.exceptions.Unauthorized,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this try/except to surround the add_devices line in setup platform. Ignoring the exception here means we're creating the entity without a valid self._server, which the rest of the code doesn't handle.

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.

done

plexapi.exceptions.NotFound) as error:
_LOGGER.info(error)

@emlove emlove Dec 18, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can't recover from this exception this should be an error level log.

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.

done

return

@property
def name(self):
Expand Down