2015-08-18 20:12:01 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.device_tracker.actiontec
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-08-18 20:50:40 +00:00
|
|
|
Device tracker platform that supports scanning an Actiontec MI424WR
|
|
|
|
(Verizon FIOS) router for device presence.
|
2015-08-18 20:12:01 +00:00
|
|
|
|
|
|
|
This device tracker needs telnet to be enabled on the router.
|
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
2015-08-18 21:14:26 +00:00
|
|
|
To use the Actiontec tracker you will need to add something like the
|
2015-09-01 18:43:14 +00:00
|
|
|
following to your config/configuration.yaml. If you experience disconnects
|
|
|
|
you can modify the home_interval variable.
|
|
|
|
|
2015-08-18 20:12:01 +00:00
|
|
|
|
|
|
|
device_tracker:
|
|
|
|
platform: actiontec
|
|
|
|
host: YOUR_ROUTER_IP
|
|
|
|
username: YOUR_ADMIN_USERNAME
|
|
|
|
password: YOUR_ADMIN_PASSWORD
|
2015-09-01 18:43:14 +00:00
|
|
|
# optional:
|
|
|
|
home_interval: 10
|
2015-08-18 20:12:01 +00:00
|
|
|
|
|
|
|
Variables:
|
|
|
|
|
|
|
|
host
|
|
|
|
*Required
|
|
|
|
The IP address of your router, e.g. 192.168.1.1.
|
|
|
|
|
|
|
|
username
|
|
|
|
*Required
|
|
|
|
The username of an user with administrative privileges, usually 'admin'.
|
|
|
|
|
|
|
|
password
|
|
|
|
*Required
|
|
|
|
The password for your given admin account.
|
2015-09-01 18:43:14 +00:00
|
|
|
|
|
|
|
home_interval
|
|
|
|
*Optional
|
|
|
|
Number of minutes it will not scan devices that it found in previous results.
|
2015-08-18 20:12:01 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
2015-09-01 18:43:14 +00:00
|
|
|
from collections import namedtuple
|
2015-08-18 20:12:01 +00:00
|
|
|
import re
|
|
|
|
import threading
|
|
|
|
import telnetlib
|
|
|
|
|
2015-09-01 18:43:14 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2015-08-18 20:12:01 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
|
|
|
|
from homeassistant.helpers import validate_config
|
2015-09-01 18:43:14 +00:00
|
|
|
from homeassistant.util import Throttle, convert
|
2015-08-18 20:12:01 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN
|
|
|
|
|
|
|
|
# Return cached results if last scan was less then this time ago
|
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=5)
|
|
|
|
|
2015-09-01 18:43:14 +00:00
|
|
|
# interval in minutes to exclude devices from a scan while they are home
|
|
|
|
CONF_HOME_INTERVAL = "home_interval"
|
|
|
|
|
2015-08-18 20:12:01 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-08-19 13:52:47 +00:00
|
|
|
_LEASES_REGEX = re.compile(
|
|
|
|
r'(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})' +
|
|
|
|
r'\smac:\s(?P<mac>([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))')
|
2015-08-18 20:12:01 +00:00
|
|
|
|
2015-08-18 21:03:13 +00:00
|
|
|
|
2015-08-18 20:12:01 +00:00
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def get_scanner(hass, config):
|
|
|
|
""" Validates config and returns a DD-WRT scanner. """
|
|
|
|
if not validate_config(config,
|
|
|
|
{DOMAIN: [CONF_HOST, CONF_USERNAME, CONF_PASSWORD]},
|
|
|
|
_LOGGER):
|
|
|
|
return None
|
|
|
|
|
|
|
|
scanner = ActiontecDeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
2015-09-01 18:43:14 +00:00
|
|
|
Device = namedtuple("Device", ["mac", "ip", "last_update"])
|
|
|
|
|
2015-08-18 21:03:13 +00:00
|
|
|
|
2015-08-18 20:12:01 +00:00
|
|
|
class ActiontecDeviceScanner(object):
|
|
|
|
""" This class queries a an actiontec router
|
|
|
|
for connected devices. Adapted from DD-WRT scanner.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, config):
|
|
|
|
self.host = config[CONF_HOST]
|
|
|
|
self.username = config[CONF_USERNAME]
|
|
|
|
self.password = config[CONF_PASSWORD]
|
2015-09-01 18:43:14 +00:00
|
|
|
minutes = convert(config.get(CONF_HOME_INTERVAL), int, 0)
|
|
|
|
self.home_interval = timedelta(minutes=minutes)
|
2015-08-18 20:12:01 +00:00
|
|
|
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
|
|
|
|
self.last_results = {}
|
|
|
|
|
|
|
|
# Test the router is accessible
|
|
|
|
data = self.get_actiontec_data()
|
|
|
|
self.success_init = data is not None
|
2015-09-01 18:43:14 +00:00
|
|
|
_LOGGER.info("actiontec scanner initialized")
|
|
|
|
if self.home_interval:
|
2015-09-01 19:09:41 +00:00
|
|
|
_LOGGER.info("home_interval set to: %s", self.home_interval)
|
2015-08-18 20:12:01 +00:00
|
|
|
|
|
|
|
def scan_devices(self):
|
|
|
|
""" Scans for new devices and return a
|
|
|
|
list containing found device ids. """
|
|
|
|
|
|
|
|
self._update_info()
|
2015-09-01 18:43:14 +00:00
|
|
|
return [client.mac for client in self.last_results]
|
2015-08-18 20:12:01 +00:00
|
|
|
|
|
|
|
def get_device_name(self, device):
|
|
|
|
""" Returns the name of the given device or None if we don't know. """
|
|
|
|
if not self.last_results:
|
|
|
|
return None
|
|
|
|
for client in self.last_results:
|
2015-09-01 18:43:14 +00:00
|
|
|
if client.mac == device:
|
|
|
|
return client.ip
|
2015-08-18 20:12:01 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_SCANS)
|
|
|
|
def _update_info(self):
|
2015-08-19 13:52:47 +00:00
|
|
|
""" Ensures the information from the Actiontec MI424WR router is up
|
|
|
|
to date. Returns boolean if scanning successful. """
|
2015-09-01 18:43:14 +00:00
|
|
|
_LOGGER.info("Scanning")
|
2015-08-18 20:12:01 +00:00
|
|
|
if not self.success_init:
|
|
|
|
return False
|
|
|
|
|
|
|
|
with self.lock:
|
2015-09-01 18:43:14 +00:00
|
|
|
exclude_targets = set()
|
|
|
|
target_list = []
|
|
|
|
self.last_results = []
|
|
|
|
now = dt_util.now()
|
|
|
|
if self.home_interval:
|
|
|
|
for host in self.last_results:
|
|
|
|
if host.last_update + self.home_interval > now:
|
|
|
|
exclude_targets.add(host)
|
|
|
|
if len(exclude_targets) > 0:
|
|
|
|
target_list = [t.ip for t in exclude_targets]
|
|
|
|
|
|
|
|
devices = self.get_actiontec_data()
|
|
|
|
if not devices:
|
2015-08-18 20:12:01 +00:00
|
|
|
return False
|
2015-09-01 19:09:41 +00:00
|
|
|
for client in target_list:
|
|
|
|
if client in devices:
|
|
|
|
devices.pop(client)
|
|
|
|
for name, data in devices.items():
|
|
|
|
device = Device(data['mac'], name, now)
|
2015-09-01 18:43:14 +00:00
|
|
|
self.last_results.append(device)
|
|
|
|
|
2015-08-18 20:12:01 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def get_actiontec_data(self):
|
2015-08-19 13:52:47 +00:00
|
|
|
""" Retrieve data from Actiontec MI424WR and return parsed result. """
|
2015-08-18 20:12:01 +00:00
|
|
|
try:
|
|
|
|
telnet = telnetlib.Telnet(self.host)
|
|
|
|
telnet.read_until(b'Username: ')
|
|
|
|
telnet.write((self.username + '\n').encode('ascii'))
|
|
|
|
telnet.read_until(b'Password: ')
|
|
|
|
telnet.write((self.password + '\n').encode('ascii'))
|
2015-08-25 14:09:47 +00:00
|
|
|
prompt = telnet.read_until(
|
|
|
|
b'Wireless Broadband Router> ').split(b'\n')[-1]
|
2015-08-18 20:12:01 +00:00
|
|
|
telnet.write('firewall mac_cache_dump\n'.encode('ascii'))
|
|
|
|
telnet.write('\n'.encode('ascii'))
|
2015-08-18 21:03:13 +00:00
|
|
|
telnet.read_until(prompt)
|
2015-08-18 20:50:40 +00:00
|
|
|
leases_result = telnet.read_until(prompt).split(b'\n')[1:-1]
|
2015-08-18 20:12:01 +00:00
|
|
|
telnet.write('exit\n'.encode('ascii'))
|
|
|
|
except EOFError:
|
|
|
|
_LOGGER.exception("Unexpected response from router")
|
|
|
|
return
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
_LOGGER.exception("Connection refused by router," +
|
|
|
|
" is telnet enabled?")
|
2015-08-25 13:39:00 +00:00
|
|
|
return None
|
2015-08-18 20:12:01 +00:00
|
|
|
|
2015-08-19 13:52:47 +00:00
|
|
|
devices = {}
|
2015-08-18 20:12:01 +00:00
|
|
|
for lease in leases_result:
|
|
|
|
match = _LEASES_REGEX.search(lease.decode('utf-8'))
|
2015-08-18 21:14:26 +00:00
|
|
|
if match is not None:
|
2015-08-19 13:52:47 +00:00
|
|
|
devices[match.group('ip')] = {
|
|
|
|
'ip': match.group('ip'),
|
|
|
|
'mac': match.group('mac').upper()
|
|
|
|
}
|
2015-08-18 20:41:03 +00:00
|
|
|
return devices
|