Clean-up, ordering, constants, and extend of schema (#2903)
* Clean-up, ordering, constants, and extend of schema * Put REQUIREMENTS back and re-add line breaks * Clean-up, ordering, constants, and extend of schema * Extend platform
This commit is contained in:
1 parent
8fc27cbe43
commit
e8c6e4d561
28 files changed
+232
-228
No files matched your search
@@ -8,51 +8,58 @@ from datetime import timedelta
|
||||
import logging
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (CONF_NAME, CONF_PLATFORM, CONF_USERNAME,
|
||||
CONF_PASSWORD, CONF_HOST, CONF_PORT)
|
||||
from homeassistant.components.switch import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_HOST, CONF_PORT)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
REQUIREMENTS = ['plexapi==2.0.2']
|
||||
|
||||
CONF_SERVER = 'server'
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
vol.Required(CONF_PLATFORM): 'plex',
|
||||
vol.Optional(CONF_USERNAME): cv.string,
|
||||
CONF_SERVER = 'server'
|
||||
|
||||
DEFAULT_HOST = 'localhost'
|
||||
DEFAULT_NAME = 'Plex'
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_SERVER): cv.string,
|
||||
vol.Optional(CONF_NAME, default='Plex'): cv.string,
|
||||
vol.Optional(CONF_HOST, default='localhost'): cv.string,
|
||||
vol.Optional(CONF_PORT, default=32400): cv.port,
|
||||
vol.Optional(CONF_SERVER): cv.string,
|
||||
vol.Optional(CONF_USERNAME): cv.string,
|
||||
})
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the Demo sensors."""
|
||||
"""Setup the Plex sensor."""
|
||||
name = config.get(CONF_NAME)
|
||||
plex_user = config.get(CONF_USERNAME)
|
||||
plex_password = config.get(CONF_PASSWORD)
|
||||
plex_server = config.get(CONF_SERVER)
|
||||
plex_host = config.get(CONF_HOST)
|
||||
plex_port = config.get(CONF_PORT)
|
||||
plex_url = 'http://' + plex_host + ':' + str(plex_port)
|
||||
add_devices([PlexSensor(name, plex_url, plex_user,
|
||||
plex_password, plex_server)])
|
||||
plex_url = 'http://{}:{}'.format(plex_host, plex_port)
|
||||
|
||||
add_devices([PlexSensor(
|
||||
name, plex_url, plex_user, plex_password, plex_server)])
|
||||
|
||||
|
||||
class PlexSensor(Entity):
|
||||
"""Plex now playing sensor."""
|
||||
"""Representation of a Plex now playing sensor."""
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self, name, plex_url, plex_user, plex_password, plex_server):
|
||||
"""Initialize the sensor."""
|
||||
from plexapi.utils import NA
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
from plexapi.server import PlexServer
|
||||
|
||||
self._na_type = NA
|
||||
self._name = name
|
||||
@@ -60,12 +67,10 @@ class PlexSensor(Entity):
|
||||
self._now_playing = []
|
||||
|
||||
if plex_user and plex_password:
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
user = MyPlexAccount.signin(plex_user, plex_password)
|
||||
server = plex_server if plex_server else user.resources()[0].name
|
||||
self._server = user.resource(server).connect()
|
||||
else:
|
||||
from plexapi.server import PlexServer
|
||||
self._server = PlexServer(plex_url)
|
||||
|
||||
self.update()
|
||||
@@ -92,7 +97,7 @@ class PlexSensor(Entity):
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Update method for plex sensor."""
|
||||
"""Update method for Plex sensor."""
|
||||
sessions = self._server.sessions()
|
||||
now_playing = []
|
||||
for sess in sessions:
|
||||
|
||||
Reference in new issue
Block a user