Skip to content

Commit 7f7597b

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 7f7597b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

custom_components/dahua/client.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,31 @@ 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+
raise
785+
except StopAsyncIteration:
786+
break
787+
788+
except asyncio.TimeoutError:
789+
# Re-raise to trigger reconnection in thread.py
790+
raise
772791
except Exception as exception:
773792
pass
774793
finally:

0 commit comments

Comments
 (0)