From 0e780541953199ff2acf8af0b32d2e2a8cd22c4d Mon Sep 17 00:00:00 2001 From: Colby Rome Date: Tue, 5 Mar 2019 01:57:45 -0500 Subject: [PATCH] Xfinity Gateway device_tracker platform (#21026) * initial commit * updated .coveragerc, CODEOWNERS, generated requirements_all.txt * fixed lines exceeding 79 characters * pylint fixes * shorten docstring and simplify get_scanner * extract initialization into get_scanner * bump pypi version * name change --- .coveragerc | 1 + CODEOWNERS | 3 +- .../components/device_tracker/xfinity.py | 58 +++++++++++++++++++ requirements_all.txt | 3 + 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/device_tracker/xfinity.py diff --git a/.coveragerc b/.coveragerc index ef64886f776..8ccf59dddc9 100644 --- a/.coveragerc +++ b/.coveragerc @@ -138,6 +138,7 @@ omit = homeassistant/components/device_tracker/trackr.py homeassistant/components/device_tracker/ubee.py homeassistant/components/device_tracker/ubus.py + homeassistant/components/device_tracker/xfinity.py homeassistant/components/digital_ocean/* homeassistant/components/dominos/* homeassistant/components/doorbird/* diff --git a/CODEOWNERS b/CODEOWNERS index d15fd85cb52..ed55211a9cf 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -63,12 +63,13 @@ homeassistant/components/cover/group.py @cdce8p homeassistant/components/cover/template.py @PhracturedBlue homeassistant/components/device_tracker/asuswrt.py @kennedyshead homeassistant/components/device_tracker/automatic.py @armills +homeassistant/components/device_tracker/bt_smarthub.py @jxwolstenholme 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/device_tracker/traccar.py @ludeeus -homeassistant/components/device_tracker/bt_smarthub.py @jxwolstenholme homeassistant/components/device_tracker/synology_srm.py @aerialls +homeassistant/components/device_tracker/xfinity.py @cisasteelersfan homeassistant/components/light/lifx_legacy.py @amelchio homeassistant/components/light/yeelight.py @rytilahti homeassistant/components/light/yeelightsunflower.py @lindsaymarkward diff --git a/homeassistant/components/device_tracker/xfinity.py b/homeassistant/components/device_tracker/xfinity.py new file mode 100644 index 00000000000..04702355de7 --- /dev/null +++ b/homeassistant/components/device_tracker/xfinity.py @@ -0,0 +1,58 @@ +"""Support for device tracking via Xfinity Gateways.""" +import logging + +from requests.exceptions import RequestException +import voluptuous as vol + +import homeassistant.helpers.config_validation as cv +from homeassistant.components.device_tracker import ( + DOMAIN, PLATFORM_SCHEMA, DeviceScanner) +from homeassistant.const import CONF_HOST + +REQUIREMENTS = ['xfinity-gateway==0.0.4'] + +_LOGGER = logging.getLogger(__name__) + +DEFAULT_HOST = '10.0.0.1' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string +}) + + +def get_scanner(hass, config): + """Validate the configuration and return an Xfinity Gateway scanner.""" + from xfinity_gateway import XfinityGateway + + gateway = XfinityGateway(config[DOMAIN][CONF_HOST]) + scanner = None + try: + gateway.scan_devices() + scanner = XfinityDeviceScanner(gateway) + except (RequestException, ValueError): + _LOGGER.error("Error communicating with Xfinity Gateway. " + "Check host: %s", gateway.host) + + return scanner + + +class XfinityDeviceScanner(DeviceScanner): + """This class queries an Xfinity Gateway.""" + + def __init__(self, gateway): + """Initialize the scanner.""" + self.gateway = gateway + + def scan_devices(self): + """Scan for new devices and return a list of found MACs.""" + connected_devices = [] + try: + connected_devices = self.gateway.scan_devices() + except (RequestException, ValueError): + _LOGGER.error("Unable to scan devices. " + "Check connection to gateway") + 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.gateway.get_device_name(device) diff --git a/requirements_all.txt b/requirements_all.txt index ad42b78eb78..6ff44fe48b5 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1778,6 +1778,9 @@ xbee-helper==0.0.7 # homeassistant.components.sensor.xbox_live xboxapi==0.1.1 +# homeassistant.components.device_tracker.xfinity +xfinity-gateway==0.0.4 + # homeassistant.components.knx xknx==0.9.4