mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Basic discovery works, added plex logo for configurator. Missing configurator support for fields. Todo: config save on successful connect
This commit is contained in:
parent
6a82504e5e
commit
884525df33
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
@ -10,12 +10,14 @@ import logging
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from homeassistant.loader import get_component
|
||||||
|
import homeassistant.util as util
|
||||||
from homeassistant.components.media_player import (
|
from homeassistant.components.media_player import (
|
||||||
MediaPlayerDevice, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK,
|
MediaPlayerDevice, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK,
|
||||||
SUPPORT_NEXT_TRACK, MEDIA_TYPE_TVSHOW, MEDIA_TYPE_VIDEO)
|
SUPPORT_NEXT_TRACK, MEDIA_TYPE_TVSHOW, MEDIA_TYPE_VIDEO)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
STATE_IDLE, STATE_PLAYING, STATE_PAUSED, STATE_OFF, STATE_UNKNOWN)
|
CONF_HOST, DEVICE_DEFAULT_NAME, STATE_IDLE, STATE_PLAYING,
|
||||||
import homeassistant.util as util
|
STATE_PAUSED, STATE_OFF, STATE_UNKNOWN)
|
||||||
|
|
||||||
REQUIREMENTS = ['plexapi==1.1.0']
|
REQUIREMENTS = ['plexapi==1.1.0']
|
||||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||||
@ -31,70 +33,60 @@ SUPPORT_PLEX = SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK
|
|||||||
|
|
||||||
|
|
||||||
# pylint: disable=abstract-method, unused-argument
|
# pylint: disable=abstract-method, unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||||
""" Sets up the plex platform. """
|
""" Sets up the plex platform. """
|
||||||
try:
|
|
||||||
# pylint: disable=unused-variable
|
|
||||||
from plexapi.myplex import MyPlexUser
|
|
||||||
from plexapi.exceptions import BadRequest
|
|
||||||
except ImportError:
|
|
||||||
_LOGGER.exception("Error while importing dependency plexapi.")
|
|
||||||
return
|
|
||||||
|
|
||||||
if discovery_info is not None:
|
if discovery_info is not None:
|
||||||
|
# Parse discovery data
|
||||||
host = urlparse(discovery_info[1]).netloc
|
host = urlparse(discovery_info[1]).netloc
|
||||||
_LOGGER.error('Discovered PLEX server: %s'%host)
|
_LOGGER.info('Discovered PLEX server: %s'%host)
|
||||||
else:
|
else:
|
||||||
# 'name' is currently used for plexserver
|
host = config.get(CONF_HOST, None)
|
||||||
# This indicates old config method
|
|
||||||
host = config.get('name','')
|
|
||||||
|
|
||||||
if host in _CONFIGURING:
|
if host in _CONFIGURING:
|
||||||
return
|
return
|
||||||
|
|
||||||
setup_plexserver(host, config, hass, add_devices)
|
setup_plexserver(host, hass, add_devices_callback)
|
||||||
|
|
||||||
|
|
||||||
def setup_plexserver(host, hass, add_devices):
|
def setup_plexserver(host, hass, add_devices_callback):
|
||||||
''' Setup a plexserver based on host parameter'''
|
''' Setup a plexserver based on host parameter'''
|
||||||
from plexapi.server import PlexServer
|
import plexapi
|
||||||
from plexapi.exceptions import BadRequest
|
|
||||||
|
|
||||||
|
# Config parsing & discovery mix
|
||||||
conf_file = hass.config.path(PLEX_CONFIG_FILE)
|
conf_file = hass.config.path(PLEX_CONFIG_FILE)
|
||||||
|
try:
|
||||||
# Compatability mode. If there's name, user, etc set in
|
with open(conf_file,'r') as f:
|
||||||
# configuration, let's use those, not to break anything
|
conf_dict = eval(f.read())
|
||||||
# We may want to use this method as option when HA's
|
except IOError: # File not found
|
||||||
# configuration options increase
|
if host == None:
|
||||||
if config.get('name', ''):
|
# No discovery, no config, quit here
|
||||||
name = config.get('name', '')
|
|
||||||
user = config.get('user', '')
|
|
||||||
password = config.get('password', '')
|
|
||||||
plexuser = MyPlexUser.signin(user, password)
|
|
||||||
plexserver = plexuser.getResource(name).connect()
|
|
||||||
|
|
||||||
# Discovery mode. Parse config file, attempt conenction
|
|
||||||
# Request configuration on connect fail
|
|
||||||
else:
|
|
||||||
|
|
||||||
print('WEEEEJ, host: %s'%host)
|
|
||||||
try:
|
|
||||||
# Get configuration from config file
|
|
||||||
# FIXME unauthenticated plex servers dont require
|
|
||||||
# a token, so config file isn't mandatory
|
|
||||||
with open(conf_file,'r') as f:
|
|
||||||
conf_dict = eval(f.read())
|
|
||||||
|
|
||||||
plexserver = PlexServer(
|
|
||||||
'http://%s'%host,
|
|
||||||
conf_dict.get(host)['token'])
|
|
||||||
except IOError: # File not found
|
|
||||||
request_configuration(host, hass, add_devices_callback)
|
|
||||||
return
|
return
|
||||||
|
conf_dict = {}
|
||||||
|
|
||||||
except NotFound: # Wrong host was given or need token?
|
if host == None:
|
||||||
_LOGGER.exception("Error connecting to the Hue bridge at %s", host)
|
# Called by module inclusion, let's only use config
|
||||||
return
|
host,token = conf_dict.popitem()
|
||||||
|
token = token['token']
|
||||||
|
elif host not in conf_dict.keys():
|
||||||
|
# Not in config
|
||||||
|
conf_dict[host] = { 'token' : '' }
|
||||||
|
token = None
|
||||||
|
|
||||||
|
_LOGGER.info('Connecting to: htts://%s using token: %s' %
|
||||||
|
(host, token))
|
||||||
|
try:
|
||||||
|
plexserver = plexapi.PlexServer('http://%s'%host, token)
|
||||||
|
except Exception:
|
||||||
|
request_configuration(host, hass, add_devices_callback)
|
||||||
|
return
|
||||||
|
except plexapi.exceptions.BadRequest as e:
|
||||||
|
_LOGGER.error('BLABLA1')
|
||||||
|
request_configuration(host, hass, add_devices_callback)
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.info('Connected to: htts://%s using token: %s' %
|
||||||
|
(host, token))
|
||||||
|
|
||||||
plex_clients = {}
|
plex_clients = {}
|
||||||
plex_sessions = {}
|
plex_sessions = {}
|
||||||
@ -155,14 +147,14 @@ def request_configuration(host, hass, add_devices_callback):
|
|||||||
|
|
||||||
def plex_configuration_callback(data):
|
def plex_configuration_callback(data):
|
||||||
""" Actions to do when our configuration callback is called. """
|
""" Actions to do when our configuration callback is called. """
|
||||||
setup_plexserrver(host, hass, add_devices_callback)
|
setup_plexserver(host, hass, add_devices_callback)
|
||||||
|
|
||||||
_CONFIGURING[host] = configurator.request_config(
|
_CONFIGURING[host] = configurator.request_config(
|
||||||
hass, "Plex Media Server", plex_configuration_callback,
|
hass, "Plex Media Server", plex_configuration_callback,
|
||||||
description=("Enter the X-Plex-Token as descrobed here<BR>"
|
description=('Enter the X-Plex-Token'),
|
||||||
'<A HREF="https://support.plex.tv/hc/en-us/articles/204059436-Finding-your-account-token-X-Plex-Token" TARGET="_new">Plex documentation</A>'),
|
description_image="/static/images/config_plex_mediaserver.png",
|
||||||
description_image="/static/images/config_plexserver.jpg",
|
submit_caption="Confirm",
|
||||||
submit_caption="I have pressed the button"
|
fields=[{'Token':'token'}]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user