2019-04-03 15:40:03 +00:00
|
|
|
"""Support for ASUSWRT routers."""
|
2015-07-30 09:30:31 +00:00
|
|
|
import logging
|
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
from homeassistant.components.device_tracker import DeviceScanner
|
2016-08-27 20:30:06 +00:00
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DATA_ASUSWRT
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-07-30 09:30:31 +00:00
|
|
|
|
2016-08-27 20:30:06 +00:00
|
|
|
|
2018-10-23 09:08:11 +00:00
|
|
|
async def async_get_scanner(hass, config):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Validate the configuration and return an ASUS-WRT scanner."""
|
2018-11-07 17:32:13 +00:00
|
|
|
scanner = AsusWrtDeviceScanner(hass.data[DATA_ASUSWRT])
|
2018-10-23 09:08:11 +00:00
|
|
|
await scanner.async_connect()
|
2015-07-30 09:30:31 +00:00
|
|
|
return scanner if scanner.success_init else None
|
|
|
|
|
2016-11-19 05:47:59 +00:00
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
class AsusWrtDeviceScanner(DeviceScanner):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""This class queries a router running ASUSWRT firmware."""
|
|
|
|
|
2016-06-10 04:30:47 +00:00
|
|
|
# Eighth attribute needed for mode (AP mode vs router mode)
|
2018-11-07 17:32:13 +00:00
|
|
|
def __init__(self, api):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Initialize the scanner."""
|
2018-10-23 09:08:11 +00:00
|
|
|
self.last_results = {}
|
|
|
|
self.success_init = False
|
2018-11-07 17:32:13 +00:00
|
|
|
self.connection = api
|
2020-06-24 16:50:58 +00:00
|
|
|
self._connect_error = False
|
2018-10-23 09:08:11 +00:00
|
|
|
|
|
|
|
async def async_connect(self):
|
|
|
|
"""Initialize connection to the router."""
|
2016-03-07 20:18:53 +00:00
|
|
|
# Test the router is accessible.
|
2018-10-23 09:08:11 +00:00
|
|
|
data = await self.connection.async_get_connected_devices()
|
2015-07-30 09:30:31 +00:00
|
|
|
self.success_init = data is not None
|
|
|
|
|
2018-10-23 09:08:11 +00:00
|
|
|
async def async_scan_devices(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Scan for new devices and return a list with found device IDs."""
|
2018-10-23 09:08:11 +00:00
|
|
|
await self.async_update_info()
|
2017-12-17 11:46:47 +00:00
|
|
|
return list(self.last_results.keys())
|
2015-07-30 09:30:31 +00:00
|
|
|
|
2018-10-23 09:08:11 +00:00
|
|
|
async def async_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-12-17 11:46:47 +00:00
|
|
|
if device not in self.last_results:
|
2015-07-30 09:30:31 +00:00
|
|
|
return None
|
2017-12-17 11:46:47 +00:00
|
|
|
return self.last_results[device].name
|
2015-07-30 09:30:31 +00:00
|
|
|
|
2018-10-23 09:08:11 +00:00
|
|
|
async def async_update_info(self):
|
2016-03-07 20:18:53 +00:00
|
|
|
"""Ensure the information from the ASUSWRT router is up to date.
|
|
|
|
|
|
|
|
Return boolean if scanning successful.
|
2015-09-07 17:19:11 +00:00
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Checking Devices")
|
2017-05-23 16:55:01 +00:00
|
|
|
|
2020-06-24 16:50:58 +00:00
|
|
|
try:
|
|
|
|
self.last_results = await self.connection.async_get_connected_devices()
|
|
|
|
if self._connect_error:
|
|
|
|
self._connect_error = False
|
2020-07-05 18:21:21 +00:00
|
|
|
_LOGGER.info("Reconnected to ASUS router for device update")
|
2020-06-24 16:50:58 +00:00
|
|
|
|
|
|
|
except OSError as err:
|
|
|
|
if not self._connect_error:
|
|
|
|
self._connect_error = True
|
|
|
|
_LOGGER.error(
|
|
|
|
"Error connecting to ASUS router for device update: %s", err
|
|
|
|
)
|