From d764c3dbc053840bc774e43630ddd8768943d183 Mon Sep 17 00:00:00 2001 From: Jean-Francois Roy Date: Tue, 6 Jun 2023 12:34:03 -0400 Subject: [PATCH] Add `delay_between_connects_seconds` parameter to `Device` init Add a `delay_between_connects_seconds` parameter to `Device` init with a default of 30 seconds. This default is available as a new public constant. --- aiobafi6/const.py | 3 +++ aiobafi6/device.py | 21 +++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/aiobafi6/const.py b/aiobafi6/const.py index 8f8984c..81cdb51 100644 --- a/aiobafi6/const.py +++ b/aiobafi6/const.py @@ -1,5 +1,8 @@ """Constants for aiobafi6.""" +# The default delay between device connection attempts. +DELAY_BETWEEN_CONNECTS_SECONDS = 30 + # The minimum device API version supported by aiobafi6. MIN_API_VERSION = 4 diff --git a/aiobafi6/device.py b/aiobafi6/device.py index 30ea43a..5b1188c 100644 --- a/aiobafi6/device.py +++ b/aiobafi6/device.py @@ -15,7 +15,7 @@ from google.protobuf.message import Message from . import wireutils -from .const import OCCUPANCY_MIN_API_VERSION +from .const import DELAY_BETWEEN_CONNECTS_SECONDS, OCCUPANCY_MIN_API_VERSION from .discovery import Service from .proto import aiobafi6_pb2 from .protoprop import ( @@ -41,7 +41,6 @@ ) _LOGGER = logging.getLogger(__name__) -_DELAY_BETWEEN_CONNECT_ATTEMPTS_SECONDS = 30 _MAX_SPEED = 7 _RECV_BUFFER_LIMIT = 4096 # No message is ever expected to be > 4K _PROPS_REQUIRED_FOR_AVAILABLE = ( @@ -93,6 +92,7 @@ def __init__( service: Service, query_interval_seconds: int = 60, ignore_volatile_props: bool = True, + delay_between_connects_seconds: int = DELAY_BETWEEN_CONNECTS_SECONDS, ): if len(service.ip_addresses) == 0 or service.port == 0: raise ValueError( @@ -120,6 +120,7 @@ def __init__( self._next_connect_ts: float = time.monotonic() self._connect_timer: t.Optional[asyncio.TimerHandle] = None self._connect_task: t.Optional[asyncio.Task] = None + self._delay_between_connects_seconds = delay_between_connects_seconds self._transport: t.Optional[asyncio.Transport] = None self._protocol: t.Optional[Protocol] = None self._query_timer: t.Optional[asyncio.TimerHandle] = None @@ -283,9 +284,7 @@ def _sched_connect_or_signal_run_fut(self): def _connect(self) -> None: self._connect_timer = None - self._next_connect_ts = ( - time.monotonic() + _DELAY_BETWEEN_CONNECT_ATTEMPTS_SECONDS - ) + self._next_connect_ts = time.monotonic() + self._delay_between_connects_seconds _LOGGER.debug( "%s: Connecting to %s:%s.", self.name, @@ -300,9 +299,7 @@ def _connect(self) -> None: ) ) connect_task.add_done_callback(self._finish_connect) - self._loop.call_later( - _DELAY_BETWEEN_CONNECT_ATTEMPTS_SECONDS, connect_task.cancel - ) + self._loop.call_later(self._delay_between_connects_seconds, connect_task.cancel) self._connect_task = connect_task def _finish_connect(self, task: asyncio.Task) -> None: @@ -375,7 +372,7 @@ def async_run(self) -> asyncio.Future: """Run the device asynchronously. A running `Device` schedules functions on the run loop to maintain a connection - to the device, send periodic property queries, and service query commits. + to the device, sends periodic property queries, and services query commits. Returns a future that will resolve when the device stops. Cancelling any future returned by this function will stop the device. @@ -424,7 +421,7 @@ def _stop(self) -> None: if self._stop_requested: return _LOGGER.debug("%s: Stopping.", self.name) - # This will cause `_sched_connect` to signal `_run_fut`. + # This will cause `_sched_connect_or_signal_run_fut` to signal `_run_fut`. self._stop_requested = True # The device is not available anymore. Dispatch device callbacks so clients can # react to the change. @@ -436,8 +433,8 @@ def _stop(self) -> None: # Otherwise, if the device is opening a connection, cancel that. elif self._connect_task is not None: self._connect_task.cancel() - # Otherwise, if `_connect` is scheduled, cancel that and call `_sched_connect` - # directly because nothing else will. + # Otherwise, if `_connect` is scheduled, cancel that and call + # `_sched_connect_or_signal_run_fut` directly because nothing else will. elif self._connect_timer is not None: self._connect_timer.cancel() self._sched_connect_or_signal_run_fut()