Add asuswrt interface and dnsmasq location configuration (#29834)

* Added interface and dnsmasq location configuration

* Updated aioasuswrt requirement to latest 1.2.1 release

* Updated to working aioasus library and fixed correct conf formatting
pull/32068/head
Patrick Kishino 2020-02-22 01:01:57 +09:00 committed by GitHub
parent dd8597cb46
commit f32411e394
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 26 deletions

View File

@ -58,7 +58,6 @@ omit =
homeassistant/components/arwn/sensor.py homeassistant/components/arwn/sensor.py
homeassistant/components/asterisk_cdr/mailbox.py homeassistant/components/asterisk_cdr/mailbox.py
homeassistant/components/asterisk_mbox/* homeassistant/components/asterisk_mbox/*
homeassistant/components/asuswrt/device_tracker.py
homeassistant/components/aten_pe/* homeassistant/components/aten_pe/*
homeassistant/components/atome/* homeassistant/components/atome/*
homeassistant/components/august/* homeassistant/components/august/*

View File

@ -17,6 +17,8 @@ from homeassistant.helpers.discovery import async_load_platform
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_DNSMASQ = "dnsmasq"
CONF_INTERFACE = "interface"
CONF_PUB_KEY = "pub_key" CONF_PUB_KEY = "pub_key"
CONF_REQUIRE_IP = "require_ip" CONF_REQUIRE_IP = "require_ip"
CONF_SENSORS = "sensors" CONF_SENSORS = "sensors"
@ -24,7 +26,10 @@ CONF_SSH_KEY = "ssh_key"
DOMAIN = "asuswrt" DOMAIN = "asuswrt"
DATA_ASUSWRT = DOMAIN DATA_ASUSWRT = DOMAIN
DEFAULT_SSH_PORT = 22 DEFAULT_SSH_PORT = 22
DEFAULT_INTERFACE = "eth0"
DEFAULT_DNSMASQ = "/var/lib/misc"
SECRET_GROUP = "Password or SSH Key" SECRET_GROUP = "Password or SSH Key"
SENSOR_TYPES = ["upload_speed", "download_speed", "download", "upload"] SENSOR_TYPES = ["upload_speed", "download_speed", "download", "upload"]
@ -45,6 +50,8 @@ CONFIG_SCHEMA = vol.Schema(
vol.Optional(CONF_SENSORS): vol.All( vol.Optional(CONF_SENSORS): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)] 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,
} }
) )
}, },
@ -59,13 +66,15 @@ async def async_setup(hass, config):
api = AsusWrt( api = AsusWrt(
conf[CONF_HOST], conf[CONF_HOST],
conf.get(CONF_PORT), conf[CONF_PORT],
conf.get(CONF_PROTOCOL) == "telnet", conf[CONF_PROTOCOL] == "telnet",
conf[CONF_USERNAME], conf[CONF_USERNAME],
conf.get(CONF_PASSWORD, ""), conf.get(CONF_PASSWORD, ""),
conf.get("ssh_key", conf.get("pub_key", "")), conf.get("ssh_key", conf.get("pub_key", "")),
conf.get(CONF_MODE), conf[CONF_MODE],
conf.get(CONF_REQUIRE_IP), conf[CONF_REQUIRE_IP],
conf[CONF_INTERFACE],
conf[CONF_DNSMASQ],
) )
await api.connection.async_connect() await api.connection.async_connect()

View File

@ -2,7 +2,7 @@
"domain": "asuswrt", "domain": "asuswrt",
"name": "ASUSWRT", "name": "ASUSWRT",
"documentation": "https://www.home-assistant.io/integrations/asuswrt", "documentation": "https://www.home-assistant.io/integrations/asuswrt",
"requirements": ["aioasuswrt==1.1.22"], "requirements": ["aioasuswrt==1.2.2"],
"dependencies": [], "dependencies": [],
"codeowners": ["@kennedyshead"] "codeowners": ["@kennedyshead"]
} }

View File

@ -138,7 +138,7 @@ aio_georss_gdacs==0.3
aioambient==1.0.2 aioambient==1.0.2
# homeassistant.components.asuswrt # homeassistant.components.asuswrt
aioasuswrt==1.1.22 aioasuswrt==1.2.2
# homeassistant.components.automatic # homeassistant.components.automatic
aioautomatic==0.6.5 aioautomatic==0.6.5

View File

@ -50,7 +50,7 @@ aio_georss_gdacs==0.3
aioambient==1.0.2 aioambient==1.0.2
# homeassistant.components.asuswrt # homeassistant.components.asuswrt
aioasuswrt==1.1.22 aioasuswrt==1.2.2
# homeassistant.components.automatic # homeassistant.components.automatic
aioautomatic==0.6.5 aioautomatic==0.6.5

View File

@ -2,30 +2,16 @@
from unittest.mock import patch from unittest.mock import patch
from homeassistant.components.asuswrt import ( from homeassistant.components.asuswrt import (
CONF_MODE, CONF_DNSMASQ,
CONF_PORT, CONF_INTERFACE,
CONF_PROTOCOL,
DATA_ASUSWRT, DATA_ASUSWRT,
DOMAIN, DOMAIN,
) )
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PLATFORM, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import mock_coro_func from tests.common import mock_coro_func
FAKEFILE = None
VALID_CONFIG_ROUTER_SSH = {
DOMAIN: {
CONF_PLATFORM: "asuswrt",
CONF_HOST: "fake_host",
CONF_USERNAME: "fake_user",
CONF_PROTOCOL: "ssh",
CONF_MODE: "router",
CONF_PORT: "22",
}
}
async def test_password_or_pub_key_required(hass): async def test_password_or_pub_key_required(hass):
"""Test creating an AsusWRT scanner without a pass or pubkey.""" """Test creating an AsusWRT scanner without a pass or pubkey."""
@ -33,7 +19,9 @@ async def test_password_or_pub_key_required(hass):
AsusWrt().connection.async_connect = mock_coro_func() AsusWrt().connection.async_connect = mock_coro_func()
AsusWrt().is_connected = False AsusWrt().is_connected = False
result = await async_setup_component( result = await async_setup_component(
hass, DOMAIN, {DOMAIN: {CONF_HOST: "fake_host", CONF_USERNAME: "fake_user"}} hass,
DOMAIN,
{DOMAIN: {CONF_HOST: "fake_host", CONF_USERNAME: "fake_user"}},
) )
assert not result assert not result
@ -53,8 +41,74 @@ async def test_get_scanner_with_password_no_pubkey(hass):
CONF_HOST: "fake_host", CONF_HOST: "fake_host",
CONF_USERNAME: "fake_user", CONF_USERNAME: "fake_user",
CONF_PASSWORD: "4321", CONF_PASSWORD: "4321",
CONF_DNSMASQ: "/",
} }
}, },
) )
assert result assert result
assert hass.data[DATA_ASUSWRT] is not None assert hass.data[DATA_ASUSWRT] is not None
async def test_specify_non_directory_path_for_dnsmasq(hass):
"""Test creating an AsusWRT scanner with a dnsmasq location which is not a valid directory."""
with patch("homeassistant.components.asuswrt.AsusWrt") as AsusWrt:
AsusWrt().connection.async_connect = mock_coro_func()
AsusWrt().is_connected = False
result = await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
CONF_HOST: "fake_host",
CONF_USERNAME: "fake_user",
CONF_PASSWORD: "4321",
CONF_DNSMASQ: "?non_directory?",
}
},
)
assert not result
async def test_interface(hass):
"""Test creating an AsusWRT scanner using interface eth1."""
with patch("homeassistant.components.asuswrt.AsusWrt") as AsusWrt:
AsusWrt().connection.async_connect = mock_coro_func()
AsusWrt().connection.async_get_connected_devices = mock_coro_func(
return_value={}
)
result = await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
CONF_HOST: "fake_host",
CONF_USERNAME: "fake_user",
CONF_PASSWORD: "4321",
CONF_DNSMASQ: "/",
CONF_INTERFACE: "eth1",
}
},
)
assert result
assert hass.data[DATA_ASUSWRT] is not None
async def test_no_interface(hass):
"""Test creating an AsusWRT scanner using no interface."""
with patch("homeassistant.components.asuswrt.AsusWrt") as AsusWrt:
AsusWrt().connection.async_connect = mock_coro_func()
AsusWrt().is_connected = False
result = await async_setup_component(
hass,
DOMAIN,
{
DOMAIN: {
CONF_HOST: "fake_host",
CONF_USERNAME: "fake_user",
CONF_PASSWORD: "4321",
CONF_DNSMASQ: "/",
CONF_INTERFACE: None,
}
},
)
assert not result

View File

@ -0,0 +1,42 @@
"""The tests for the ASUSWRT sensor platform."""
from unittest.mock import patch
# import homeassistant.components.sensor as sensor
from homeassistant.components.asuswrt import (
CONF_DNSMASQ,
CONF_INTERFACE,
CONF_MODE,
CONF_PORT,
CONF_PROTOCOL,
CONF_SENSORS,
DATA_ASUSWRT,
DOMAIN,
)
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
from homeassistant.setup import async_setup_component
from tests.common import mock_coro_func
VALID_CONFIG_ROUTER_SSH = {
DOMAIN: {
CONF_DNSMASQ: "/",
CONF_HOST: "fake_host",
CONF_INTERFACE: "eth0",
CONF_MODE: "router",
CONF_PORT: "22",
CONF_PROTOCOL: "ssh",
CONF_USERNAME: "fake_user",
CONF_PASSWORD: "fake_pass",
CONF_SENSORS: "upload",
}
}
async def test_default_sensor_setup(hass):
"""Test creating an AsusWRT sensor."""
with patch("homeassistant.components.asuswrt.AsusWrt") as AsusWrt:
AsusWrt().connection.async_connect = mock_coro_func()
result = await async_setup_component(hass, DOMAIN, VALID_CONFIG_ROUTER_SSH)
assert result
assert hass.data[DATA_ASUSWRT] is not None