From 248f28a9220df74968f3f5b3a032ff68440e09c2 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Wed, 25 May 2022 13:47:58 +0300 Subject: [PATCH 1/3] Fix descovery --- custom_components/miwifi/const.py | 1 + custom_components/miwifi/discovery.py | 38 +++++++++++++++++++----- custom_components/miwifi/manifest.json | 2 +- tests/test_discovery.py | 40 ++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 10 deletions(-) diff --git a/custom_components/miwifi/const.py b/custom_components/miwifi/const.py index bd42790..599fc78 100644 --- a/custom_components/miwifi/const.py +++ b/custom_components/miwifi/const.py @@ -53,6 +53,7 @@ DEFAULT_RETRY: Final = 10 DEFAULT_SCAN_INTERVAL: Final = 30 DEFAULT_TIMEOUT: Final = 20 +DEFAULT_CHECK_TIMEOUT: Final = 5 DEFAULT_STAY_ONLINE: Final = 0 DEFAULT_ACTIVITY_DAYS: Final = 30 DEFAULT_CALL_DELAY: Final = 1 diff --git a/custom_components/miwifi/discovery.py b/custom_components/miwifi/discovery.py index 62b3251..81fe87e 100644 --- a/custom_components/miwifi/discovery.py +++ b/custom_components/miwifi/discovery.py @@ -16,11 +16,12 @@ from .const import ( CLIENT_ADDRESS, CLIENT_ADDRESS_IP, + DEFAULT_CHECK_TIMEOUT, DISCOVERY, DISCOVERY_INTERVAL, DOMAIN, ) -from .exceptions import LuciError +from .exceptions import LuciConnectionError, LuciError from .luci import LuciClient _LOGGER = logging.getLogger(__name__) @@ -79,10 +80,13 @@ async def async_discover_devices(client: AsyncClient) -> list: ): return [] - devices = [response["graph"]["ip"].strip()] + devices: list = [] + + if await async_check_ip_address(client, response["graph"]["ip"].strip()): + devices.append(response["graph"]["ip"].strip()) if "leafs" in response["graph"]: - devices = parse_leafs(devices, response["graph"]["leafs"]) + devices = await async_prepare_leafs(client, devices, response["graph"]["leafs"]) _LOGGER.debug("Found devices: %s", devices) @@ -110,9 +114,10 @@ def async_trigger_discovery( ) -def parse_leafs(devices: list, leafs: list) -> list: - """Recursive parse leafs. +async def async_prepare_leafs(client: AsyncClient, devices: list, leafs: list) -> list: + """Recursive prepare leafs. + :param client: AsyncClient: Async Client object :param devices: list: ip list :param leafs: list: leaf devices :return list @@ -127,9 +132,28 @@ def parse_leafs(devices: list, leafs: list) -> list: ): continue - devices.append(leaf["ip"].strip()) + if await async_check_ip_address(client, leaf["ip"].strip()): + devices.append(leaf["ip"].strip()) if "leafs" in leaf and len(leaf["leafs"]) > 0: - devices = parse_leafs(devices, leaf["leafs"]) + devices = await async_prepare_leafs(client, devices, leaf["leafs"]) return devices + + +async def async_check_ip_address(client: AsyncClient, ip_address: str) -> bool: + """Check ip address + + :param client: AsyncClient: Async Client object + :param ip_address: str: IP address + :return bool + """ + + try: + await LuciClient(client, ip_address, timeout=DEFAULT_CHECK_TIMEOUT).topo_graph() + except LuciConnectionError: + return False + except LuciError: + pass + + return True diff --git a/custom_components/miwifi/manifest.json b/custom_components/miwifi/manifest.json index 7f197f8..641de0c 100644 --- a/custom_components/miwifi/manifest.json +++ b/custom_components/miwifi/manifest.json @@ -1,7 +1,7 @@ { "domain": "miwifi", "name": "MiWiFi", - "version": "2.7.3", + "version": "2.7.4", "documentation": "https://github.com/dmamontov/hass-miwifi/blob/main/README.md", "issue_tracker": "https://github.com/dmamontov/hass-miwifi/issues", "config_flow": true, diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 28fc660..6a96d7c 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -14,8 +14,8 @@ from custom_components.miwifi.const import DOMAIN from custom_components.miwifi.discovery import async_start_discovery -from custom_components.miwifi.exceptions import LuciError -from tests.setup import async_mock_luci_client +from custom_components.miwifi.exceptions import LuciConnectionError, LuciError +from tests.setup import MultipleSideEffect, async_mock_luci_client _LOGGER = logging.getLogger(__name__) @@ -97,3 +97,39 @@ async def test_discovery_error(hass: HomeAssistant) -> None: await hass.async_block_till_done() assert len(hass.config_entries.flow._progress) == 0 + + +async def test_discovery_invalid_device(hass: HomeAssistant) -> None: + """discovery init. + + :param hass: HomeAssistant + """ + + with patch("custom_components.miwifi.discovery.LuciClient") as mock_luci_client: + await async_mock_luci_client(mock_luci_client) + + def success() -> dict: + return json.loads(load_fixture("topo_graph_data.json")) + + def error() -> None: + raise LuciConnectionError + + def correct_error() -> None: + raise LuciError + + mock_luci_client.return_value.topo_graph = AsyncMock( + side_effect=MultipleSideEffect(success, error, correct_error) + ) + + async_start_discovery(hass) + await hass.async_block_till_done() + + assert len(hass.config_entries.flow._progress) == 1 + + for entry_id in hass.config_entries.flow._progress.keys(): + flow = hass.config_entries.flow.async_get(entry_id) + + assert flow["handler"] == DOMAIN + assert flow["step_id"] == "discovery_confirm" + assert flow["context"]["unique_id"] == "192.168.31.62" + assert flow["context"]["source"] == "integration_discovery" From 2d32f422baa9d235220c2a66b91d5d6a0cbf63dc Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Wed, 25 May 2022 14:00:12 +0300 Subject: [PATCH 2/3] Fix descovery --- hacs.json | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/hacs.json b/hacs.json index 8f790f2..7354b7d 100644 --- a/hacs.json +++ b/hacs.json @@ -1,15 +1,5 @@ { "name": "MiWiFi", "render_readme": true, - "domains": [ - "binary_sensor", - "button", - "device_tracker", - "light", - "select", - "sensor", - "switch", - "update" - ], "homeassistant": "2022.4.0" } From 1378ef03ccac20d15836639f7c5404c5ea2fd8e5 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Wed, 25 May 2022 14:01:23 +0300 Subject: [PATCH 3/3] Update documentation url --- custom_components/miwifi/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/miwifi/manifest.json b/custom_components/miwifi/manifest.json index 641de0c..5dcf95c 100644 --- a/custom_components/miwifi/manifest.json +++ b/custom_components/miwifi/manifest.json @@ -2,7 +2,7 @@ "domain": "miwifi", "name": "MiWiFi", "version": "2.7.4", - "documentation": "https://github.com/dmamontov/hass-miwifi/blob/main/README.md", + "documentation": "https://github.com/dmamontov/hass-miwifi/wiki", "issue_tracker": "https://github.com/dmamontov/hass-miwifi/issues", "config_flow": true, "requirements": [],