From 419725e1a9026afe63640426ac14ae5124d91cc1 Mon Sep 17 00:00:00 2001 From: Colby Rome Date: Wed, 10 Oct 2018 02:23:31 -0400 Subject: [PATCH] Add Verizon Fios Quantum Gateway device_tracker platform (#17023) * wrote quantum_gateway.py * ran gen_requirements script * fixed linting errors, added docstrings * update .coveragerc * fixed typo * add myself to contributors * single quotes for single words * added error handling to prevent stacktrace * updated my pypi library * houndci fixes - added RequestException * added password to config schema --- .coveragerc | 1 + CODEOWNERS | 1 + .../device_tracker/quantum_gateway.py | 69 +++++++++++++++++++ requirements_all.txt | 3 + 4 files changed, 74 insertions(+) create mode 100644 homeassistant/components/device_tracker/quantum_gateway.py diff --git a/.coveragerc b/.coveragerc index 459b59f3d39..297c7edb9a7 100644 --- a/.coveragerc +++ b/.coveragerc @@ -483,6 +483,7 @@ omit = homeassistant/components/device_tracker/netgear.py homeassistant/components/device_tracker/nmap_tracker.py homeassistant/components/device_tracker/ping.py + homeassistant/components/device_tracker/quantum_gateway.py homeassistant/components/device_tracker/ritassist.py homeassistant/components/device_tracker/sky_hub.py homeassistant/components/device_tracker/snmp.py diff --git a/CODEOWNERS b/CODEOWNERS index b3ba8ff564d..93fd0418833 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -60,6 +60,7 @@ homeassistant/components/cover/group.py @cdce8p homeassistant/components/cover/template.py @PhracturedBlue homeassistant/components/device_tracker/automatic.py @armills homeassistant/components/device_tracker/huawei_router.py @abmantis +homeassistant/components/device_tracker/quantum_gateway.py @cisasteelersfan homeassistant/components/device_tracker/tile.py @bachya homeassistant/components/history_graph.py @andrey-git homeassistant/components/influx.py @fabaff diff --git a/homeassistant/components/device_tracker/quantum_gateway.py b/homeassistant/components/device_tracker/quantum_gateway.py new file mode 100644 index 00000000000..a06794f9179 --- /dev/null +++ b/homeassistant/components/device_tracker/quantum_gateway.py @@ -0,0 +1,69 @@ +""" +Support for Verizon FiOS Quantum Gateways. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/device_tracker.quantum_gateway/ +""" +import logging + +from requests.exceptions import RequestException +import voluptuous as vol + +from homeassistant.components.device_tracker import (DOMAIN, PLATFORM_SCHEMA, + DeviceScanner) +from homeassistant.const import (CONF_HOST, CONF_PASSWORD) +import homeassistant.helpers.config_validation as cv + +REQUIREMENTS = ['quantum-gateway==0.0.3'] + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_HOST = 'myfiosgateway.com' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, + vol.Required(CONF_PASSWORD): cv.string +}) + + +def get_scanner(hass, config): + """Validate the configuration and return a Quantum Gateway scanner.""" + scanner = QuantumGatewayDeviceScanner(config[DOMAIN]) + + return scanner if scanner.success_init else None + + +class QuantumGatewayDeviceScanner(DeviceScanner): + """This class queries a Quantum Gateway.""" + + def __init__(self, config): + """Initialize the scanner.""" + from quantum_gateway import QuantumGatewayScanner + + self.host = config[CONF_HOST] + self.password = config[CONF_PASSWORD] + _LOGGER.debug('Initializing') + + try: + self.quantum = QuantumGatewayScanner(self.host, self.password) + self.success_init = self.quantum.success_init + except RequestException: + self.success_init = False + _LOGGER.error("Unable to connect to gateway. Check host.") + + if not self.success_init: + _LOGGER.error("Unable to login to gateway. Check password and " + "host.") + + def scan_devices(self): + """Scan for new devices and return a list of found MACs.""" + connected_devices = [] + try: + connected_devices = self.quantum.scan_devices() + except RequestException: + _LOGGER.error("Unable to scan devices. Check connection to router") + return connected_devices + + def get_device_name(self, device): + """Return the name of the given device or None if we don't know.""" + return self.quantum.get_device_name(device) diff --git a/requirements_all.txt b/requirements_all.txt index 68bb64844c4..2ef85e05166 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1264,6 +1264,9 @@ pyzabbix==0.7.4 # homeassistant.components.sensor.qnap qnapstats==0.2.7 +# homeassistant.components.device_tracker.quantum_gateway +quantum-gateway==0.0.3 + # homeassistant.components.rachio rachiopy==0.1.3