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.pull/289/head
parent
0b6358e759
commit
f5b98c86f0
|
@ -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()
|
||||
|
|
|
@ -10,6 +10,7 @@ start by calling homeassistant.start_home_assistant(bus)
|
|||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
""" Exceptions used by Home Assistant """
|
||||
|
||||
|
||||
class HomeAssistantError(Exception):
|
||||
""" General Home Assistant exception occured. """
|
||||
pass
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue