core/homeassistant/components/ddwrt/device_tracker.py

158 lines
5.2 KiB
Python
Raw Normal View History

2015-05-11 16:05:35 +00:00
"""
2016-03-07 17:12:06 +00:00
Support for DD-WRT routers.
2015-05-11 16:05:35 +00:00
2015-10-13 18:49:28 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/device_tracker.ddwrt/
2015-05-11 16:05:35 +00:00
"""
import logging
import re
2016-02-19 05:27:50 +00:00
import requests
import voluptuous as vol
from homeassistant.components.device_tracker import (
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_SSL, CONF_USERNAME, CONF_VERIFY_SSL)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
_DDWRT_DATA_REGEX = re.compile(r'\{(\w+)::([^\}]*)\}')
_MAC_REGEX = re.compile(r'(([0-9A-Fa-f]{1,2}\:){5}[0-9A-Fa-f]{1,2})')
DEFAULT_SSL = False
DEFAULT_VERIFY_SSL = True
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
})
2015-03-29 00:49:07 +00:00
def get_scanner(hass, config):
2016-03-07 20:18:53 +00:00
"""Validate the configuration and return a DD-WRT scanner."""
try:
return DdWrtDeviceScanner(config[DOMAIN])
except ConnectionError:
return None
class DdWrtDeviceScanner(DeviceScanner):
2016-03-07 20:18:53 +00:00
"""This class queries a wireless router running DD-WRT firmware."""
def __init__(self, config):
"""Initialize the DD-WRT scanner."""
self.protocol = 'https' if config[CONF_SSL] else 'http'
self.verify_ssl = config[CONF_VERIFY_SSL]
self.host = config[CONF_HOST]
self.username = config[CONF_USERNAME]
2015-03-28 07:29:45 +00:00
self.password = config[CONF_PASSWORD]
self.last_results = {}
self.mac2name = {}
# Test the router is accessible
url = '{}://{}/Status_Wireless.live.asp'.format(
self.protocol, self.host)
data = self.get_ddwrt_data(url)
if not data:
raise ConnectionError('Cannot connect to DD-Wrt router')
def scan_devices(self):
2016-03-07 20:18:53 +00:00
"""Scan for new devices and return a list with found device IDs."""
self._update_info()
return self.last_results
def get_device_name(self, device):
2016-03-07 20:18:53 +00:00
"""Return the name of the given device or None if we don't know."""
# If not initialised and not already scanned and not found.
if device not in self.mac2name:
url = '{}://{}/Status_Lan.live.asp'.format(
self.protocol, self.host)
data = self.get_ddwrt_data(url)
if not data:
return None
dhcp_leases = data.get('dhcp_leases', None)
if not dhcp_leases:
return None
# Remove leading and trailing quotes and spaces
cleaned_str = dhcp_leases.replace(
"\"", "").replace("\'", "").replace(" ", "")
elements = cleaned_str.split(',')
num_clients = int(len(elements) / 5)
self.mac2name = {}
for idx in range(0, num_clients):
# The data is a single array
# every 5 elements represents one host, the MAC
# is the third element and the name is the first.
mac_index = (idx * 5) + 2
if mac_index < len(elements):
mac = elements[mac_index]
self.mac2name[mac] = elements[idx * 5]
return self.mac2name.get(device)
def _update_info(self):
2016-03-07 20:18:53 +00:00
"""Ensure the information from the DD-WRT router is up to date.
Return boolean if scanning successful.
"""
_LOGGER.info("Checking ARP")
url = '{}://{}/Status_Wireless.live.asp'.format(
self.protocol, self.host)
data = self.get_ddwrt_data(url)
if not data:
return False
self.last_results = []
active_clients = data.get('active_wireless', None)
if not active_clients:
return False
2015-03-28 07:29:45 +00:00
# The DD-WRT UI uses its own data format and then
# regex's out values so this is done here too
# Remove leading and trailing single quotes.
clean_str = active_clients.strip().strip("'")
elements = clean_str.split("','")
2015-03-28 07:29:45 +00:00
self.last_results.extend(item for item in elements
if _MAC_REGEX.match(item))
return True
def get_ddwrt_data(self, url):
2016-03-07 17:12:06 +00:00
"""Retrieve data from DD-WRT and return parsed result."""
try:
2015-03-28 07:59:12 +00:00
response = requests.get(
url, auth=(self.username, self.password),
timeout=4, verify=self.verify_ssl)
except requests.exceptions.Timeout:
_LOGGER.exception("Connection to the router timed out")
return
if response.status_code == 200:
return _parse_ddwrt_response(response.text)
if response.status_code == 401:
# Authentication error
_LOGGER.exception(
"Failed to authenticate, check your username and password")
return
_LOGGER.error("Invalid response from DD-WRT: %s", response)
def _parse_ddwrt_response(data_str):
2016-03-07 17:12:06 +00:00
"""Parse the DD-WRT data format."""
return {
key: val for key, val in _DDWRT_DATA_REGEX.findall(data_str)}