Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions custom_components/dahua/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,12 +763,32 @@ async def stream_events(self, on_receive, events: list, channel: int):

try:
auth = DigestAuth(self._username, self._password, self._session)
response = await auth.request("GET", url)
response = await auth.request("GET", url, timeout=aiohttp.ClientTimeout(total=3600))
response.raise_for_status()

# https://docs.aiohttp.org/en/stable/streams.html
async for data, _ in response.content.iter_chunks():
on_receive(data, channel)
# Monitor heartbeat - timeout if no data for 15 seconds (3x heartbeat interval)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't had a chance to manually test this. How confident are you in this change? Did you test it locally?

chunk_iterator = response.content.iter_chunks()

while True:
try:
# Wait for next chunk with 15-second timeout
data, _ = await asyncio.wait_for(
chunk_iterator.__anext__(),
timeout=15.0
)

on_receive(data, channel)

except asyncio.TimeoutError:
_LOGGER.debug("No data received for 15+ seconds, reconnecting...")
raise
except StopAsyncIteration:
break

except asyncio.TimeoutError:
# Re-raise to trigger reconnection in thread.py
raise
except Exception as exception:
pass
finally:
Expand Down
Loading