Mostly PyLint and Flake8 updates.

Rewrote imports of exceptions to be from the exceptions module.
Made nmap scanner check for libnmap dependency without crashing.
Various flake8 and pylint updates.
This commit is contained in:
Ryan Kraus 2015-08-29 22:34:35 -04:00
parent 0b6358e759
commit f5b98c86f0
8 changed files with 32 additions and 23 deletions

View file

@ -108,10 +108,9 @@ def main():
def open_browser(event): def open_browser(event):
""" Open the webinterface in a browser. """ """ Open the webinterface in a browser. """
if hass.config.api is not None: if hass.config.api is not None:
from homeassistant.const import EVENT_HOMEASSISTANT_START
import webbrowser import webbrowser
webbrowser.open(hass.config.api.base_url) webbrowser.open(hass.config.api.base_url)
from homeassistant.const import EVENT_HOMEASSISTANT_START
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser)
hass.start() hass.start()

View file

@ -10,6 +10,7 @@ start by calling homeassistant.start_home_assistant(bus)
""" """
import os import os
import sys
import logging import logging
from collections import defaultdict from collections import defaultdict

View file

@ -26,8 +26,12 @@ from collections import namedtuple
import subprocess import subprocess
import re import re
from libnmap.process import NmapProcess try:
from libnmap.parser import NmapParser, NmapParserException from libnmap.process import NmapProcess
from libnmap.parser import NmapParser, NmapParserException
LIB_LOADED = True
except ImportError:
LIB_LOADED = False
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.const import CONF_HOSTS from homeassistant.const import CONF_HOSTS
@ -52,6 +56,10 @@ def get_scanner(hass, config):
_LOGGER): _LOGGER):
return None return None
if not LIB_LOADED:
_LOGGER.error("Error while importing dependency python-libnmap.")
return False
scanner = NmapDeviceScanner(config[DOMAIN]) scanner = NmapDeviceScanner(config[DOMAIN])
return scanner if scanner.success_init else None return scanner if scanner.success_init else None

View file

@ -46,7 +46,7 @@ The keep alive in seconds for this client. Default is 60.
import logging import logging
import socket import socket
from homeassistant.core import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
import homeassistant.util as util import homeassistant.util as util
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.const import ( from homeassistant.const import (

View file

@ -7,7 +7,6 @@ of entities and react to changes.
""" """
import os import os
import sys
import time import time
import logging import logging
import threading import threading
@ -23,7 +22,7 @@ from homeassistant.const import (
EVENT_SERVICE_EXECUTED, ATTR_SERVICE_CALL_ID, EVENT_SERVICE_REGISTERED, EVENT_SERVICE_EXECUTED, ATTR_SERVICE_CALL_ID, EVENT_SERVICE_REGISTERED,
TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME) TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME)
from homeassistant.exceptions import ( from homeassistant.exceptions import (
HomeAssistantError, InvalidEntityFormatError, NoEntitySpecifiedError) HomeAssistantError, InvalidEntityFormatError)
import homeassistant.util as util import homeassistant.util as util
import homeassistant.util.dt as date_util import homeassistant.util.dt as date_util
import homeassistant.helpers.temperature as temp_helper import homeassistant.helpers.temperature as temp_helper

View file

@ -1,5 +1,6 @@
""" Exceptions used by Home Assistant """ """ Exceptions used by Home Assistant """
class HomeAssistantError(Exception): class HomeAssistantError(Exception):
""" General Home Assistant exception occured. """ """ General Home Assistant exception occured. """
pass pass

View file

@ -7,7 +7,7 @@ Provides ABC for entities in HA.
from collections import defaultdict from collections import defaultdict
from homeassistant.core import NoEntitySpecifiedError from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.const import ( from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN, ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN,

View file

@ -18,6 +18,7 @@ import urllib.parse
import requests import requests
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.exceptions import HomeAssistantError
import homeassistant.bootstrap as bootstrap import homeassistant.bootstrap as bootstrap
from homeassistant.const import ( from homeassistant.const import (
@ -84,12 +85,12 @@ class API(object):
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
_LOGGER.exception("Error connecting to server") _LOGGER.exception("Error connecting to server")
raise ha.HomeAssistantError("Error connecting to server") raise HomeAssistantError("Error connecting to server")
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
error = "Timeout when talking to {}".format(self.host) error = "Timeout when talking to {}".format(self.host)
_LOGGER.exception(error) _LOGGER.exception(error)
raise ha.HomeAssistantError(error) raise HomeAssistantError(error)
def __repr__(self): def __repr__(self):
return "API({}, {}, {})".format( return "API({}, {}, {})".format(
@ -102,7 +103,7 @@ class HomeAssistant(ha.HomeAssistant):
def __init__(self, remote_api, local_api=None): def __init__(self, remote_api, local_api=None):
if not remote_api.validate_api(): if not remote_api.validate_api():
raise ha.HomeAssistantError( raise HomeAssistantError(
"Remote API at {}:{} not valid: {}".format( "Remote API at {}:{} not valid: {}".format(
remote_api.host, remote_api.port, remote_api.status)) remote_api.host, remote_api.port, remote_api.status))
@ -121,7 +122,7 @@ class HomeAssistant(ha.HomeAssistant):
# Ensure a local API exists to connect with remote # Ensure a local API exists to connect with remote
if self.config.api is None: if self.config.api is None:
if not bootstrap.setup_component(self, 'api'): if not bootstrap.setup_component(self, 'api'):
raise ha.HomeAssistantError( raise HomeAssistantError(
'Unable to setup local API to receive events') 'Unable to setup local API to receive events')
ha.create_timer(self) ha.create_timer(self)
@ -132,7 +133,7 @@ class HomeAssistant(ha.HomeAssistant):
# Setup that events from remote_api get forwarded to local_api # Setup that events from remote_api get forwarded to local_api
# Do this after we fire START, otherwise HTTP is not started # Do this after we fire START, otherwise HTTP is not started
if not connect_remote_events(self.remote_api, self.config.api): if not connect_remote_events(self.remote_api, self.config.api):
raise ha.HomeAssistantError(( raise HomeAssistantError((
'Could not setup event forwarding from api {} to ' 'Could not setup event forwarding from api {} to '
'local api {}').format(self.remote_api, self.config.api)) 'local api {}').format(self.remote_api, self.config.api))
@ -293,7 +294,7 @@ def validate_api(api):
else: else:
return APIStatus.UNKNOWN return APIStatus.UNKNOWN
except ha.HomeAssistantError: except HomeAssistantError:
return APIStatus.CANNOT_CONNECT return APIStatus.CANNOT_CONNECT
@ -318,7 +319,7 @@ def connect_remote_events(from_api, to_api):
return False return False
except ha.HomeAssistantError: except HomeAssistantError:
_LOGGER.exception("Error setting up event forwarding") _LOGGER.exception("Error setting up event forwarding")
return False return False
@ -342,7 +343,7 @@ def disconnect_remote_events(from_api, to_api):
return False return False
except ha.HomeAssistantError: except HomeAssistantError:
_LOGGER.exception("Error removing an event forwarder") _LOGGER.exception("Error removing an event forwarder")
return False return False
@ -354,7 +355,7 @@ def get_event_listeners(api):
return req.json() if req.status_code == 200 else {} return req.json() if req.status_code == 200 else {}
except (ha.HomeAssistantError, ValueError): except (HomeAssistantError, ValueError):
# ValueError if req.json() can't parse the json # ValueError if req.json() can't parse the json
_LOGGER.exception("Unexpected result retrieving event listeners") _LOGGER.exception("Unexpected result retrieving event listeners")
@ -371,7 +372,7 @@ def fire_event(api, event_type, data=None):
_LOGGER.error("Error firing event: %d - %d", _LOGGER.error("Error firing event: %d - %d",
req.status_code, req.text) req.status_code, req.text)
except ha.HomeAssistantError: except HomeAssistantError:
_LOGGER.exception("Error firing event") _LOGGER.exception("Error firing event")
@ -387,7 +388,7 @@ def get_state(api, entity_id):
return ha.State.from_dict(req.json()) \ return ha.State.from_dict(req.json()) \
if req.status_code == 200 else None if req.status_code == 200 else None
except (ha.HomeAssistantError, ValueError): except (HomeAssistantError, ValueError):
# ValueError if req.json() can't parse the json # ValueError if req.json() can't parse the json
_LOGGER.exception("Error fetching state") _LOGGER.exception("Error fetching state")
@ -404,7 +405,7 @@ def get_states(api):
return [ha.State.from_dict(item) for return [ha.State.from_dict(item) for
item in req.json()] item in req.json()]
except (ha.HomeAssistantError, ValueError, AttributeError): except (HomeAssistantError, ValueError, AttributeError):
# ValueError if req.json() can't parse the json # ValueError if req.json() can't parse the json
_LOGGER.exception("Error fetching states") _LOGGER.exception("Error fetching states")
@ -434,7 +435,7 @@ def set_state(api, entity_id, new_state, attributes=None):
else: else:
return True return True
except ha.HomeAssistantError: except HomeAssistantError:
_LOGGER.exception("Error setting state") _LOGGER.exception("Error setting state")
return False return False
@ -457,7 +458,7 @@ def get_services(api):
return req.json() if req.status_code == 200 else {} return req.json() if req.status_code == 200 else {}
except (ha.HomeAssistantError, ValueError): except (HomeAssistantError, ValueError):
# ValueError if req.json() can't parse the json # ValueError if req.json() can't parse the json
_LOGGER.exception("Got unexpected services result") _LOGGER.exception("Got unexpected services result")
@ -475,5 +476,5 @@ def call_service(api, domain, service, service_data=None):
_LOGGER.error("Error calling service: %d - %s", _LOGGER.error("Error calling service: %d - %s",
req.status_code, req.text) req.status_code, req.text)
except ha.HomeAssistantError: except HomeAssistantError:
_LOGGER.exception("Error calling service") _LOGGER.exception("Error calling service")