Skip to content

Commit

Permalink
Fix device url regex for more gateway-id (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyroDev authored Dec 12, 2022
1 parent 1b9e3f4 commit d37d7ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pyoverkiz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# pylint: disable=unused-argument, too-many-instance-attributes, too-many-locals

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


@define(init=False, kw_only=True)
Expand Down Expand Up @@ -144,8 +144,8 @@ class Device:
enabled: bool
label: str = field(repr=obfuscate_string)
device_url: str = field(repr=obfuscate_id)
gateway_id: str = field(repr=obfuscate_id)
device_address: str = field(repr=obfuscate_id)
gateway_id: str | None = field(repr=obfuscate_id)
device_address: str | None = field(repr=obfuscate_id)
subsystem_id: int | None = None
is_sub_device: bool = False
controllable_name: str
Expand All @@ -156,7 +156,7 @@ class Device:
states: States
type: ProductType
place_oid: str | None = None
protocol: Protocol = field(init=False, repr=False)
protocol: Protocol | None = field(init=False, repr=False)

def __init__(
self,
Expand Down Expand Up @@ -190,12 +190,15 @@ def __init__(
self.type = ProductType(type)
self.place_oid = place_oid

# Split <protocol>://<gatewayId>/<deviceAddress>[#<subsystemId>] into multiple variables
match = re.search(DEVICE_URL_RE, device_url)

self.protocol = None
self.gateway_id = None
self.device_address = None
self.subsystem_id = None
self.is_sub_device = False

# Split <protocol>://<gatewayId>/<deviceAddress>[#<subsystemId>] into multiple variables
match = re.search(DEVICE_URL_RE, device_url)

if match:
self.protocol = Protocol(match.group("protocol"))
self.gateway_id = match.group("gatewayId")
Expand Down
33 changes: 33 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,39 @@ class TestDevice:
2,
True,
),
(
"eliot://ELIOT-000000000000000000000000000ABCDE/00000000000000000000000000125abc",
Protocol.ELIOT,
"ELIOT-000000000000000000000000000ABCDE",
"00000000000000000000000000125abc",
None,
False,
),
(
"eliot://ELIOT-000000000000000000000000000ABCDE/00000000000000000000000000125abc#1",
Protocol.ELIOT,
"ELIOT-000000000000000000000000000ABCDE",
"00000000000000000000000000125abc",
1,
False,
),
# Wrong device urls:
(
"foo://whatever-blah/12",
Protocol.UNKNOWN,
"whatever-blah",
"12",
None,
False,
),
(
"foo://whatever",
None,
None,
None,
None,
False,
),
],
)
def test_base_url_parsing(
Expand Down

0 comments on commit d37d7ca

Please sign in to comment.