From d195fd47f723521e1d8fcc4d38742755c5d85fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Costa?= Date: Thu, 10 Aug 2017 16:01:52 +0100 Subject: [PATCH] Add new device tracker for Huawei Routers. (#8488) * Add new device tracker for Huawei Routers. This was tested with the HG8247H model, used by Vodafone Portugal for the Fiber service. * add to .coveragerc; remove import and space * add comments and fix lint * rename methods * huawei_router: add constants to scanner class * huawei_router: remove lock; use format() in string * huawei_router: use tupple instead of member only class * huawei_router: reduce min scan time * huawei_router: lint * huawei_router: lint * huawei_router: add missing lines in imports * huawei_router: correctly decode string after router firmware update * Remove things that is done on core now --- .coveragerc | 1 + .../device_tracker/huawei_router.py | 145 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 homeassistant/components/device_tracker/huawei_router.py diff --git a/.coveragerc b/.coveragerc index 7cf0a78dc31..56ccb37d169 100644 --- a/.coveragerc +++ b/.coveragerc @@ -272,6 +272,7 @@ omit = homeassistant/components/device_tracker/cisco_ios.py homeassistant/components/device_tracker/fritz.py homeassistant/components/device_tracker/gpslogger.py + homeassistant/components/device_tracker/huawei_router.py homeassistant/components/device_tracker/icloud.py homeassistant/components/device_tracker/linksys_ap.py homeassistant/components/device_tracker/linksys_smart.py diff --git a/homeassistant/components/device_tracker/huawei_router.py b/homeassistant/components/device_tracker/huawei_router.py new file mode 100644 index 00000000000..b78683696cf --- /dev/null +++ b/homeassistant/components/device_tracker/huawei_router.py @@ -0,0 +1,145 @@ +""" +Support for HUAWEI routers. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/device_tracker.huawei/ +""" +import base64 +import logging +import re +from collections import namedtuple + +import requests +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, CONF_PASSWORD, CONF_USERNAME + +_LOGGER = logging.getLogger(__name__) + +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 HUAWEI scanner.""" + scanner = HuaweiDeviceScanner(config[DOMAIN]) + + return scanner + + +Device = namedtuple('Device', ['name', 'ip', 'mac', 'state']) + + +class HuaweiDeviceScanner(DeviceScanner): + """This class queries a router running HUAWEI firmware.""" + + ARRAY_REGEX = re.compile(r'var UserDevinfo = new Array\((.*),null\);') + DEVICE_REGEX = re.compile(r'new USERDevice\((.*?)\),') + DEVICE_ATTR_REGEX = re.compile( + '"(?P.*?)","(?P.*?)",' + '"(?P.*?)","(?P.*?)",' + '"(?P.*?)","(?P.*?)",' + '"(?P.*?)","(?P.*?)",' + '"(?P