2019-04-03 15:40:03 +00:00
|
|
|
"""Support for DD-WRT routers."""
|
2015-03-27 16:51:33 +00:00
|
|
|
import logging
|
|
|
|
import re
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2015-03-27 16:51:33 +00:00
|
|
|
import requests
|
2016-09-02 04:28:03 +00:00
|
|
|
import voluptuous as vol
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DeviceScanner,
|
|
|
|
)
|
2018-10-13 19:04:51 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
2020-04-08 16:47:38 +00:00
|
|
|
HTTP_OK,
|
2020-09-15 17:01:07 +00:00
|
|
|
HTTP_UNAUTHORIZED,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-13 19:04:51 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-03-27 16:51:33 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
_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})")
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2018-10-13 19:04:51 +00:00
|
|
|
DEFAULT_SSL = False
|
|
|
|
DEFAULT_VERIFY_SSL = True
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_WIRELESS_ONLY = "wireless_only"
|
2019-06-08 00:45:58 +00:00
|
|
|
DEFAULT_WIRELESS_ONLY = True
|
2018-10-13 19:04:51 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
|
|
|
vol.Optional(CONF_WIRELESS_ONLY, default=DEFAULT_WIRELESS_ONLY): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
2016-09-02 04:28:03 +00:00
|
|
|
|
2015-03-29 00:49:07 +00:00
|
|
|
|
2015-03-27 16:51:33 +00:00
|
|
|
def get_scanner(hass, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Validate the configuration and return a DD-WRT scanner."""
|
2016-10-25 05:18:24 +00:00
|
|
|
try:
|
|
|
|
return DdWrtDeviceScanner(config[DOMAIN])
|
|
|
|
except ConnectionError:
|
|
|
|
return None
|
2015-03-27 16:51:33 +00:00
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class DdWrtDeviceScanner(DeviceScanner):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""This class queries a wireless router running DD-WRT firmware."""
|
|
|
|
|
2015-03-27 16:51:33 +00:00
|
|
|
def __init__(self, config):
|
2018-10-13 19:04:51 +00:00
|
|
|
"""Initialize the DD-WRT scanner."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.protocol = "https" if config[CONF_SSL] else "http"
|
2018-10-13 19:04:51 +00:00
|
|
|
self.verify_ssl = config[CONF_VERIFY_SSL]
|
2015-03-27 16:51:33 +00:00
|
|
|
self.host = config[CONF_HOST]
|
|
|
|
self.username = config[CONF_USERNAME]
|
2015-03-28 07:29:45 +00:00
|
|
|
self.password = config[CONF_PASSWORD]
|
2019-06-08 00:45:58 +00:00
|
|
|
self.wireless_only = config[CONF_WIRELESS_ONLY]
|
2015-03-27 16:51:33 +00:00
|
|
|
|
|
|
|
self.last_results = {}
|
2015-10-09 05:15:12 +00:00
|
|
|
self.mac2name = {}
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2015-03-28 08:17:51 +00:00
|
|
|
# Test the router is accessible
|
2019-09-03 15:09:59 +00:00
|
|
|
url = f"{self.protocol}://{self.host}/Status_Wireless.live.asp"
|
2015-03-27 16:51:33 +00:00
|
|
|
data = self.get_ddwrt_data(url)
|
2016-10-25 05:18:24 +00:00
|
|
|
if not data:
|
2019-07-31 19:25:30 +00:00
|
|
|
raise ConnectionError("Cannot connect to DD-Wrt router")
|
2015-03-27 16:51:33 +00:00
|
|
|
|
|
|
|
def scan_devices(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
2015-03-27 16:51:33 +00:00
|
|
|
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."""
|
2017-07-24 14:45:02 +00:00
|
|
|
# If not initialised and not already scanned and not found.
|
|
|
|
if device not in self.mac2name:
|
2019-09-03 15:09:59 +00:00
|
|
|
url = f"{self.protocol}://{self.host}/Status_Lan.live.asp"
|
2017-07-24 14:45:02 +00:00
|
|
|
data = self.get_ddwrt_data(url)
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
2020-04-07 19:06:05 +00:00
|
|
|
dhcp_leases = data.get("dhcp_leases")
|
2017-07-24 14:45:02 +00:00
|
|
|
|
|
|
|
if not dhcp_leases:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Remove leading and trailing quotes and spaces
|
2019-07-31 19:25:30 +00:00
|
|
|
cleaned_str = dhcp_leases.replace('"', "").replace("'", "").replace(" ", "")
|
|
|
|
elements = cleaned_str.split(",")
|
2017-07-24 14:45:02 +00:00
|
|
|
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)
|
|
|
|
|
2015-03-27 16:51:33 +00:00
|
|
|
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.
|
2015-09-07 17:19:11 +00:00
|
|
|
"""
|
2020-06-08 09:30:36 +00:00
|
|
|
_LOGGER.debug("Checking ARP")
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
endpoint = "Wireless" if self.wireless_only else "Lan"
|
2019-09-03 15:09:59 +00:00
|
|
|
url = f"{self.protocol}://{self.host}/Status_{endpoint}.live.asp"
|
2017-07-24 14:45:02 +00:00
|
|
|
data = self.get_ddwrt_data(url)
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2017-07-24 14:45:02 +00:00
|
|
|
if not data:
|
|
|
|
return False
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2017-07-24 14:45:02 +00:00
|
|
|
self.last_results = []
|
2015-10-09 05:15:12 +00:00
|
|
|
|
2019-06-08 00:45:58 +00:00
|
|
|
if self.wireless_only:
|
2020-04-07 19:06:05 +00:00
|
|
|
active_clients = data.get("active_wireless")
|
2019-06-08 00:45:58 +00:00
|
|
|
else:
|
2020-04-07 19:06:05 +00:00
|
|
|
active_clients = data.get("arp_table")
|
2017-07-24 14:45:02 +00:00
|
|
|
if not active_clients:
|
|
|
|
return False
|
2015-03-28 07:29:45 +00:00
|
|
|
|
2017-07-24 14:45:02 +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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.last_results.extend(item for item in elements if _MAC_REGEX.match(item))
|
2015-03-27 16:51:33 +00:00
|
|
|
|
2017-07-24 14:45:02 +00:00
|
|
|
return True
|
2015-03-27 16:51:33 +00:00
|
|
|
|
|
|
|
def get_ddwrt_data(self, url):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""Retrieve data from DD-WRT and return parsed result."""
|
2015-03-27 16:51:33 +00:00
|
|
|
try:
|
2015-03-28 07:59:12 +00:00
|
|
|
response = requests.get(
|
2019-07-31 19:25:30 +00:00
|
|
|
url,
|
|
|
|
auth=(self.username, self.password),
|
|
|
|
timeout=4,
|
|
|
|
verify=self.verify_ssl,
|
|
|
|
)
|
2015-03-27 16:51:33 +00:00
|
|
|
except requests.exceptions.Timeout:
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.exception("Connection to the router timed out")
|
2015-03-27 16:51:33 +00:00
|
|
|
return
|
2020-04-08 16:47:38 +00:00
|
|
|
if response.status_code == HTTP_OK:
|
2015-03-27 16:51:33 +00:00
|
|
|
return _parse_ddwrt_response(response.text)
|
2020-09-15 17:01:07 +00:00
|
|
|
if response.status_code == HTTP_UNAUTHORIZED:
|
2015-03-28 08:17:51 +00:00
|
|
|
# Authentication error
|
2015-03-27 16:51:33 +00:00
|
|
|
_LOGGER.exception(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Failed to authenticate, check your username and password"
|
|
|
|
)
|
2015-03-27 16:51:33 +00:00
|
|
|
return
|
2018-07-23 08:16:05 +00:00
|
|
|
_LOGGER.error("Invalid response from DD-WRT: %s", response)
|
2015-03-27 16:51:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _parse_ddwrt_response(data_str):
|
2016-03-07 17:12:06 +00:00
|
|
|
"""Parse the DD-WRT data format."""
|
2019-10-07 15:17:39 +00:00
|
|
|
return dict(_DDWRT_DATA_REGEX.findall(data_str))
|