2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ASUSWRT devices."""
|
2018-11-07 17:32:13 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_PORT, CONF_MODE,
|
|
|
|
CONF_PROTOCOL)
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers.discovery import async_load_platform
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
CONF_PUB_KEY = 'pub_key'
|
|
|
|
CONF_REQUIRE_IP = 'require_ip'
|
2019-02-13 20:21:14 +00:00
|
|
|
CONF_SENSORS = 'sensors'
|
|
|
|
CONF_SSH_KEY = 'ssh_key'
|
|
|
|
|
|
|
|
DOMAIN = "asuswrt"
|
|
|
|
DATA_ASUSWRT = DOMAIN
|
2018-11-07 17:32:13 +00:00
|
|
|
DEFAULT_SSH_PORT = 22
|
2019-02-13 20:21:14 +00:00
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
SECRET_GROUP = 'Password or SSH Key'
|
2019-01-08 14:14:16 +00:00
|
|
|
SENSOR_TYPES = ['upload_speed', 'download_speed', 'download', 'upload']
|
2018-11-07 17:32:13 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_PROTOCOL, default='ssh'): vol.In(['ssh', 'telnet']),
|
|
|
|
vol.Optional(CONF_MODE, default='router'): vol.In(['router', 'ap']),
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_SSH_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_REQUIRE_IP, default=True): cv.boolean,
|
|
|
|
vol.Exclusive(CONF_PASSWORD, SECRET_GROUP): cv.string,
|
|
|
|
vol.Exclusive(CONF_SSH_KEY, SECRET_GROUP): cv.isfile,
|
2019-01-08 14:14:16 +00:00
|
|
|
vol.Exclusive(CONF_PUB_KEY, SECRET_GROUP): cv.isfile,
|
|
|
|
vol.Optional(CONF_SENSORS): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
2018-11-07 17:32:13 +00:00
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the asuswrt component."""
|
|
|
|
from aioasuswrt.asuswrt import AsusWrt
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
|
|
|
|
api = AsusWrt(conf[CONF_HOST], conf.get(CONF_PORT),
|
|
|
|
conf.get(CONF_PROTOCOL) == 'telnet',
|
|
|
|
conf[CONF_USERNAME],
|
|
|
|
conf.get(CONF_PASSWORD, ''),
|
|
|
|
conf.get('ssh_key', conf.get('pub_key', '')),
|
|
|
|
conf.get(CONF_MODE), conf.get(CONF_REQUIRE_IP))
|
|
|
|
|
|
|
|
await api.connection.async_connect()
|
|
|
|
if not api.is_connected:
|
|
|
|
_LOGGER.error("Unable to setup asuswrt component")
|
|
|
|
return False
|
|
|
|
|
|
|
|
hass.data[DATA_ASUSWRT] = api
|
|
|
|
|
|
|
|
hass.async_create_task(async_load_platform(
|
2019-01-08 14:14:16 +00:00
|
|
|
hass, 'sensor', DOMAIN, config[DOMAIN].get(CONF_SENSORS), config))
|
2018-11-07 17:32:13 +00:00
|
|
|
hass.async_create_task(async_load_platform(
|
|
|
|
hass, 'device_tracker', DOMAIN, {}, config))
|
2019-01-08 14:14:16 +00:00
|
|
|
|
2018-11-07 17:32:13 +00:00
|
|
|
return True
|