Skip to content

Commit

Permalink
Fix descovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed May 25, 2022
1 parent 128a2af commit 248f28a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 10 deletions.
1 change: 1 addition & 0 deletions custom_components/miwifi/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 31 additions & 7 deletions custom_components/miwifi/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion custom_components/miwifi/manifest.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
40 changes: 38 additions & 2 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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"

0 comments on commit 248f28a

Please sign in to comment.