Skip to content

Commit

Permalink
Create futures using loop.create_future() (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 10, 2023
1 parent 7ec459a commit fd6ca97
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions flux_led/aiodevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ def __init__(
) -> None:
"""Init and setup the bulb."""
super().__init__(ipaddr, port, timeout, discovery)
loop = asyncio.get_running_loop()
self._connect_lock = asyncio.Lock()
self._aio_protocol: Optional[AIOLEDENETProtocol] = None
self._get_time_lock: asyncio.Lock = asyncio.Lock()
self._get_time_future: Optional[asyncio.Future[bool]] = None
self._get_timers_lock: asyncio.Lock = asyncio.Lock()
self._get_timers_future: Optional[asyncio.Future[bool]] = None
self._timers: Optional[List[LedTimer]] = None
self._power_restore_future: "asyncio.Future[bool]" = asyncio.Future()
self._power_restore_future: "asyncio.Future[bool]" = loop.create_future()
self._device_config_lock: asyncio.Lock = asyncio.Lock()
self._device_config_future: asyncio.Future[bool] = asyncio.Future()
self._remote_config_future: asyncio.Future[bool] = asyncio.Future()
self._device_config_future: asyncio.Future[bool] = loop.create_future()
self._remote_config_future: asyncio.Future[bool] = loop.create_future()
self._device_config_setup = False
self._power_state_lock = asyncio.Lock()
self._power_state_futures: List["asyncio.Future[bool]"] = []
Expand All @@ -97,7 +98,7 @@ def __init__(
self._last_update_time: float = NEVER_TIME
self._power_restore_state: Optional[PowerRestoreStates] = None
self._buffer = b""
self.loop = asyncio.get_running_loop()
self.loop = loop

@property
def power_restore_states(self) -> Optional[PowerRestoreStates]:
Expand Down Expand Up @@ -162,7 +163,7 @@ async def _async_device_config_setup(self) -> None:
return

if self._device_config_setup:
self._device_config_future = asyncio.Future()
self._device_config_future = self.loop.create_future()
self._device_config_setup = True

assert isinstance(self._protocol, ALL_ADDRESSABLE_PROTOCOLS)
Expand Down Expand Up @@ -210,9 +211,9 @@ async def _async_set_power_state(
self, state: bool, accept_any_power_state_response: bool
) -> bool:
assert self._protocol is not None
power_state_future: "asyncio.Future[bool]" = asyncio.Future()
power_state_future: "asyncio.Future[bool]" = self.loop.create_future()
state_future: "asyncio.Future[Union[LEDENETRawState, LEDENETOriginalRawState]]" = (
asyncio.Future()
self.loop.create_future()
)
self._power_state_futures.append(power_state_future)
self._state_futures.append(state_future)
Expand All @@ -237,7 +238,7 @@ async def _async_set_power_state(
"%s: Bulb failed to respond, sending state query", self.ipaddr
)
if state_future.done():
state_future = asyncio.Future()
state_future = self.loop.create_future()
self._state_futures.append(state_future)
pending: "List[asyncio.Future[Any]]" = [state_future]
if not power_state_future.done():
Expand Down Expand Up @@ -622,7 +623,7 @@ async def async_get_time(self) -> Optional[datetime]:
assert self._protocol is not None
await self._async_send_msg(self._protocol.construct_get_time())
async with self._get_time_lock:
self._get_time_future = asyncio.Future()
self._get_time_future = self.loop.create_future()
try:
async with asyncio_timeout(self.timeout):
await self._get_time_future
Expand All @@ -639,7 +640,7 @@ async def async_get_timers(self) -> Optional[List[LedTimer]]:
return led_timers
await self._async_send_msg(self._protocol.construct_get_timers())
async with self._get_timers_lock:
self._get_timers_future = asyncio.Future()
self._get_timers_future = self.loop.create_future()
try:
async with asyncio_timeout(self.timeout):
await self._get_timers_future
Expand Down Expand Up @@ -855,7 +856,7 @@ async def _async_determine_protocol(self) -> None:
async with self._connect_lock:
await self._async_connect()
assert self._aio_protocol is not None
self._determine_protocol_future = asyncio.Future()
self._determine_protocol_future = self.loop.create_future()
self._aio_protocol.write(protocol.construct_state_query())
try:
async with asyncio_timeout(self.timeout):
Expand Down
2 changes: 1 addition & 1 deletion flux_led/aioscanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async def async_scan(
"""Discover LEDENET."""
sock = self._create_socket()
destination = self._destination_from_address(address)
found_all_future: "asyncio.Future[bool]" = asyncio.Future()
found_all_future: "asyncio.Future[bool]" = self.loop.create_future()

def _on_response(data: bytes, addr: Tuple[str, int]) -> None:
_LOGGER.debug("discover: %s <= %s", addr, data)
Expand Down

0 comments on commit fd6ca97

Please sign in to comment.