From 7b9c636cfcd7ff09b0a4142bf541388308b0a576 Mon Sep 17 00:00:00 2001 From: Julius Mittenzwei Date: Fri, 22 May 2020 10:13:30 +0200 Subject: [PATCH] Added setter serial number. --- .../frames/frame_get_all_nodes_information.py | 13 +++++++++++ pyvlx/frames/frame_get_node_information.py | 11 +++++++++- ...rame_get_all_nodes_information_ntf_test.py | 22 ++++++++++++++----- test/frame_get_node_information_ntf_test.py | 22 ++++++++++++++----- test/node_helper_test.py | 20 ++++++++--------- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/pyvlx/frames/frame_get_all_nodes_information.py b/pyvlx/frames/frame_get_all_nodes_information.py index 17467ab7..7d923292 100644 --- a/pyvlx/frames/frame_get_all_nodes_information.py +++ b/pyvlx/frames/frame_get_all_nodes_information.py @@ -7,6 +7,7 @@ from pyvlx.const import Command, NodeTypeWithSubtype, NodeVariation, Velocity from pyvlx.parameter import Parameter from pyvlx.string_helper import bytes_to_string, string_to_bytes +from pyvlx.exception import PyVLXException from .frame import FrameBase @@ -93,6 +94,18 @@ def serial_number(self): return None return ":".join("{:02x}".format(c) for c in self._serial_number) + @serial_number.setter + def serial_number(self, serial_number): + """Set serial number.""" + if serial_number is None: + self._serial_number = bytes(8) + return + self._serial_number = b'' + for elem in (serial_number.split(":")): + self._serial_number += bytes.fromhex(elem) + if len(self._serial_number) != 8: + raise PyVLXException("could_not_parse_serial_number") + def get_payload(self): """Return Payload.""" payload = bytes() diff --git a/pyvlx/frames/frame_get_node_information.py b/pyvlx/frames/frame_get_node_information.py index 1fcd7605..8fac03c8 100644 --- a/pyvlx/frames/frame_get_node_information.py +++ b/pyvlx/frames/frame_get_node_information.py @@ -7,6 +7,7 @@ from pyvlx.const import Command, NodeTypeWithSubtype, NodeVariation, Velocity from pyvlx.parameter import Parameter from pyvlx.string_helper import bytes_to_string, string_to_bytes +from pyvlx.exception import PyVLXException from .frame import FrameBase @@ -109,7 +110,15 @@ def serial_number(self): @serial_number.setter def serial_number(self, serial_number): - self._serial_number = serial_number + """Set serial number.""" + if serial_number is None: + self._serial_number = bytes(8) + return + self._serial_number = b'' + for elem in (serial_number.split(":")): + self._serial_number += bytes.fromhex(elem) + if len(self._serial_number) != 8: + raise PyVLXException("could_not_parse_serial_number") def get_payload(self): """Return Payload.""" diff --git a/test/frame_get_all_nodes_information_ntf_test.py b/test/frame_get_all_nodes_information_ntf_test.py index ddf5b116..af41d18d 100644 --- a/test/frame_get_all_nodes_information_ntf_test.py +++ b/test/frame_get_all_nodes_information_ntf_test.py @@ -7,6 +7,7 @@ from pyvlx.frame_creation import frame_from_raw from pyvlx.frames import FrameGetAllNodesInformationNotification from pyvlx.parameter import Position +from pyvlx.exception import PyVLXException class TestFrameGetAllNodesInformationNotification(unittest.TestCase): @@ -38,9 +39,8 @@ def test_bytes(self): frame.node_variation = NodeVariation.TOPHUNG frame.power_mode = 1 frame.build_number = 7 - frame._serial_number = ( # pylint: disable=protected-access - b"\x01\x02\x03\x04\x05\x06\x06\x08" - ) + frame.serial_number = "01:02:03:04:05:06:06:08" + frame.state = 1 frame.current_position = Position(position=12) frame.target = Position(position=123) @@ -104,12 +104,22 @@ def test_str(self): def test_serial_number(self): """Test serial number property.""" frame = FrameGetAllNodesInformationNotification() - frame._serial_number = ( # pylint: disable=protected-access - b"\x01\x02\x03\x04\x05\x06\x06\x08" - ) + frame.serial_number = "01:02:03:04:05:06:06:08" self.assertEqual(frame.serial_number, "01:02:03:04:05:06:06:08") def test_serial_number_none(self): """Test serial number property with no value set.""" frame = FrameGetAllNodesInformationNotification() + frame.serial_number = None + self.assertEqual(frame.serial_number, None) + + def test_serial_number_not_set(self): + """Test serial number property with not set.""" + frame = FrameGetAllNodesInformationNotification() self.assertEqual(frame.serial_number, None) + + def test_wrong_serial_number(self): + """Test setting a wrong serial number.""" + frame = FrameGetAllNodesInformationNotification() + with self.assertRaises(PyVLXException): + frame.serial_number = "01:02:03:04:05:06:06" diff --git a/test/frame_get_node_information_ntf_test.py b/test/frame_get_node_information_ntf_test.py index 4312dd79..845eee4e 100644 --- a/test/frame_get_node_information_ntf_test.py +++ b/test/frame_get_node_information_ntf_test.py @@ -7,6 +7,7 @@ from pyvlx.frame_creation import frame_from_raw from pyvlx.frames import FrameGetNodeInformationNotification from pyvlx.parameter import Position +from pyvlx.exception import PyVLXException class TestFrameGetNodeInformationNotification(unittest.TestCase): @@ -38,9 +39,8 @@ def test_bytes(self): frame.node_variation = NodeVariation.TOPHUNG frame.power_mode = 1 frame.build_number = 7 - frame._serial_number = ( # pylint: disable=protected-access - b"\x01\x02\x03\x04\x05\x06\x06\x08" - ) + frame.serial_number = "01:02:03:04:05:06:06:08" + frame.state = 1 frame.current_position = Position(position=12) frame.target = Position(position=123) @@ -104,12 +104,22 @@ def test_str(self): def test_serial_number(self): """Test serial number property.""" frame = FrameGetNodeInformationNotification() - frame._serial_number = ( # pylint: disable=protected-access - b"\x01\x02\x03\x04\x05\x06\x06\x08" - ) + frame.serial_number = "01:02:03:04:05:06:06:08" self.assertEqual(frame.serial_number, "01:02:03:04:05:06:06:08") def test_serial_number_none(self): """Test serial number property with no value set.""" frame = FrameGetNodeInformationNotification() + frame.serial_number = None + self.assertEqual(frame.serial_number, None) + + def test_serial_number_not_set(self): + """Test serial number property with not set.""" + frame = FrameGetNodeInformationNotification() self.assertEqual(frame.serial_number, None) + + def test_wrong_serial_number(self): + """Test setting a wrong serial number.""" + frame = FrameGetNodeInformationNotification() + with self.assertRaises(PyVLXException): + frame.serial_number = "01:02:03:04:05:06:06" diff --git a/test/node_helper_test.py b/test/node_helper_test.py index b577b7f8..b362631e 100644 --- a/test/node_helper_test.py +++ b/test/node_helper_test.py @@ -18,7 +18,7 @@ def test_window(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.WINDOW_OPENER - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -37,7 +37,7 @@ def test_window_with_rain_sensor(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.WINDOW_OPENER_WITH_RAIN_SENSOR - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -57,7 +57,7 @@ def test_blind(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.EXTERIOR_VENETIAN_BLIND - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -76,7 +76,7 @@ def test_roller_shutter(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.ROLLER_SHUTTER - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -95,7 +95,7 @@ def test_garage_door(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.GARAGE_DOOR_OPENER - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -114,7 +114,7 @@ def test_gate(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.GATE_OPENER - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -133,7 +133,7 @@ def test_gate_with_angular_position(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.GATE_OPENER_ANGULAR_POSITION - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -152,7 +152,7 @@ def test_blade(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.BLADE_OPENER - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual( @@ -171,7 +171,7 @@ def test_no_type(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.NO_TYPE - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() self.assertEqual(convert_frame_to_node(pyvlx, frame), None) @@ -181,7 +181,7 @@ def test_light(self): frame.node_id = 23 frame.name = "Fnord23" frame.node_type = NodeTypeWithSubtype.LIGHT - frame.serial_number = bytes.fromhex("aa bb aa bb aa bb aa 23") + frame.serial_number = "aa:bb:aa:bb:aa:bb:aa:23" pyvlx = PyVLX() node = convert_frame_to_node(pyvlx, frame) self.assertEqual(