2019-04-03 15:40:03 +00:00
|
|
|
"""Support for OpenWRT (luci) routers."""
|
2019-02-20 15:55:00 +00:00
|
|
|
import logging
|
2019-04-10 12:35:57 +00:00
|
|
|
|
2019-12-04 10:13:22 +00:00
|
|
|
from openwrt_luci_rpc import OpenWrtRpc
|
2016-09-05 17:37:36 +00:00
|
|
|
import voluptuous as vol
|
2014-11-12 05:39:17 +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,
|
|
|
|
)
|
2019-10-20 16:43:44 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2019-04-10 12:35:57 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-05-26 07:51:21 +00:00
|
|
|
DEFAULT_SSL = False
|
2019-10-20 16:43:44 +00:00
|
|
|
DEFAULT_VERIFY_SSL = True
|
2018-05-26 07:51:21 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
2019-10-20 16:43:44 +00:00
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2016-09-05 17:37:36 +00:00
|
|
|
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
def get_scanner(hass, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Validate the configuration and return a Luci scanner."""
|
2014-11-12 05:39:17 +00:00
|
|
|
scanner = LuciDeviceScanner(config[DOMAIN])
|
|
|
|
|
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class LuciDeviceScanner(DeviceScanner):
|
2019-02-20 15:55:00 +00:00
|
|
|
"""This class scans for devices connected to an OpenWrt router."""
|
2016-03-07 20:18:53 +00:00
|
|
|
|
2014-11-12 05:39:17 +00:00
|
|
|
def __init__(self, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Initialize the scanner."""
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2019-04-10 12:35:57 +00:00
|
|
|
self.router = OpenWrtRpc(
|
2019-07-31 19:25:30 +00:00
|
|
|
config[CONF_HOST],
|
|
|
|
config[CONF_USERNAME],
|
|
|
|
config[CONF_PASSWORD],
|
|
|
|
config[CONF_SSL],
|
2019-10-20 16:43:44 +00:00
|
|
|
config[CONF_VERIFY_SSL],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2019-02-20 15:55:00 +00:00
|
|
|
self.last_results = {}
|
|
|
|
self.success_init = self.router.is_logged_in()
|
2017-03-20 15:55:59 +00:00
|
|
|
|
2014-11-12 05:39:17 +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."""
|
2014-11-12 05:39:17 +00:00
|
|
|
self._update_info()
|
2019-02-20 15:55:00 +00:00
|
|
|
|
2018-11-06 12:15:48 +00:00
|
|
|
return [device.mac for device in self.last_results]
|
2014-11-12 05:39:17 +00:00
|
|
|
|
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
name = next(
|
|
|
|
(result.hostname for result in self.last_results if result.mac == device),
|
|
|
|
None,
|
|
|
|
)
|
2019-02-20 15:55:00 +00:00
|
|
|
return name
|
2018-11-06 12:15:48 +00:00
|
|
|
|
2019-02-23 15:28:41 +00:00
|
|
|
def get_extra_attributes(self, device):
|
|
|
|
"""
|
|
|
|
Get extra attributes of a device.
|
|
|
|
|
|
|
|
Some known extra attributes that may be returned in the device tuple
|
2019-04-10 12:35:57 +00:00
|
|
|
include MAC address (mac), network device (dev), IP address
|
|
|
|
(ip), reachable status (reachable), associated router
|
|
|
|
(host), hostname if known (hostname) among others.
|
2019-02-23 15:28:41 +00:00
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
device = next(
|
|
|
|
(result for result in self.last_results if result.mac == device), None
|
|
|
|
)
|
2019-02-23 15:28:41 +00:00
|
|
|
return device._asdict()
|
|
|
|
|
2014-11-12 05:39:17 +00:00
|
|
|
def _update_info(self):
|
2019-02-20 15:55:00 +00:00
|
|
|
"""Check the Luci router for devices."""
|
2019-07-31 19:25:30 +00:00
|
|
|
result = self.router.get_all_connected_devices(only_reachable=True)
|
2019-02-20 15:55:00 +00:00
|
|
|
|
2019-04-10 12:35:57 +00:00
|
|
|
_LOGGER.debug("Luci get_all_connected_devices returned: %s", result)
|
2019-02-20 15:55:00 +00:00
|
|
|
|
|
|
|
last_results = []
|
|
|
|
for device in result:
|
2020-09-22 08:53:46 +00:00
|
|
|
if (
|
|
|
|
not hasattr(self.router.router.owrt_version, "release")
|
2020-09-25 07:06:24 +00:00
|
|
|
or not self.router.router.owrt_version.release
|
2020-09-22 08:53:46 +00:00
|
|
|
or self.router.router.owrt_version.release[0] < 19
|
|
|
|
or device.reachable
|
|
|
|
):
|
2020-09-17 09:35:13 +00:00
|
|
|
last_results.append(device)
|
2019-02-20 15:55:00 +00:00
|
|
|
|
|
|
|
self.last_results = last_results
|