Skip to content

Commit 9137c90

Browse files
authored
Handle cameras that generate broken subscription urls (#36)
1 parent bb35c98 commit 9137c90

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

onvif/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from onvif.definition import SERVICES
3030
from onvif.exceptions import ONVIFAuthError, ONVIFError, ONVIFTimeoutError
3131

32-
from .util import extract_subcodes_as_strings, is_auth_error, stringify_onvif_error
32+
from .util import normalize_url, stringify_onvif_error
3333

3434
logger = logging.getLogger("onvif")
3535
logging.basicConfig(level=logging.INFO)
@@ -577,7 +577,7 @@ async def _start(self) -> float:
577577
# pylint: disable=protected-access
578578
device.xaddrs[
579579
"http://www.onvif.org/ver10/events/wsdl/NotificationConsumer"
580-
] = result.SubscriptionReference.Address._value_1
580+
] = normalize_url(result.SubscriptionReference.Address._value_1)
581581
# Create subscription manager
582582
# 5.2.3 BASIC NOTIFICATION INTERFACE - NOTIFY
583583
# Call SetSynchronizationPoint to generate a notification message
@@ -657,7 +657,7 @@ async def _start(self) -> float:
657657
# pylint: disable=protected-access
658658
device.xaddrs[
659659
"http://www.onvif.org/ver10/events/wsdl/PullPointSubscription"
660-
] = result.SubscriptionReference.Address._value_1
660+
] = normalize_url(result.SubscriptionReference.Address._value_1)
661661
# Create subscription manager
662662
self._subscription = await device.create_subscription_service(
663663
"PullPointSubscription"

onvif/util.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22
from __future__ import annotations
33

44
from typing import Any
5+
from urllib.parse import urlparse, urlunparse
56

67
from zeep.exceptions import Fault
78

89

10+
def normalize_url(url: str) -> str:
11+
"""Normalize URL.
12+
13+
Some cameras respond with <wsa5:Address>http://192.168.1.106:8106:8106/onvif/Subscription?Idx=43</wsa5:Address>
14+
https://github.com/home-assistant/core/issues/92603#issuecomment-1537213126
15+
"""
16+
parsed = urlparse(url)
17+
if "[" not in parsed.netloc and parsed.netloc.count(":") > 1:
18+
net_location = parsed.netloc.split(":", 3)
19+
net_location.pop()
20+
return urlunparse(parsed._replace(netloc=":".join(net_location)))
21+
return url
22+
23+
924
def extract_subcodes_as_strings(subcodes: Any) -> list[str]:
1025
"""Stringify ONVIF subcodes."""
1126
if isinstance(subcodes, list):

0 commit comments

Comments
 (0)