Skip to content

Commit

Permalink
Ensure multiple asyncio discoveries can combine data (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Dec 7, 2021
1 parent 52d4f71 commit e47c528
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
11 changes: 3 additions & 8 deletions flux_led/aioscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
import contextlib
import logging
import time
from typing import Callable, Dict, List, Optional, Tuple, cast
from typing import Callable, List, Optional, Tuple, cast

from .scanner import BulbScanner, FluxLEDDiscovery

_LOGGER = logging.getLogger(__name__)


MAX_UPDATES_WITHOUT_RESPONSE = 4


class LEDENETDiscovery(asyncio.DatagramProtocol):
def __init__(
self,
Expand Down Expand Up @@ -77,11 +74,10 @@ async def async_scan(
sock = self._create_socket()
destination = self._destination_from_address(address)
found_all_future: "asyncio.Future[bool]" = asyncio.Future()
response_list: Dict[str, FluxLEDDiscovery] = {}

def _on_response(data: bytes, addr: Tuple[str, int]) -> None:
_LOGGER.debug("discover: %s <= %s", addr, data)
if self._process_response(data, addr, address, response_list):
if self._process_response(data, addr, address, self._discoveries):
with contextlib.suppress(asyncio.InvalidStateError):
found_all_future.set_result(True)

Expand All @@ -100,5 +96,4 @@ def _on_response(data: bytes, addr: Tuple[str, int]) -> None:
finally:
transport.close()

self.found_bulbs = self._found_bulbs(response_list)
return list(self.found_bulbs)
return self.found_bulbs
17 changes: 7 additions & 10 deletions flux_led/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ class BulbScanner:
BROADCAST_ADDRESS = "<broadcast>"

def __init__(self) -> None:
self.found_bulbs: List[FluxLEDDiscovery] = []
self._discoveries: Dict[str, FluxLEDDiscovery] = {}

@property
def found_bulbs(self) -> List[FluxLEDDiscovery]:
"""Return only complete bulb discoveries."""
return [info for info in self._discoveries.values() if info["id"]]

def getBulbInfoByID(self, id: str) -> FluxLEDDiscovery:
for b in self.found_bulbs:
Expand Down Expand Up @@ -167,12 +172,6 @@ def _process_data(
elif "," in decoded_data:
_process_discovery_message(data, decoded_data)

def _found_bulbs(
self, response_list: Dict[str, FluxLEDDiscovery]
) -> List[FluxLEDDiscovery]:
"""Return only complete bulb discoveries."""
return [info for info in response_list.values() if info["id"]]

def send_discovery_messages(
self,
sender: Union[socket.socket, asyncio.DatagramTransport],
Expand All @@ -195,7 +194,6 @@ def scan(
destination = self._destination_from_address(address)
# set the time at which we will quit the search
quit_time = time.monotonic() + timeout
response_list: Dict[str, FluxLEDDiscovery] = {}
found_all = False
# outer loop for query send
while not found_all:
Expand Down Expand Up @@ -223,9 +221,8 @@ def scan(
except socket.timeout:
continue

if self._process_response(data, addr, address, response_list):
if self._process_response(data, addr, address, self._discoveries):
found_all = True
break

self.found_bulbs = self._found_bulbs(response_list)
return self.found_bulbs

0 comments on commit e47c528

Please sign in to comment.