Skip to content

Commit 07c0016

Browse files
authored
Fix device url regex for more gateway-id (#702)
1 parent ed40567 commit 07c0016

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

pyoverkiz/models.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals
2727

2828
# <protocol>://<gatewayId>/<deviceAddress>[#<subsystemId>]
29-
DEVICE_URL_RE = r"(?P<protocol>.+)://(?P<gatewayId>\d{4}-\d{4}-\d{4})/(?P<deviceAddress>[^#]+)(#(?P<subsystemId>\d+))?"
29+
DEVICE_URL_RE = r"(?P<protocol>.+)://(?P<gatewayId>[^/]+)/(?P<deviceAddress>[^#]+)(#(?P<subsystemId>\d+))?"
3030

3131

3232
@define(init=False, kw_only=True)
@@ -144,8 +144,8 @@ class Device:
144144
enabled: bool
145145
label: str = field(repr=obfuscate_string)
146146
device_url: str = field(repr=obfuscate_id)
147-
gateway_id: str = field(repr=obfuscate_id)
148-
device_address: str = field(repr=obfuscate_id)
147+
gateway_id: str | None = field(repr=obfuscate_id)
148+
device_address: str | None = field(repr=obfuscate_id)
149149
subsystem_id: int | None = None
150150
is_sub_device: bool = False
151151
controllable_name: str
@@ -156,7 +156,7 @@ class Device:
156156
states: States
157157
type: ProductType
158158
place_oid: str | None = None
159-
protocol: Protocol = field(init=False, repr=False)
159+
protocol: Protocol | None = field(init=False, repr=False)
160160

161161
def __init__(
162162
self,
@@ -190,12 +190,15 @@ def __init__(
190190
self.type = ProductType(type)
191191
self.place_oid = place_oid
192192

193-
# Split <protocol>://<gatewayId>/<deviceAddress>[#<subsystemId>] into multiple variables
194-
match = re.search(DEVICE_URL_RE, device_url)
195-
193+
self.protocol = None
194+
self.gateway_id = None
195+
self.device_address = None
196196
self.subsystem_id = None
197197
self.is_sub_device = False
198198

199+
# Split <protocol>://<gatewayId>/<deviceAddress>[#<subsystemId>] into multiple variables
200+
match = re.search(DEVICE_URL_RE, device_url)
201+
199202
if match:
200203
self.protocol = Protocol(match.group("protocol"))
201204
self.gateway_id = match.group("gatewayId")

tests/test_models.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,39 @@ class TestDevice:
131131
2,
132132
True,
133133
),
134+
(
135+
"eliot://ELIOT-000000000000000000000000000ABCDE/00000000000000000000000000125abc",
136+
Protocol.ELIOT,
137+
"ELIOT-000000000000000000000000000ABCDE",
138+
"00000000000000000000000000125abc",
139+
None,
140+
False,
141+
),
142+
(
143+
"eliot://ELIOT-000000000000000000000000000ABCDE/00000000000000000000000000125abc#1",
144+
Protocol.ELIOT,
145+
"ELIOT-000000000000000000000000000ABCDE",
146+
"00000000000000000000000000125abc",
147+
1,
148+
False,
149+
),
150+
# Wrong device urls:
151+
(
152+
"foo://whatever-blah/12",
153+
Protocol.UNKNOWN,
154+
"whatever-blah",
155+
"12",
156+
None,
157+
False,
158+
),
159+
(
160+
"foo://whatever",
161+
None,
162+
None,
163+
None,
164+
None,
165+
False,
166+
),
134167
],
135168
)
136169
def test_base_url_parsing(

0 commit comments

Comments
 (0)