Skip to content

Commit 696e662

Browse files
authored
Fix event stream timeout at session level (#508)
Moves timeout fix from request level (#501) to session level, which is more appropriate since the session is created specifically for the Dahua client. Changes: - Add timeout=ClientTimeout(total=None) to ClientSession creation in __init__.py - Revert request-level timeout fix from client.py The session-level fix prevents the default 5-minute timeout from disconnecting infinite event streams while keeping regular API requests properly scoped. Tested in production with no timeout errors.
1 parent 2427d07 commit 696e662

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

custom_components/dahua/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from homeassistant.components.tag import async_scan_tag
1313
import hashlib
1414

15-
from aiohttp import ClientError, ClientResponseError, ClientSession, TCPConnector
15+
from aiohttp import ClientError, ClientResponseError, ClientSession, ClientTimeout, TCPConnector
1616
from homeassistant.config_entries import ConfigEntry
1717
from homeassistant.core import CALLBACK_TYPE, HomeAssistant
1818
from homeassistant.exceptions import ConfigEntryNotReady, PlatformNotReady
@@ -98,7 +98,8 @@ def __init__(self, hass: HomeAssistant, events: list, address: str, port: int, r
9898
"""Initialize the coordinator."""
9999
# Self signed certs are used over HTTPS so we'll disable SSL verification
100100
connector = TCPConnector(enable_cleanup_closed=True, ssl=SSL_CONTEXT)
101-
self._session = ClientSession(connector=connector)
101+
# Disable total timeout for infinite event stream (default 5-minute timeout causes disconnects)
102+
self._session = ClientSession(connector=connector, timeout=ClientTimeout(total=None))
102103

103104
# The client used to communicate with Dahua devices
104105
self.client: DahuaClient = DahuaClient(username, password, address, port, rtsp_port, self._session)

custom_components/dahua/client.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -759,18 +759,13 @@ async def stream_events(self, on_receive, events: list, channel: int):
759759
"""
760760
# Use codes=[All] for all codes
761761
codes = ",".join(events)
762-
heartbeat_interval = 5
763-
url = "{0}/cgi-bin/eventManager.cgi?action=attach&codes=[{1}]&heartbeat={2}".format(
764-
self._base, codes, heartbeat_interval
765-
)
762+
url = "{0}/cgi-bin/eventManager.cgi?action=attach&codes=[{1}]&heartbeat=5".format(self._base, codes)
766763
if self._username is not None and self._password is not None:
767764
response = None
768765

769766
try:
770767
auth = DigestAuth(self._username, self._password, self._session)
771-
# Disable timeout for infinite event stream (fixes 5-minute disconnection)
772-
timeout = aiohttp.ClientTimeout(total=None)
773-
response = await auth.request("GET", url, timeout=timeout)
768+
response = await auth.request("GET", url)
774769
response.raise_for_status()
775770

776771
# https://docs.aiohttp.org/en/stable/streams.html

0 commit comments

Comments
 (0)