From 38af54ce47654209f3dc83bec1cd527808243e84 Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 5 Mar 2024 09:30:09 +1000 Subject: [PATCH] Add props --- teslemetry_stream/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/teslemetry_stream/__init__.py b/teslemetry_stream/__init__.py index 35b2a37..5d1654b 100644 --- a/teslemetry_stream/__init__.py +++ b/teslemetry_stream/__init__.py @@ -39,6 +39,8 @@ class TeslemetryStream: _update_lock = asyncio.Lock() _response: aiohttp.ClientResponse | None = None _listeners: dict[Callable, Callable] = {} + connected = False + active = False def __init__( self, @@ -151,6 +153,7 @@ async def connect(self) -> None: raise_for_status=True, timeout=aiohttp.ClientTimeout(connect=5, sock_read=30, total=None), ) + self.connected = True LOGGER.debug( "Connected to %s with status %s", self._response.url, self._response.status ) @@ -161,6 +164,7 @@ def close(self) -> None: LOGGER.debug("Disconnecting from %s", self.server) self._response.close() self._response = None + self.connected = False async def __aenter__(self) -> "TeslemetryStream": """Connect and listen Server-Sent Event.""" @@ -177,6 +181,7 @@ def __aiter__(self): async def __anext__(self) -> dict: """Return next event.""" + self.active = True if not self._response: await self.connect() try: @@ -194,6 +199,8 @@ async def __anext__(self) -> dict: continue except aiohttp.ClientConnectionError as error: raise StopAsyncIteration from error + finally: + self.active = False def async_add_listener(self, callback: Callable) -> Callable[[], None]: """Listen for data updates."""