From 1601719e430db3bc29337c02dc1f32a86e49f12d Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 7 Mar 2024 18:18:05 +0100 Subject: [PATCH] Fix empty ports response (#366) --- axis/vapix/models/port_management.py | 2 +- tests/test_port_management.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/axis/vapix/models/port_management.py b/axis/vapix/models/port_management.py index d0ca664d..04f44eb6 100644 --- a/axis/vapix/models/port_management.py +++ b/axis/vapix/models/port_management.py @@ -218,7 +218,7 @@ def decode(cls, bytes_data: bytes) -> Self: api_version=data["apiVersion"], context=data["context"], method=data["method"], - data=Port.decode_to_dict(data["data"]["items"]), + data=Port.decode_to_dict(data["data"].get("items", [])), ) diff --git a/tests/test_port_management.py b/tests/test_port_management.py index 8efb4bf1..0a2ad70f 100644 --- a/tests/test_port_management.py +++ b/tests/test_port_management.py @@ -71,6 +71,16 @@ async def test_get_ports(respx_mock, io_port_management): } +async def test_get_empty_ports_response(respx_mock, io_port_management): + """Test get_ports call.""" + respx_mock.post("/axis-cgi/io/portmanagement.cgi").respond( + json=GET_EMPTY_PORTS_RESPONSE, + ) + await io_port_management.update() + assert io_port_management.initialized + assert len(io_port_management.values()) == 0 + + async def test_set_ports(respx_mock, io_port_management): """Test set_ports call.""" route = respx_mock.post("/axis-cgi/io/portmanagement.cgi") @@ -171,6 +181,13 @@ async def test_get_supported_versions(respx_mock, io_port_management): }, } +GET_EMPTY_PORTS_RESPONSE = { + "apiVersion": "1.0", + "context": "Axis library", + "method": "getPorts", + "data": {"numberOfPorts": 0}, +} + GET_SUPPORTED_VERSIONS_RESPONSE = { "apiVersion": "1.0", "context": "",