diff --git a/.coveragerc b/.coveragerc index 35c47de4160..bf980a40c92 100644 --- a/.coveragerc +++ b/.coveragerc @@ -58,7 +58,6 @@ omit = homeassistant/components/arwn/sensor.py homeassistant/components/asterisk_cdr/mailbox.py homeassistant/components/asterisk_mbox/* - homeassistant/components/asuswrt/device_tracker.py homeassistant/components/aten_pe/* homeassistant/components/atome/* homeassistant/components/august/* diff --git a/homeassistant/components/asuswrt/__init__.py b/homeassistant/components/asuswrt/__init__.py index 897258c6299..f2d7a72e54d 100644 --- a/homeassistant/components/asuswrt/__init__.py +++ b/homeassistant/components/asuswrt/__init__.py @@ -17,6 +17,8 @@ from homeassistant.helpers.discovery import async_load_platform _LOGGER = logging.getLogger(__name__) +CONF_DNSMASQ = "dnsmasq" +CONF_INTERFACE = "interface" CONF_PUB_KEY = "pub_key" CONF_REQUIRE_IP = "require_ip" CONF_SENSORS = "sensors" @@ -24,7 +26,10 @@ CONF_SSH_KEY = "ssh_key" DOMAIN = "asuswrt" DATA_ASUSWRT = DOMAIN + DEFAULT_SSH_PORT = 22 +DEFAULT_INTERFACE = "eth0" +DEFAULT_DNSMASQ = "/var/lib/misc" SECRET_GROUP = "Password or SSH Key" SENSOR_TYPES = ["upload_speed", "download_speed", "download", "upload"] @@ -45,6 +50,8 @@ CONFIG_SCHEMA = vol.Schema( 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, } ) }, @@ -59,13 +66,15 @@ async def async_setup(hass, config): api = AsusWrt( conf[CONF_HOST], - conf.get(CONF_PORT), - conf.get(CONF_PROTOCOL) == "telnet", + conf[CONF_PORT], + conf[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), + conf[CONF_MODE], + conf[CONF_REQUIRE_IP], + conf[CONF_INTERFACE], + conf[CONF_DNSMASQ], ) await api.connection.async_connect() diff --git a/homeassistant/components/asuswrt/manifest.json b/homeassistant/components/asuswrt/manifest.json index 416144b450c..c161dc4f536 100644 --- a/homeassistant/components/asuswrt/manifest.json +++ b/homeassistant/components/asuswrt/manifest.json @@ -2,7 +2,7 @@ "domain": "asuswrt", "name": "ASUSWRT", "documentation": "https://www.home-assistant.io/integrations/asuswrt", - "requirements": ["aioasuswrt==1.1.22"], + "requirements": ["aioasuswrt==1.2.2"], "dependencies": [], "codeowners": ["@kennedyshead"] } diff --git a/requirements_all.txt b/requirements_all.txt index 4a95d388ffb..03aa1089d35 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -138,7 +138,7 @@ aio_georss_gdacs==0.3 aioambient==1.0.2 # homeassistant.components.asuswrt -aioasuswrt==1.1.22 +aioasuswrt==1.2.2 # homeassistant.components.automatic aioautomatic==0.6.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 3b5c47c55cf..7f82944b575 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -50,7 +50,7 @@ aio_georss_gdacs==0.3 aioambient==1.0.2 # homeassistant.components.asuswrt -aioasuswrt==1.1.22 +aioasuswrt==1.2.2 # homeassistant.components.automatic aioautomatic==0.6.5 diff --git a/tests/components/asuswrt/test_device_tracker.py b/tests/components/asuswrt/test_device_tracker.py index 2ecab9c1d37..095b7b76d60 100644 --- a/tests/components/asuswrt/test_device_tracker.py +++ b/tests/components/asuswrt/test_device_tracker.py @@ -2,30 +2,16 @@ from unittest.mock import patch from homeassistant.components.asuswrt import ( - CONF_MODE, - CONF_PORT, - CONF_PROTOCOL, + CONF_DNSMASQ, + CONF_INTERFACE, DATA_ASUSWRT, 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 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): """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().is_connected = False 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 @@ -53,8 +41,74 @@ async def test_get_scanner_with_password_no_pubkey(hass): CONF_HOST: "fake_host", CONF_USERNAME: "fake_user", CONF_PASSWORD: "4321", + CONF_DNSMASQ: "/", } }, ) assert result 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 diff --git a/tests/components/asuswrt/test_sensor.py b/tests/components/asuswrt/test_sensor.py new file mode 100644 index 00000000000..39443c3fef8 --- /dev/null +++ b/tests/components/asuswrt/test_sensor.py @@ -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