From a9f15bd0ab5be6fbce3146c70509fe0e131264f2 Mon Sep 17 00:00:00 2001 From: Ilia Sotnikov Date: Tue, 17 Dec 2024 00:39:45 +0300 Subject: [PATCH 1/5] fix: Disallow storing empty device GUID * Under not yet identified circumstances the device ID might be empty string provided by `G90Alarm.get_host_info()` method - `G90DeviceNotifications.device_id` property will ignore that when being set --- src/pyg90alarm/device_notifications.py | 8 ++++++++ tests/test_notifications.py | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/pyg90alarm/device_notifications.py b/src/pyg90alarm/device_notifications.py index aaba80e..93d80a7 100644 --- a/src/pyg90alarm/device_notifications.py +++ b/src/pyg90alarm/device_notifications.py @@ -471,4 +471,12 @@ def device_id(self) -> Optional[str]: @device_id.setter def device_id(self, device_id: str) -> None: + # Under not yet identified circumstances the device ID might be empty + # string provided by :meth:`G90Alarm.get_host_info` - disallow that + if not device_id or not len(device_id.strip()): + _LOGGER.debug( + 'Device ID is empty or contains whitespace only, not setting' + ) + return + self._device_id = device_id diff --git a/tests/test_notifications.py b/tests/test_notifications.py index ebdfe82..78374ee 100644 --- a/tests/test_notifications.py +++ b/tests/test_notifications.py @@ -247,6 +247,27 @@ async def test_wrong_host( g90._handle_notification.assert_not_called() +@pytest.mark.g90device( + sent_data=[ + b'ISTART[206,' + b'["","DUMMYPRODUCT",' + b'"1.2","1.1","206","206",3,3,0,2,"4242",50,100]]IEND\0', + ], +) +async def test_empty_device_guid(mock_device: DeviceMock) -> None: + """ + Verifies that alert from device with empty GUID is ignored. + """ + g90 = G90Alarm( + host=mock_device.host, port=mock_device.port, + notifications_local_host=mock_device.notification_host, + notifications_local_port=mock_device.notification_port + ) + # The command will fetch the host info and store the GIUD + await g90.get_host_info() + assert g90.device_id is None + + @pytest.mark.g90device( sent_data=[ b'ISTART[206,' From 84832d0e2a7d242113ee52e8e39b155e871935b2 Mon Sep 17 00:00:00 2001 From: Ilia Sotnikov Date: Tue, 17 Dec 2024 00:43:23 +0300 Subject: [PATCH 2/5] * `device_notifications.py`: Linting fixes --- src/pyg90alarm/device_notifications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyg90alarm/device_notifications.py b/src/pyg90alarm/device_notifications.py index 93d80a7..e32b94e 100644 --- a/src/pyg90alarm/device_notifications.py +++ b/src/pyg90alarm/device_notifications.py @@ -473,7 +473,7 @@ def device_id(self) -> Optional[str]: def device_id(self, device_id: str) -> None: # Under not yet identified circumstances the device ID might be empty # string provided by :meth:`G90Alarm.get_host_info` - disallow that - if not device_id or not len(device_id.strip()): + if not device_id or not len(device_id.strip()) == 0: _LOGGER.debug( 'Device ID is empty or contains whitespace only, not setting' ) From c82754a7651fcdb402b693c403f9b4837883cb19 Mon Sep 17 00:00:00 2001 From: Ilia Sotnikov Date: Tue, 17 Dec 2024 00:46:13 +0300 Subject: [PATCH 3/5] * `test_empty_device_guid`: Close instance of `G90Alarm`, otherwise other tests could fail --- tests/test_notifications.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_notifications.py b/tests/test_notifications.py index 78374ee..dab67df 100644 --- a/tests/test_notifications.py +++ b/tests/test_notifications.py @@ -265,6 +265,7 @@ async def test_empty_device_guid(mock_device: DeviceMock) -> None: ) # The command will fetch the host info and store the GIUD await g90.get_host_info() + g90.close() assert g90.device_id is None From 705bb4ceb35247d1db20c412d02859501f1ab39d Mon Sep 17 00:00:00 2001 From: Ilia Sotnikov Date: Tue, 17 Dec 2024 00:51:26 +0300 Subject: [PATCH 4/5] * Fixed wrong condition to test for empty device ID in `G90DeviceNotifications.device_id` setter --- src/pyg90alarm/device_notifications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyg90alarm/device_notifications.py b/src/pyg90alarm/device_notifications.py index e32b94e..f1b13b0 100644 --- a/src/pyg90alarm/device_notifications.py +++ b/src/pyg90alarm/device_notifications.py @@ -473,7 +473,7 @@ def device_id(self) -> Optional[str]: def device_id(self, device_id: str) -> None: # Under not yet identified circumstances the device ID might be empty # string provided by :meth:`G90Alarm.get_host_info` - disallow that - if not device_id or not len(device_id.strip()) == 0: + if not device_id or not len(device_id.strip()) != 0: _LOGGER.debug( 'Device ID is empty or contains whitespace only, not setting' ) From f6b6538108804646a6cd406df96a0f64ae762d38 Mon Sep 17 00:00:00 2001 From: Ilia Sotnikov Date: Tue, 17 Dec 2024 00:57:32 +0300 Subject: [PATCH 5/5] * `device_notifications.py`: Address Sonar concerns re: condition to disallow empty device ID --- src/pyg90alarm/device_notifications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyg90alarm/device_notifications.py b/src/pyg90alarm/device_notifications.py index f1b13b0..333d6b4 100644 --- a/src/pyg90alarm/device_notifications.py +++ b/src/pyg90alarm/device_notifications.py @@ -473,7 +473,7 @@ def device_id(self) -> Optional[str]: def device_id(self, device_id: str) -> None: # Under not yet identified circumstances the device ID might be empty # string provided by :meth:`G90Alarm.get_host_info` - disallow that - if not device_id or not len(device_id.strip()) != 0: + if not device_id or len(device_id.strip()) == 0: _LOGGER.debug( 'Device ID is empty or contains whitespace only, not setting' )