2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Zyxel Keenetic NDMS2 based routers."""
|
2017-09-16 08:29:24 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-04 10:17:20 +00:00
|
|
|
from ndms2_client import Client, ConnectionException, TelnetConnection
|
2017-09-16 08:29:24 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.device_tracker import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DeviceScanner,
|
2017-09-16 08:29:24 +00:00
|
|
|
)
|
2019-12-04 10:17:20 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-09-16 08:29:24 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# Interface name to track devices for. Most likely one will not need to
|
|
|
|
# change it from default 'Home'. This is needed not to track Guest WI-FI-
|
|
|
|
# clients and router itself
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_INTERFACE = "interface"
|
2017-09-16 08:29:24 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_INTERFACE = "Home"
|
2018-07-31 09:14:49 +00:00
|
|
|
DEFAULT_PORT = 23
|
2017-09-16 08:29:24 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_INTERFACE, default=DEFAULT_INTERFACE): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-09-16 08:29:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_scanner(_hass, config):
|
2020-05-29 06:00:20 +00:00
|
|
|
"""Validate the configuration and return a Keenetic NDMS2 scanner."""
|
2017-09-16 08:29:24 +00:00
|
|
|
scanner = KeeneticNDMS2DeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
|
|
|
class KeeneticNDMS2DeviceScanner(DeviceScanner):
|
|
|
|
"""This class scans for devices using keenetic NDMS2 web interface."""
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
"""Initialize the scanner."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-09-16 08:29:24 +00:00
|
|
|
self.last_results = []
|
|
|
|
|
|
|
|
self._interface = config[CONF_INTERFACE]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._client = Client(
|
|
|
|
TelnetConnection(
|
|
|
|
config.get(CONF_HOST),
|
|
|
|
config.get(CONF_PORT),
|
|
|
|
config.get(CONF_USERNAME),
|
|
|
|
config.get(CONF_PASSWORD),
|
|
|
|
)
|
|
|
|
)
|
2017-09-16 08:29:24 +00:00
|
|
|
|
|
|
|
self.success_init = self._update_info()
|
|
|
|
_LOGGER.info("Scanner initialized")
|
|
|
|
|
|
|
|
def scan_devices(self):
|
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
|
|
|
self._update_info()
|
|
|
|
|
|
|
|
return [device.mac for device in self.last_results]
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def get_device_name(self, device):
|
2017-09-16 08:29:24 +00:00
|
|
|
"""Return the name of the given device or None if we don't know."""
|
2019-07-31 19:25:30 +00:00
|
|
|
name = next(
|
|
|
|
(result.name for result in self.last_results if result.mac == device), None
|
|
|
|
)
|
2018-07-31 09:14:49 +00:00
|
|
|
return name
|
|
|
|
|
|
|
|
def get_extra_attributes(self, device):
|
|
|
|
"""Return the IP of the given device."""
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = next(
|
|
|
|
({"ip": result.ip} for result in self.last_results if result.mac == device),
|
|
|
|
{},
|
|
|
|
)
|
2018-07-31 09:14:49 +00:00
|
|
|
return attributes
|
2017-09-16 08:29:24 +00:00
|
|
|
|
|
|
|
def _update_info(self):
|
|
|
|
"""Get ARP from keenetic router."""
|
2018-07-31 09:14:49 +00:00
|
|
|
_LOGGER.debug("Fetching devices from router...")
|
2017-09-16 08:29:24 +00:00
|
|
|
|
|
|
|
try:
|
2018-07-31 09:14:49 +00:00
|
|
|
self.last_results = [
|
|
|
|
dev
|
|
|
|
for dev in self._client.get_devices()
|
|
|
|
if dev.interface == self._interface
|
|
|
|
]
|
|
|
|
_LOGGER.debug("Successfully fetched data from router")
|
|
|
|
return True
|
|
|
|
|
|
|
|
except ConnectionException:
|
|
|
|
_LOGGER.error("Error fetching data from router")
|
2017-09-16 08:29:24 +00:00
|
|
|
return False
|