Use voluptuous for Fritzbox and DDWRT (#3122)

This commit is contained in:
Johann Kellerman 2016-09-02 06:28:03 +02:00 committed by Teagan Glenn
parent afdd734b44
commit 78f0e681ed
2 changed files with 36 additions and 37 deletions

View File

@ -10,10 +10,11 @@ import threading
from datetime import timedelta from datetime import timedelta
import requests 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.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle from homeassistant.util import Throttle
# Return cached results if last scan was less then this time ago. # 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+)::([^\}]*)\}') _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})') _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 # pylint: disable=unused-argument
def get_scanner(hass, config): def get_scanner(hass, config):
"""Validate the configuration and return a DD-WRT scanner.""" """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]) scanner = DdWrtDeviceScanner(config[DOMAIN])
return scanner if scanner.success_init else None return scanner if scanner.success_init else None
@ -107,7 +109,7 @@ class DdWrtDeviceScanner(object):
return False return False
with self.lock: with self.lock:
_LOGGER.info("Checking ARP") _LOGGER.info('Checking ARP')
url = 'http://{}/Status_Wireless.live.asp'.format(self.host) url = 'http://{}/Status_Wireless.live.asp'.format(self.host)
data = self.get_ddwrt_data(url) data = self.get_ddwrt_data(url)
@ -143,18 +145,18 @@ class DdWrtDeviceScanner(object):
auth=(self.username, self.password), auth=(self.username, self.password),
timeout=4) timeout=4)
except requests.exceptions.Timeout: except requests.exceptions.Timeout:
_LOGGER.exception("Connection to the router timed out") _LOGGER.exception('Connection to the router timed out')
return return
if response.status_code == 200: if response.status_code == 200:
return _parse_ddwrt_response(response.text) return _parse_ddwrt_response(response.text)
elif response.status_code == 401: elif response.status_code == 401:
# Authentication error # Authentication error
_LOGGER.exception( _LOGGER.exception(
"Failed to authenticate, " 'Failed to authenticate, '
"please check your username and password") 'please check your username and password')
return return
else: else:
_LOGGER.error("Invalid response from ddwrt: %s", response) _LOGGER.error('Invalid response from ddwrt: %s', response)
def _parse_ddwrt_response(data_str): def _parse_ddwrt_response(data_str):

View File

@ -7,9 +7,11 @@ https://home-assistant.io/components/device_tracker.fritz/
import logging import logging
from datetime import timedelta 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.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle from homeassistant.util import Throttle
REQUIREMENTS = ['https://github.com/deisi/fritzconnection/archive/' REQUIREMENTS = ['https://github.com/deisi/fritzconnection/archive/'
@ -21,14 +23,17 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
_LOGGER = logging.getLogger(__name__) _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): def get_scanner(hass, config):
"""Validate the configuration and return FritzBoxScanner.""" """Validate the configuration and return FritzBoxScanner."""
if not validate_config(config,
{DOMAIN: []},
_LOGGER):
return None
scanner = FritzBoxScanner(config[DOMAIN]) scanner = FritzBoxScanner(config[DOMAIN])
return scanner if scanner.success_init else None return scanner if scanner.success_init else None
@ -40,22 +45,14 @@ class FritzBoxScanner(object):
def __init__(self, config): def __init__(self, config):
"""Initialize the scanner.""" """Initialize the scanner."""
self.last_results = [] self.last_results = []
self.host = '169.254.1.1' # This IP is valid for all FRITZ!Box router. self.host = config[CONF_HOST]
self.username = 'admin' self.username = config[CONF_USERNAME]
self.password = '' self.password = config[CONF_PASSWORD]
self.success_init = True self.success_init = True
# pylint: disable=import-error # pylint: disable=import-error
import fritzconnection as fc 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. # Establish a connection to the FRITZ!Box.
try: try:
self.fritz_box = fc.FritzHosts(address=self.host, self.fritz_box = fc.FritzHosts(address=self.host,
@ -70,25 +67,25 @@ class FritzBoxScanner(object):
self.success_init = False self.success_init = False
if self.success_init: if self.success_init:
_LOGGER.info("Successfully connected to %s", _LOGGER.info('Successfully connected to %s',
self.fritz_box.modelname) self.fritz_box.modelname)
self._update_info() self._update_info()
else: else:
_LOGGER.error("Failed to establish connection to FRITZ!Box " _LOGGER.error('Failed to establish connection to FRITZ!Box '
"with IP: %s", self.host) 'with IP: %s', self.host)
def scan_devices(self): def scan_devices(self):
"""Scan for new devices and return a list of found device ids.""" """Scan for new devices and return a list of found device ids."""
self._update_info() self._update_info()
active_hosts = [] active_hosts = []
for known_host in self.last_results: for known_host in self.last_results:
if known_host["status"] == "1": if known_host['status'] == '1':
active_hosts.append(known_host["mac"]) active_hosts.append(known_host['mac'])
return active_hosts return active_hosts
def get_device_name(self, mac): def get_device_name(self, mac):
"""Return the name of the given device or None if is not known.""" """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 == {}: if ret == {}:
return None return None
return ret return ret
@ -99,6 +96,6 @@ class FritzBoxScanner(object):
if not self.success_init: if not self.success_init:
return False return False
_LOGGER.info("Scanning") _LOGGER.info('Scanning')
self.last_results = self.fritz_box.get_hosts_info() self.last_results = self.fritz_box.get_hosts_info()
return True return True