Skip to content

Commit

Permalink
Add props
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Mar 4, 2024
1 parent bcc69e5 commit 38af54c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions teslemetry_stream/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
)
Expand All @@ -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."""
Expand All @@ -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:
Expand All @@ -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."""
Expand Down

0 comments on commit 38af54c

Please sign in to comment.