Skip to content

Commit

Permalink
Speed up discovery by leveraging asyncio even more
Browse files Browse the repository at this point in the history
  • Loading branch information
Darsstar committed Jun 3, 2023
1 parent 3662429 commit cc0aa34
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"aiohttp>=3.5.4, <4",
"async_timeout>=4.0.2",
"voluptuous>=0.11.5",
"mypy_extensions",
],
setup_requires=[
"setuptools_scm",
Expand Down
44 changes: 33 additions & 11 deletions solax/discovery.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import Type

from solax.http_client import all_variations
Expand Down Expand Up @@ -26,18 +27,13 @@ class DiscoveryError(Exception):
async def discover(host, port, pwd="") -> Inverter:
failures: list = []
clients = all_variations(host, port, pwd)
for client_name, client in clients.items():
try:
response = await client.request()
except InverterError as ex:
failures.append(
(
client_name,
ex,
)
)
continue
pending = []

async def identify_inverter(sleep, client_name, client):
await asyncio.sleep(sleep) # don't spam the inverter
response = await client.request()
for inverter_class in REGISTRY:
await asyncio.sleep(0)
try:
inverter = inverter_class(client)
if inverter.identify(response):
Expand All @@ -58,6 +54,32 @@ async def discover(host, port, pwd="") -> Inverter:
ex,
)
)

for sleep, (name, client) in enumerate(clients.items()):
pending.append(asyncio.create_task(
identify_inverter(sleep, name, client),
name=name,
))

while pending:
done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)

for task in done:
if task.cancelled():
continue

try:
return await task
except Exception as ex:
failures.append(
(
task.get_name(),
ex,
)
)



msg = (
"Unable to connect to the inverter at "
f"host={host} port={port}, or your inverter is not supported yet.\n"
Expand Down

0 comments on commit cc0aa34

Please sign in to comment.