Skip to content
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

Support for Plex sensor with enforced SSL #21432

Merged
merged 2 commits into from
Mar 25, 2019
Merged
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions homeassistant/components/sensor/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.components.switch import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_HOST, CONF_PORT, CONF_TOKEN,
CONF_SSL)
CONF_SSL, CONF_VERIFY_SSL)
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
Expand All @@ -26,6 +26,7 @@
DEFAULT_NAME = 'Plex'
DEFAULT_PORT = 32400
DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = True

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)

Expand All @@ -38,6 +39,7 @@
vol.Optional(CONF_SERVER): cv.string,
vol.Optional(CONF_USERNAME): cv.string,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
})


Expand All @@ -59,7 +61,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
try:
add_entities([PlexSensor(
name, plex_url, plex_user, plex_password, plex_server,
plex_token)], True)
plex_token, config.get(CONF_VERIFY_SSL))], True)
except (plexapi.exceptions.BadRequest, plexapi.exceptions.Unauthorized,
plexapi.exceptions.NotFound) as error:
_LOGGER.error(error)
Expand All @@ -70,23 +72,30 @@ class PlexSensor(Entity):
"""Representation of a Plex now playing sensor."""

def __init__(self, name, plex_url, plex_user, plex_password,
plex_server, plex_token):
plex_server, plex_token, verify_ssl):
"""Initialize the sensor."""
from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
from requests import Session

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

cert_session = None
if not verify_ssl:
_LOGGER.info("Ignoring SSL verification")
cert_session = Session()
cert_session.verify = False

if plex_token:
self._server = PlexServer(plex_url, 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)
self._server = PlexServer(plex_url, None, cert_session)

@property
def name(self):
Expand Down