From 78f0e681ed9c898eb714de66af9a4e99ba467f8b Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Fri, 2 Sep 2016 06:28:03 +0200 Subject: [PATCH] Use voluptuous for Fritzbox and DDWRT (#3122) --- .../components/device_tracker/ddwrt.py | 26 +++++----- .../components/device_tracker/fritz.py | 47 +++++++++---------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/device_tracker/ddwrt.py b/homeassistant/components/device_tracker/ddwrt.py index 02f49fe7475..4dc6229566c 100644 --- a/homeassistant/components/device_tracker/ddwrt.py +++ b/homeassistant/components/device_tracker/ddwrt.py @@ -10,10 +10,11 @@ import threading from datetime import timedelta import requests +import voluptuous as vol -from homeassistant.components.device_tracker import DOMAIN +import homeassistant.helpers.config_validation as cv +from homeassistant.components.device_tracker import DOMAIN, PLATFORM_SCHEMA from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME -from homeassistant.helpers import validate_config from homeassistant.util import Throttle # Return cached results if last scan was less then this time ago. @@ -24,15 +25,16 @@ _LOGGER = logging.getLogger(__name__) _DDWRT_DATA_REGEX = re.compile(r'\{(\w+)::([^\}]*)\}') _MAC_REGEX = re.compile(r'(([0-9A-Fa-f]{1,2}\:){5}[0-9A-Fa-f]{1,2})') +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_HOST): cv.string, + vol.Required(CONF_PASSWORD): cv.string, + vol.Required(CONF_USERNAME): cv.string +}) + # pylint: disable=unused-argument def get_scanner(hass, config): """Validate the configuration and return a DD-WRT scanner.""" - if not validate_config(config, - {DOMAIN: [CONF_HOST, CONF_USERNAME, CONF_PASSWORD]}, - _LOGGER): - return None - scanner = DdWrtDeviceScanner(config[DOMAIN]) return scanner if scanner.success_init else None @@ -107,7 +109,7 @@ class DdWrtDeviceScanner(object): return False with self.lock: - _LOGGER.info("Checking ARP") + _LOGGER.info('Checking ARP') url = 'http://{}/Status_Wireless.live.asp'.format(self.host) data = self.get_ddwrt_data(url) @@ -143,18 +145,18 @@ class DdWrtDeviceScanner(object): auth=(self.username, self.password), timeout=4) except requests.exceptions.Timeout: - _LOGGER.exception("Connection to the router timed out") + _LOGGER.exception('Connection to the router timed out') return if response.status_code == 200: return _parse_ddwrt_response(response.text) elif response.status_code == 401: # Authentication error _LOGGER.exception( - "Failed to authenticate, " - "please check your username and password") + 'Failed to authenticate, ' + 'please check your username and password') return else: - _LOGGER.error("Invalid response from ddwrt: %s", response) + _LOGGER.error('Invalid response from ddwrt: %s', response) def _parse_ddwrt_response(data_str): diff --git a/homeassistant/components/device_tracker/fritz.py b/homeassistant/components/device_tracker/fritz.py index 8def71cce73..202919871ad 100644 --- a/homeassistant/components/device_tracker/fritz.py +++ b/homeassistant/components/device_tracker/fritz.py @@ -7,9 +7,11 @@ https://home-assistant.io/components/device_tracker.fritz/ import logging from datetime import timedelta -from homeassistant.components.device_tracker import DOMAIN +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.device_tracker import DOMAIN, PLATFORM_SCHEMA from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME -from homeassistant.helpers import validate_config from homeassistant.util import Throttle REQUIREMENTS = ['https://github.com/deisi/fritzconnection/archive/' @@ -21,14 +23,17 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5) _LOGGER = logging.getLogger(__name__) +CONF_DEFAULT_IP = '169.254.1.1' # This IP is valid for all FRITZ!Box routers. + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_HOST, default=CONF_DEFAULT_IP): cv.string, + vol.Optional(CONF_PASSWORD, default='admin'): cv.string, + vol.Optional(CONF_USERNAME, default=''): cv.string +}) + def get_scanner(hass, config): """Validate the configuration and return FritzBoxScanner.""" - if not validate_config(config, - {DOMAIN: []}, - _LOGGER): - return None - scanner = FritzBoxScanner(config[DOMAIN]) return scanner if scanner.success_init else None @@ -40,22 +45,14 @@ class FritzBoxScanner(object): def __init__(self, config): """Initialize the scanner.""" self.last_results = [] - self.host = '169.254.1.1' # This IP is valid for all FRITZ!Box router. - self.username = 'admin' - self.password = '' + self.host = config[CONF_HOST] + self.username = config[CONF_USERNAME] + self.password = config[CONF_PASSWORD] self.success_init = True # pylint: disable=import-error import fritzconnection as fc - # Check for user specific configuration - if CONF_HOST in config.keys(): - self.host = config[CONF_HOST] - if CONF_USERNAME in config.keys(): - self.username = config[CONF_USERNAME] - if CONF_PASSWORD in config.keys(): - self.password = config[CONF_PASSWORD] - # Establish a connection to the FRITZ!Box. try: self.fritz_box = fc.FritzHosts(address=self.host, @@ -70,25 +67,25 @@ class FritzBoxScanner(object): self.success_init = False if self.success_init: - _LOGGER.info("Successfully connected to %s", + _LOGGER.info('Successfully connected to %s', self.fritz_box.modelname) self._update_info() else: - _LOGGER.error("Failed to establish connection to FRITZ!Box " - "with IP: %s", self.host) + _LOGGER.error('Failed to establish connection to FRITZ!Box ' + 'with IP: %s', self.host) def scan_devices(self): """Scan for new devices and return a list of found device ids.""" self._update_info() active_hosts = [] for known_host in self.last_results: - if known_host["status"] == "1": - active_hosts.append(known_host["mac"]) + if known_host['status'] == '1': + active_hosts.append(known_host['mac']) return active_hosts def get_device_name(self, mac): """Return the name of the given device or None if is not known.""" - ret = self.fritz_box.get_specific_host_entry(mac)["NewHostName"] + ret = self.fritz_box.get_specific_host_entry(mac)['NewHostName'] if ret == {}: return None return ret @@ -99,6 +96,6 @@ class FritzBoxScanner(object): if not self.success_init: return False - _LOGGER.info("Scanning") + _LOGGER.info('Scanning') self.last_results = self.fritz_box.get_hosts_info() return True