core/homeassistant/components/asuswrt/__init__.py

97 lines
2.7 KiB
Python
Raw Normal View History

"""Support for ASUSWRT devices."""
import logging
from aioasuswrt.asuswrt import AsusWrt
import voluptuous as vol
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_MODE,
2019-07-31 19:25:30 +00:00
CONF_PASSWORD,
CONF_PORT,
CONF_PROTOCOL,
CONF_USERNAME,
2019-07-31 19:25:30 +00:00
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.discovery import async_load_platform
_LOGGER = logging.getLogger(__name__)
CONF_DNSMASQ = "dnsmasq"
CONF_INTERFACE = "interface"
2019-07-31 19:25:30 +00:00
CONF_PUB_KEY = "pub_key"
CONF_REQUIRE_IP = "require_ip"
CONF_SENSORS = "sensors"
CONF_SSH_KEY = "ssh_key"
DOMAIN = "asuswrt"
DATA_ASUSWRT = DOMAIN
DEFAULT_SSH_PORT = 22
DEFAULT_INTERFACE = "eth0"
DEFAULT_DNSMASQ = "/var/lib/misc"
2019-07-31 19:25:30 +00:00
SECRET_GROUP = "Password or SSH Key"
SENSOR_TYPES = ["upload_speed", "download_speed", "download", "upload"]
2019-07-31 19:25:30 +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,
vol.Exclusive(CONF_PUB_KEY, SECRET_GROUP): cv.isfile,
vol.Optional(CONF_SENSORS): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]
),
vol.Optional(CONF_INTERFACE, default=DEFAULT_INTERFACE): cv.string,
vol.Optional(CONF_DNSMASQ, default=DEFAULT_DNSMASQ): cv.isdir,
2019-07-31 19:25:30 +00:00
}
)
},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass, config):
"""Set up the asuswrt component."""
2019-07-31 19:25:30 +00:00
conf = config[DOMAIN]
2019-07-31 19:25:30 +00:00
api = AsusWrt(
conf[CONF_HOST],
conf[CONF_PORT],
conf[CONF_PROTOCOL] == "telnet",
2019-07-31 19:25:30 +00:00
conf[CONF_USERNAME],
conf.get(CONF_PASSWORD, ""),
conf.get("ssh_key", conf.get("pub_key", "")),
conf[CONF_MODE],
conf[CONF_REQUIRE_IP],
conf[CONF_INTERFACE],
conf[CONF_DNSMASQ],
2019-07-31 19:25:30 +00:00
)
await api.connection.async_connect()
if not api.is_connected:
_LOGGER.error("Unable to setup component")
return False
hass.data[DATA_ASUSWRT] = api
2019-07-31 19:25:30 +00:00
hass.async_create_task(
async_load_platform(
hass, "sensor", DOMAIN, config[DOMAIN].get(CONF_SENSORS), config
)
)
hass.async_create_task(
async_load_platform(hass, "device_tracker", DOMAIN, {}, config)
)
return True