Skip to content

Commit

Permalink
Fix QRProvisioningInformation.from_dict() (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 authored May 18, 2024
1 parent 333ca9c commit ceb54cf
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 32 deletions.
90 changes: 90 additions & 0 deletions test/model/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,96 @@ async def test_provision_smart_start_node_qr_info(controller, uuid4, mock_comman
"messageId": uuid4,
}

ack_commands.clear()

# Test that when supported protocols aren't included, it should be None
# instead of an empty list.
provisioning_entry = controller_pkg.QRProvisioningInformation(
dsk="test1",
security_classes=[SecurityClass.S2_UNAUTHENTICATED],
requested_security_classes=[SecurityClass.S2_UNAUTHENTICATED],
status=ProvisioningEntryStatus.INACTIVE,
version=QRCodeVersion.SMART_START,
generic_device_class=1,
specific_device_class=2,
installer_icon_type=3,
manufacturer_id=4,
product_type=5,
product_id=6,
application_version="test2",
max_inclusion_request_interval=7,
uuid="test3",
supported_protocols=None,
)
await controller.async_provision_smart_start_node(provisioning_entry)

qr_entry = {
"version": 1,
"securityClasses": [0],
"requestedSecurityClasses": [0],
"status": 1,
"dsk": "test1",
"genericDeviceClass": 1,
"specificDeviceClass": 2,
"installerIconType": 3,
"manufacturerId": 4,
"productType": 5,
"productId": 6,
"applicationVersion": "test2",
"maxInclusionRequestInterval": 7,
"uuid": "test3",
}

assert len(ack_commands) == 1
assert ack_commands[0] == {
"command": "controller.provision_smart_start_node",
"entry": qr_entry,
"messageId": uuid4,
}

assert (
qr_entry
== controller_pkg.QRProvisioningInformation.from_dict(qr_entry).to_dict()
)

assert controller_pkg.QRProvisioningInformation(
dsk="test1",
security_classes=[SecurityClass.S2_UNAUTHENTICATED],
requested_security_classes=[SecurityClass.S2_UNAUTHENTICATED],
status=ProvisioningEntryStatus.INACTIVE,
version=QRCodeVersion.SMART_START,
generic_device_class=1,
specific_device_class=2,
installer_icon_type=3,
manufacturer_id=4,
product_type=5,
product_id=6,
application_version="test2",
max_inclusion_request_interval=7,
uuid="test3",
supported_protocols=[Protocols.ZWAVE],
additional_properties={"test": "foo"},
) == controller_pkg.QRProvisioningInformation.from_dict(
{
"version": 1,
"securityClasses": [0],
"requestedSecurityClasses": [0],
"status": 1,
"dsk": "test1",
"genericDeviceClass": 1,
"specificDeviceClass": 2,
"installerIconType": 3,
"manufacturerId": 4,
"productType": 5,
"productId": 6,
"applicationVersion": "test2",
"maxInclusionRequestInterval": 7,
"uuid": "test3",
"supportedProtocols": [0],
"test": "foo",
}
)

# Test that S2 QR Code can't be used with `async_provision_smart_start_node`
provisioning_entry = controller_pkg.QRProvisioningInformation(
dsk="test1",
Expand Down
68 changes: 36 additions & 32 deletions zwave_js_server/model/controller/inclusion_and_provisioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ def from_dict(cls, data: dict[str, Any]) -> ProvisioningEntry:
security_classes=[
SecurityClass(sec_cls) for sec_cls in data["securityClasses"]
],
additional_properties={
k: v
for k, v in data.items()
if k
not in {"dsk", "securityClasses", "requestedSecurityClasses", "status"}
},
)
if additional_properties := {
k: v
for k, v in data.items()
if k not in ("dsk", "securityClasses", "requestedSecurityClasses", "status")
}:
cls_instance.additional_properties = additional_properties
if "requestedSecurityClasses" in data:
cls_instance.requested_security_classes = [
SecurityClass(sec_cls) for sec_cls in data["requestedSecurityClasses"]
Expand Down Expand Up @@ -148,6 +148,12 @@ def to_dict(self) -> dict[str, Any]:
@classmethod
def from_dict(cls, data: dict[str, Any]) -> QRProvisioningInformation:
"""Return QRProvisioningInformation from data dict."""
supported_protocols: list[Protocols] | None = None
if "supportedProtocols" in data:
supported_protocols = [
Protocols(supported_protocol)
for supported_protocol in data["supportedProtocols"]
]
cls_instance = cls(
version=QRCodeVersion(data["version"]),
security_classes=[
Expand All @@ -163,33 +169,31 @@ def from_dict(cls, data: dict[str, Any]) -> QRProvisioningInformation:
application_version=data["applicationVersion"],
max_inclusion_request_interval=data.get("maxInclusionRequestInterval"),
uuid=data.get("uuid"),
supported_protocols=[
Protocols(supported_protocol)
for supported_protocol in data.get("supportedProtocols", [])
],
additional_properties={
k: v
for k, v in data.items()
if k
not in {
"version",
"securityClasses",
"requestedSecurityClasses",
"dsk",
"genericDeviceClass",
"specificDeviceClass",
"installerIconType",
"manufacturerId",
"productType",
"productId",
"applicationVersion",
"maxInclusionRequestInterval",
"uuid",
"supportedProtocols",
"status",
}
},
supported_protocols=supported_protocols,
)
if additional_properties := {
k: v
for k, v in data.items()
if k
not in (
"version",
"securityClasses",
"requestedSecurityClasses",
"dsk",
"genericDeviceClass",
"specificDeviceClass",
"installerIconType",
"manufacturerId",
"productType",
"productId",
"applicationVersion",
"maxInclusionRequestInterval",
"uuid",
"supportedProtocols",
"status",
)
}:
cls_instance.additional_properties = additional_properties
if "requestedSecurityClasses" in data:
cls_instance.requested_security_classes = [
SecurityClass(sec_cls) for sec_cls in data["requestedSecurityClasses"]
Expand Down

0 comments on commit ceb54cf

Please sign in to comment.