mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Added ssl and verify_ssl parameters in ddwrt device tracker component (#17406)
* Added ssl and verify_ssl parameters in ddwrt device tracker component * Set defaults in validation
This commit is contained in:
parent
78e29cd3fa
commit
ef8253c549
@ -10,20 +10,26 @@ import re
|
|||||||
import requests
|
import requests
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.components.device_tracker import (
|
from homeassistant.components.device_tracker import (
|
||||||
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
DOMAIN, PLATFORM_SCHEMA, DeviceScanner)
|
||||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
_DDWRT_DATA_REGEX = re.compile(r'\{(\w+)::([^\}]*)\}')
|
_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})')
|
_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({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
vol.Required(CONF_USERNAME): 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,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -39,7 +45,9 @@ class DdWrtDeviceScanner(DeviceScanner):
|
|||||||
"""This class queries a wireless router running DD-WRT firmware."""
|
"""This class queries a wireless router running DD-WRT firmware."""
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
"""Initialize the scanner."""
|
"""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.host = config[CONF_HOST]
|
||||||
self.username = config[CONF_USERNAME]
|
self.username = config[CONF_USERNAME]
|
||||||
self.password = config[CONF_PASSWORD]
|
self.password = config[CONF_PASSWORD]
|
||||||
@ -48,7 +56,8 @@ class DdWrtDeviceScanner(DeviceScanner):
|
|||||||
self.mac2name = {}
|
self.mac2name = {}
|
||||||
|
|
||||||
# Test the router is accessible
|
# Test the router is accessible
|
||||||
url = 'http://{}/Status_Wireless.live.asp'.format(self.host)
|
url = '{}://{}/Status_Wireless.live.asp'.format(
|
||||||
|
self.protocol, self.host)
|
||||||
data = self.get_ddwrt_data(url)
|
data = self.get_ddwrt_data(url)
|
||||||
if not data:
|
if not data:
|
||||||
raise ConnectionError('Cannot connect to DD-Wrt router')
|
raise ConnectionError('Cannot connect to DD-Wrt router')
|
||||||
@ -63,7 +72,8 @@ class DdWrtDeviceScanner(DeviceScanner):
|
|||||||
"""Return the name of the given device or None if we don't know."""
|
"""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 not initialised and not already scanned and not found.
|
||||||
if device not in self.mac2name:
|
if device not in self.mac2name:
|
||||||
url = 'http://{}/Status_Lan.live.asp'.format(self.host)
|
url = '{}://{}/Status_Lan.live.asp'.format(
|
||||||
|
self.protocol, self.host)
|
||||||
data = self.get_ddwrt_data(url)
|
data = self.get_ddwrt_data(url)
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
@ -98,7 +108,8 @@ class DdWrtDeviceScanner(DeviceScanner):
|
|||||||
"""
|
"""
|
||||||
_LOGGER.info("Checking ARP")
|
_LOGGER.info("Checking ARP")
|
||||||
|
|
||||||
url = 'http://{}/Status_Wireless.live.asp'.format(self.host)
|
url = '{}://{}/Status_Wireless.live.asp'.format(
|
||||||
|
self.protocol, self.host)
|
||||||
data = self.get_ddwrt_data(url)
|
data = self.get_ddwrt_data(url)
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
@ -125,7 +136,8 @@ class DdWrtDeviceScanner(DeviceScanner):
|
|||||||
"""Retrieve data from DD-WRT and return parsed result."""
|
"""Retrieve data from DD-WRT and return parsed result."""
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url, auth=(self.username, self.password), timeout=4)
|
url, auth=(self.username, self.password),
|
||||||
|
timeout=4, verify=self.verify_ssl)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
_LOGGER.exception("Connection to the router timed out")
|
_LOGGER.exception("Connection to the router timed out")
|
||||||
return
|
return
|
||||||
@ -142,5 +154,4 @@ class DdWrtDeviceScanner(DeviceScanner):
|
|||||||
def _parse_ddwrt_response(data_str):
|
def _parse_ddwrt_response(data_str):
|
||||||
"""Parse the DD-WRT data format."""
|
"""Parse the DD-WRT data format."""
|
||||||
return {
|
return {
|
||||||
key: val for key, val in _DDWRT_DATA_REGEX
|
key: val for key, val in _DDWRT_DATA_REGEX.findall(data_str)}
|
||||||
.findall(data_str)}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user