From f5b98c86f0d87f3c4261aaf586067b79ed9df936 Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Sat, 29 Aug 2015 22:34:35 -0400 Subject: [PATCH] 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. --- homeassistant/__main__.py | 3 +- homeassistant/bootstrap.py | 1 + .../components/device_tracker/nmap_tracker.py | 12 +++++-- homeassistant/components/mqtt.py | 2 +- homeassistant/core.py | 3 +- homeassistant/exceptions.py | 1 + homeassistant/helpers/entity.py | 2 +- homeassistant/remote.py | 31 ++++++++++--------- 8 files changed, 32 insertions(+), 23 deletions(-) diff --git a/homeassistant/__main__.py b/homeassistant/__main__.py index 02ef2c401f3..c6f0fd97b98 100644 --- a/homeassistant/__main__.py +++ b/homeassistant/__main__.py @@ -108,10 +108,9 @@ def main(): def open_browser(event): """ Open the webinterface in a browser. """ if hass.config.api is not None: - from homeassistant.const import EVENT_HOMEASSISTANT_START import webbrowser webbrowser.open(hass.config.api.base_url) - + from homeassistant.const import EVENT_HOMEASSISTANT_START hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 61685dbf2fe..e9f04d9ab71 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -10,6 +10,7 @@ start by calling homeassistant.start_home_assistant(bus) """ import os +import sys import logging from collections import defaultdict diff --git a/homeassistant/components/device_tracker/nmap_tracker.py b/homeassistant/components/device_tracker/nmap_tracker.py index dc12a53f539..ee1650594ee 100644 --- a/homeassistant/components/device_tracker/nmap_tracker.py +++ b/homeassistant/components/device_tracker/nmap_tracker.py @@ -26,8 +26,12 @@ from collections import namedtuple import subprocess import re -from libnmap.process import NmapProcess -from libnmap.parser import NmapParser, NmapParserException +try: + 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 from homeassistant.const import CONF_HOSTS @@ -52,6 +56,10 @@ def get_scanner(hass, config): _LOGGER): return None + if not LIB_LOADED: + _LOGGER.error("Error while importing dependency python-libnmap.") + return False + scanner = NmapDeviceScanner(config[DOMAIN]) return scanner if scanner.success_init else None diff --git a/homeassistant/components/mqtt.py b/homeassistant/components/mqtt.py index 073406e9700..474b5ebb53e 100644 --- a/homeassistant/components/mqtt.py +++ b/homeassistant/components/mqtt.py @@ -46,7 +46,7 @@ The keep alive in seconds for this client. Default is 60. import logging import socket -from homeassistant.core import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError import homeassistant.util as util from homeassistant.helpers import validate_config from homeassistant.const import ( diff --git a/homeassistant/core.py b/homeassistant/core.py index 0ad05694d65..c04e9a9ab63 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -7,7 +7,6 @@ of entities and react to changes. """ import os -import sys import time import logging import threading @@ -23,7 +22,7 @@ from homeassistant.const import ( EVENT_SERVICE_EXECUTED, ATTR_SERVICE_CALL_ID, EVENT_SERVICE_REGISTERED, TEMP_CELCIUS, TEMP_FAHRENHEIT, ATTR_FRIENDLY_NAME) from homeassistant.exceptions import ( - HomeAssistantError, InvalidEntityFormatError, NoEntitySpecifiedError) + HomeAssistantError, InvalidEntityFormatError) import homeassistant.util as util import homeassistant.util.dt as date_util import homeassistant.helpers.temperature as temp_helper diff --git a/homeassistant/exceptions.py b/homeassistant/exceptions.py index 4ecd22f9e43..bd32d356670 100644 --- a/homeassistant/exceptions.py +++ b/homeassistant/exceptions.py @@ -1,5 +1,6 @@ """ Exceptions used by Home Assistant """ + class HomeAssistantError(Exception): """ General Home Assistant exception occured. """ pass diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index 0ca63856c27..b29379049d3 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -7,7 +7,7 @@ Provides ABC for entities in HA. from collections import defaultdict -from homeassistant.core import NoEntitySpecifiedError +from homeassistant.exceptions import NoEntitySpecifiedError from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_HIDDEN, diff --git a/homeassistant/remote.py b/homeassistant/remote.py index 2488f0a9c46..2193ede86e7 100644 --- a/homeassistant/remote.py +++ b/homeassistant/remote.py @@ -18,6 +18,7 @@ import urllib.parse import requests import homeassistant.core as ha +from homeassistant.exceptions import HomeAssistantError import homeassistant.bootstrap as bootstrap from homeassistant.const import ( @@ -84,12 +85,12 @@ class API(object): except requests.exceptions.ConnectionError: _LOGGER.exception("Error connecting to server") - raise ha.HomeAssistantError("Error connecting to server") + raise HomeAssistantError("Error connecting to server") except requests.exceptions.Timeout: error = "Timeout when talking to {}".format(self.host) _LOGGER.exception(error) - raise ha.HomeAssistantError(error) + raise HomeAssistantError(error) def __repr__(self): return "API({}, {}, {})".format( @@ -102,7 +103,7 @@ class HomeAssistant(ha.HomeAssistant): def __init__(self, remote_api, local_api=None): if not remote_api.validate_api(): - raise ha.HomeAssistantError( + raise HomeAssistantError( "Remote API at {}:{} not valid: {}".format( 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 if self.config.api is None: if not bootstrap.setup_component(self, 'api'): - raise ha.HomeAssistantError( + raise HomeAssistantError( 'Unable to setup local API to receive events') ha.create_timer(self) @@ -132,7 +133,7 @@ class HomeAssistant(ha.HomeAssistant): # Setup that events from remote_api get forwarded to local_api # Do this after we fire START, otherwise HTTP is not started if not connect_remote_events(self.remote_api, self.config.api): - raise ha.HomeAssistantError(( + raise HomeAssistantError(( 'Could not setup event forwarding from api {} to ' 'local api {}').format(self.remote_api, self.config.api)) @@ -293,7 +294,7 @@ def validate_api(api): else: return APIStatus.UNKNOWN - except ha.HomeAssistantError: + except HomeAssistantError: return APIStatus.CANNOT_CONNECT @@ -318,7 +319,7 @@ def connect_remote_events(from_api, to_api): return False - except ha.HomeAssistantError: + except HomeAssistantError: _LOGGER.exception("Error setting up event forwarding") return False @@ -342,7 +343,7 @@ def disconnect_remote_events(from_api, to_api): return False - except ha.HomeAssistantError: + except HomeAssistantError: _LOGGER.exception("Error removing an event forwarder") return False @@ -354,7 +355,7 @@ def get_event_listeners(api): 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 _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", req.status_code, req.text) - except ha.HomeAssistantError: + except HomeAssistantError: _LOGGER.exception("Error firing event") @@ -387,7 +388,7 @@ def get_state(api, entity_id): return ha.State.from_dict(req.json()) \ if req.status_code == 200 else None - except (ha.HomeAssistantError, ValueError): + except (HomeAssistantError, ValueError): # ValueError if req.json() can't parse the json _LOGGER.exception("Error fetching state") @@ -404,7 +405,7 @@ def get_states(api): return [ha.State.from_dict(item) for item in req.json()] - except (ha.HomeAssistantError, ValueError, AttributeError): + except (HomeAssistantError, ValueError, AttributeError): # ValueError if req.json() can't parse the json _LOGGER.exception("Error fetching states") @@ -434,7 +435,7 @@ def set_state(api, entity_id, new_state, attributes=None): else: return True - except ha.HomeAssistantError: + except HomeAssistantError: _LOGGER.exception("Error setting state") return False @@ -457,7 +458,7 @@ def get_services(api): 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 _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", req.status_code, req.text) - except ha.HomeAssistantError: + except HomeAssistantError: _LOGGER.exception("Error calling service")