Black
This commit is contained in:
1 parent
da05dfe708
commit
4de97abc3a
2676 files changed
+163166
-140084
No files matched your search
@@ -6,29 +6,39 @@ import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.device_tracker import (
|
||||
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
||||
DOMAIN,
|
||||
PLATFORM_SCHEMA,
|
||||
DeviceScanner,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_PASSWORD, CONF_SSL, CONF_USERNAME, CONF_VERIFY_SSL)
|
||||
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})')
|
||||
_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
|
||||
CONF_WIRELESS_ONLY = 'wireless_only'
|
||||
CONF_WIRELESS_ONLY = "wireless_only"
|
||||
DEFAULT_WIRELESS_ONLY = 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,
|
||||
vol.Optional(CONF_WIRELESS_ONLY, default=DEFAULT_WIRELESS_ONLY): cv.boolean
|
||||
})
|
||||
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,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def get_scanner(hass, config):
|
||||
@@ -44,7 +54,7 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
|
||||
def __init__(self, config):
|
||||
"""Initialize the DD-WRT scanner."""
|
||||
self.protocol = 'https' if config[CONF_SSL] else 'http'
|
||||
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]
|
||||
@@ -55,11 +65,10 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
self.mac2name = {}
|
||||
|
||||
# Test the router is accessible
|
||||
url = '{}://{}/Status_Wireless.live.asp'.format(
|
||||
self.protocol, self.host)
|
||||
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')
|
||||
raise ConnectionError("Cannot connect to DD-Wrt router")
|
||||
|
||||
def scan_devices(self):
|
||||
"""Scan for new devices and return a list with found device IDs."""
|
||||
@@ -71,22 +80,20 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
"""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)
|
||||
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)
|
||||
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(',')
|
||||
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):
|
||||
@@ -107,9 +114,8 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
"""
|
||||
_LOGGER.info("Checking ARP")
|
||||
|
||||
endpoint = 'Wireless' if self.wireless_only else 'Lan'
|
||||
url = '{}://{}/Status_{}.live.asp'.format(
|
||||
self.protocol, self.host, endpoint)
|
||||
endpoint = "Wireless" if self.wireless_only else "Lan"
|
||||
url = "{}://{}/Status_{}.live.asp".format(self.protocol, self.host, endpoint)
|
||||
data = self.get_ddwrt_data(url)
|
||||
|
||||
if not data:
|
||||
@@ -118,9 +124,9 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
self.last_results = []
|
||||
|
||||
if self.wireless_only:
|
||||
active_clients = data.get('active_wireless', None)
|
||||
active_clients = data.get("active_wireless", None)
|
||||
else:
|
||||
active_clients = data.get('arp_table', None)
|
||||
active_clients = data.get("arp_table", None)
|
||||
if not active_clients:
|
||||
return False
|
||||
|
||||
@@ -130,8 +136,7 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
clean_str = active_clients.strip().strip("'")
|
||||
elements = clean_str.split("','")
|
||||
|
||||
self.last_results.extend(item for item in elements
|
||||
if _MAC_REGEX.match(item))
|
||||
self.last_results.extend(item for item in elements if _MAC_REGEX.match(item))
|
||||
|
||||
return True
|
||||
|
||||
@@ -139,8 +144,11 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
"""Retrieve data from DD-WRT and return parsed result."""
|
||||
try:
|
||||
response = requests.get(
|
||||
url, auth=(self.username, self.password),
|
||||
timeout=4, verify=self.verify_ssl)
|
||||
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
|
||||
@@ -149,12 +157,12 @@ class DdWrtDeviceScanner(DeviceScanner):
|
||||
if response.status_code == 401:
|
||||
# Authentication error
|
||||
_LOGGER.exception(
|
||||
"Failed to authenticate, check your username and password")
|
||||
"Failed to authenticate, check your username and password"
|
||||
)
|
||||
return
|
||||
_LOGGER.error("Invalid response from DD-WRT: %s", response)
|
||||
|
||||
|
||||
def _parse_ddwrt_response(data_str):
|
||||
"""Parse the DD-WRT data format."""
|
||||
return {
|
||||
key: val for key, val in _DDWRT_DATA_REGEX.findall(data_str)}
|
||||
return {key: val for key, val in _DDWRT_DATA_REGEX.findall(data_str)}
|
||||
Reference in new issue
Block a user