Skip to content

Commit 303b6df

Browse files
authored
Fix cameras that immediately terminate subscriptions (#47)
1 parent 3b2c9a6 commit 303b6df

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

onvif/managers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
RENEW_ERRORS = (ONVIFError, httpx.RequestError, XMLParseError, *SUBSCRIPTION_ERRORS)
3030
SUBSCRIPTION_RESTART_INTERVAL_ON_ERROR = dt.timedelta(seconds=40)
3131

32+
# If the camera returns a subscription with a termination time that is less than
33+
# this value, we will use this value instead to prevent subscribing over and over
34+
# again.
35+
MINIMUM_SUBSCRIPTION_SECONDS = 60.0
3236

3337
if TYPE_CHECKING:
3438
from onvif.client import ONVIFCamera, ONVIFService
@@ -116,7 +120,10 @@ def _calculate_next_renewal_call_at(self, result: Any | None) -> float:
116120
delay = termination_time - current_time
117121
else:
118122
delay = self._interval
119-
delay_seconds = delay.total_seconds() * _RENEWAL_PERCENTAGE
123+
delay_seconds = (
124+
max(delay.total_seconds(), MINIMUM_SUBSCRIPTION_SECONDS)
125+
* _RENEWAL_PERCENTAGE
126+
)
120127
logger.debug(
121128
"%s: Renew notification subscription in %s seconds",
122129
self._device.host,
@@ -141,7 +148,7 @@ def _run_restart_or_renew(self) -> None:
141148
self._renew_or_restart_subscription()
142149
)
143150

144-
async def _restart_subscription(self) -> bool:
151+
async def _restart_subscription(self) -> float:
145152
"""Restart the notify subscription assuming the camera rebooted."""
146153
self._cancel_renewals()
147154
return await self._start()

0 commit comments

Comments
 (0)