Skip to content

Commit 7e68cd4

Browse files
committed
Fix event stream disconnections with heartbeat monitoring
Set 1-hour timeout instead of default 5-minute timeout on event stream request. Implement 15-second heartbeat monitoring with asyncio.wait_for() to detect and recover from camera restarts and network issues.
1 parent c787e51 commit 7e68cd4

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

custom_components/dahua/client.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,32 @@ async def stream_events(self, on_receive, events: list, channel: int):
763763

764764
try:
765765
auth = DigestAuth(self._username, self._password, self._session)
766-
response = await auth.request("GET", url)
766+
response = await auth.request("GET", url, timeout=aiohttp.ClientTimeout(total=3600))
767767
response.raise_for_status()
768768

769769
# https://docs.aiohttp.org/en/stable/streams.html
770-
async for data, _ in response.content.iter_chunks():
771-
on_receive(data, channel)
770+
# Monitor heartbeat - timeout if no data for 15 seconds (3x heartbeat interval)
771+
chunk_iterator = response.content.iter_chunks()
772+
773+
while True:
774+
try:
775+
# Wait for next chunk with 15-second timeout
776+
data, _ = await asyncio.wait_for(
777+
chunk_iterator.__anext__(),
778+
timeout=15.0
779+
)
780+
781+
on_receive(data, channel)
782+
783+
except asyncio.TimeoutError:
784+
_LOGGER.debug("No data received for 15+ seconds, reconnecting...")
785+
raise
786+
except StopAsyncIteration:
787+
break
788+
789+
except asyncio.TimeoutError:
790+
# Re-raise to trigger reconnection in thread.py
791+
raise
772792
except Exception as exception:
773793
pass
774794
finally:

0 commit comments

Comments
 (0)