flake8,pylint and other code cleanup

This commit is contained in:
Tom Duijf 2015-10-25 17:00:54 +00:00
parent 847d9736aa
commit 5b25d9ccd6
2 changed files with 29 additions and 29 deletions

View File

@ -90,6 +90,7 @@ def setup(hass, config):
ATTR_DISCOVERED: info ATTR_DISCOVERED: info
}) })
# pylint: disable=unused-argument
def start_discovery(event): def start_discovery(event):
""" Start discovering. """ """ Start discovering. """
netdisco = DiscoveryService(SCAN_INTERVAL) netdisco = DiscoveryService(SCAN_INTERVAL)

View File

@ -6,7 +6,9 @@ Provides an interface to the Plex API.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/media_player.plex.html https://home-assistant.io/components/media_player.plex.html
""" """
import logging, json, os import os
import json
import logging
from datetime import timedelta from datetime import timedelta
from urllib.parse import urlparse from urllib.parse import urlparse
@ -16,7 +18,7 @@ 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 (
CONF_HOST, DEVICE_DEFAULT_NAME, STATE_IDLE, STATE_PLAYING, DEVICE_DEFAULT_NAME, STATE_IDLE, STATE_PLAYING,
STATE_PAUSED, STATE_OFF, STATE_UNKNOWN) STATE_PAUSED, STATE_OFF, STATE_UNKNOWN)
REQUIREMENTS = ['plexapi==1.1.0'] REQUIREMENTS = ['plexapi==1.1.0']
@ -31,29 +33,30 @@ _LOGGER = logging.getLogger(__name__)
SUPPORT_PLEX = SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK SUPPORT_PLEX = SUPPORT_PAUSE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK
def config_from_file(filename, config=None): def config_from_file(filename, config=None):
''' Small configuration file management function''' ''' Small configuration file management function'''
if config: if config:
# We're writing configuration # We're writing configuration
try: try:
with open(filename,'w') as f: with open(filename, 'w') as fdesc:
f.write(json.dumps(config)) fdesc.write(json.dumps(config))
except IOError as e: except IOError as error:
_LOGGER.error('Saving config file failed: %s'%e) _LOGGER.error('Saving config file failed: %s', error)
return False return False
return True return True
else: else:
# We're reading config # We're reading config
if os.path.isfile(filename): if os.path.isfile(filename):
try: try:
with open(filename,'r') as f: with open(filename, 'r') as fdesc:
return json.loads(f.read()) return json.loads(fdesc.read())
except IOError as e: except IOError as error:
_LOGGER.error('Reading config file failed: %s'%e) _LOGGER.error('Reading config file failed: %s', error)
return False return False
else: else:
return {} return {}
# pylint: disable=abstract-method, unused-argument # pylint: disable=abstract-method, unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
@ -63,7 +66,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
if discovery_info is not None: if discovery_info is not None:
# Parse discovery data # Parse discovery data
host = urlparse(discovery_info[1]).netloc host = urlparse(discovery_info[1]).netloc
_LOGGER.info('Discovered PLEX server: %s'%host) _LOGGER.info('Discovered PLEX server: %s', host)
if host in _CONFIGURING: if host in _CONFIGURING:
return return
@ -73,7 +76,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# Setup a configured PlexServer # Setup a configured PlexServer
config = config_from_file(hass.config.path(PLEX_CONFIG_FILE)) config = config_from_file(hass.config.path(PLEX_CONFIG_FILE))
if len(config): if len(config):
host,token = config.popitem() host, token = config.popitem()
token = token['token'] token = token['token']
else: else:
# Empty config file? # Empty config file?
@ -82,25 +85,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
setup_plexserver(host, token, hass, add_devices_callback) setup_plexserver(host, token, hass, add_devices_callback)
# pylint: disable=too-many-branches
def setup_plexserver(host, token, hass, add_devices_callback): def setup_plexserver(host, token, hass, add_devices_callback):
''' Setup a plexserver based on host parameter''' ''' Setup a plexserver based on host parameter'''
from plexapi.server import PlexServer
from plexapi.exceptions import BadRequest
import plexapi import plexapi
_LOGGER.info('Connecting to: htts://%s using token: %s' %
(host, token))
try: try:
plexserver = plexapi.server.PlexServer('http://%s'%host, token) plexserver = plexapi.server.PlexServer('http://%s' % host, token)
except (plexapi.exceptions.BadRequest, except (plexapi.exceptions.BadRequest,
plexapi.exceptions.Unauthorized, plexapi.exceptions.Unauthorized) as error:
plexapi.exceptions.NotFound) as e: _LOGGER.info(error)
_LOGGER.info(e)
# No token or wrong token # No token or wrong token
request_configuration(host, hass, add_devices_callback) request_configuration(host, hass, add_devices_callback)
return return
except Exception as e: except plexapi.exceptions.NotFound:
_LOGGER.error('Misc Exception : %s'%e) # Host not found. Maybe it's off. Just log it and stop
_LOGGER.info(error)
return return
# If we came here and configuring this host, mark as done # If we came here and configuring this host, mark as done
@ -116,8 +116,7 @@ def setup_plexserver(host, token, hass, add_devices_callback):
{host: {'token': token}}): {host: {'token': token}}):
_LOGGER.error('failed to save config file') _LOGGER.error('failed to save config file')
_LOGGER.info('Connected to: htts://%s using token: %s' % _LOGGER.info('Connected to: htts://%s', host)
(host, token))
plex_clients = {} plex_clients = {}
plex_sessions = {} plex_sessions = {}
@ -185,14 +184,14 @@ def request_configuration(host, hass, add_devices_callback):
description=('Enter the X-Plex-Token'), description=('Enter the X-Plex-Token'),
description_image="/static/images/config_plex_mediaserver.png", description_image="/static/images/config_plex_mediaserver.png",
submit_caption="Confirm", submit_caption="Confirm",
fields=[{'id': 'token', 'name':'X-Plex-Token', 'type':''}] fields=[{'id': 'token', 'name': 'X-Plex-Token', 'type': ''}]
) )
class PlexClient(MediaPlayerDevice): class PlexClient(MediaPlayerDevice):
""" Represents a Plex device. """ """ Represents a Plex device. """
# pylint: disable=too-many-public-methods # pylint: disable=too-many-public-methods, attribute-defined-outside-init
def __init__(self, device, plex_sessions, update_devices, update_sessions): def __init__(self, device, plex_sessions, update_devices, update_sessions):
self.plex_sessions = plex_sessions self.plex_sessions = plex_sessions
self.update_devices = update_devices self.update_devices = update_devices
@ -207,12 +206,12 @@ class PlexClient(MediaPlayerDevice):
def unique_id(self): def unique_id(self):
""" Returns the id of this plex client """ """ Returns the id of this plex client """
return "{}.{}".format( return "{}.{}".format(
self.__class__, self.device.machineIdentifier or self.device.name ) self.__class__, self.device.machineIdentifier or self.device.name)
@property @property
def name(self): def name(self):
""" Returns the name of the device. """ """ Returns the name of the device. """
return self.device.name or self.device.product or self.device.deviceClass return self.device.name or DEVICE_DEFAULT_NAME
@property @property
def session(self): def session(self):