From 2fbf1fd5d91be7e5b6d8646fdd608eea3294db2a Mon Sep 17 00:00:00 2001 From: unl0ck Date: Thu, 26 Dec 2024 00:20:34 +0000 Subject: [PATCH 1/5] convert state to string --- ha_mqtt_discoverable/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ha_mqtt_discoverable/__init__.py b/ha_mqtt_discoverable/__init__.py index 6098c35..57c0290 100644 --- a/ha_mqtt_discoverable/__init__.py +++ b/ha_mqtt_discoverable/__init__.py @@ -740,6 +740,7 @@ def _state_helper( topic = self.state_topic if last_reset: state = {"state": state, "last_reset": last_reset} + state = json.dumps(state) logger.debug(f"Writing '{state}' to {topic}") if self._settings.debug: From f24c242e6ad1cf6b4e863c44573cf5dd357e0713 Mon Sep 17 00:00:00 2001 From: unl0ck Date: Thu, 26 Dec 2024 22:10:04 +0000 Subject: [PATCH 2/5] added unit test with last reset --- tests/test_sensor.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 6c5980a..4349bf5 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -17,6 +17,7 @@ from ha_mqtt_discoverable import Settings from ha_mqtt_discoverable.sensors import Sensor, SensorInfo +from unittest.mock import patch @pytest.fixture(params=["°C", "kWh"]) @@ -46,4 +47,21 @@ def test_generate_config(sensor: Sensor): def test_update_state(sensor: Sensor): - sensor.set_state(1) + with patch.object(sensor.mqtt_client, 'publish') as mock_publish: + sensor.set_state(1) + mock_publish.assert_called_with(sensor.state_topic, '1', retain=False) + +def test_update_state_with_last_reset(sensor: Sensor): + from datetime import datetime, timedelta, timezone + now = datetime.now(timezone(timedelta(hours=1))) + midnight = now.replace(hour=0, minute=0, second=0, microsecond=0) + + with patch.object(sensor.mqtt_client, 'publish') as mock_publish: + sensor.set_state(1, midnight.isoformat()) + mock_publish.assert_called_with(sensor.state_topic, '1', retain=False) + # Check the last_reset parameter + parameter1 = mock_publish.call_args.kwargs['payload'] + print(f"parameter {parameter1}") + assert parameter1 == '1' + assert mock_publish.call_args.kwargs['last_reset'] == midnight.isoformat() + From 3050b24a03d3b7d2acd4e83172eada8f149fda35 Mon Sep 17 00:00:00 2001 From: unl0ck Date: Thu, 26 Dec 2024 22:25:59 +0000 Subject: [PATCH 3/5] modified unit tests --- tests/test_sensor.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 4349bf5..3766391 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -49,7 +49,7 @@ def test_generate_config(sensor: Sensor): def test_update_state(sensor: Sensor): with patch.object(sensor.mqtt_client, 'publish') as mock_publish: sensor.set_state(1) - mock_publish.assert_called_with(sensor.state_topic, '1', retain=False) + mock_publish.assert_called_with(sensor.state_topic, '1', retain=True) def test_update_state_with_last_reset(sensor: Sensor): from datetime import datetime, timedelta, timezone @@ -58,10 +58,11 @@ def test_update_state_with_last_reset(sensor: Sensor): with patch.object(sensor.mqtt_client, 'publish') as mock_publish: sensor.set_state(1, midnight.isoformat()) - mock_publish.assert_called_with(sensor.state_topic, '1', retain=False) + mock_publish.assert_called_with(sensor.state_topic, '1', retain=True) # Check the last_reset parameter parameter1 = mock_publish.call_args.kwargs['payload'] print(f"parameter {parameter1}") - assert parameter1 == '1' - assert mock_publish.call_args.kwargs['last_reset'] == midnight.isoformat() + import json + parameter1_json = json.loads(parameter1) + assert parameter1_json['last_reset'] == midnight.isoformat() From 692e4bb4c0f6621c461516a5cd1a04cbdc8afad1 Mon Sep 17 00:00:00 2001 From: unl0ck Date: Thu, 26 Dec 2024 22:48:05 +0000 Subject: [PATCH 4/5] fixed unit test --- tests/test_sensor.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 3766391..2e558e8 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -58,11 +58,8 @@ def test_update_state_with_last_reset(sensor: Sensor): with patch.object(sensor.mqtt_client, 'publish') as mock_publish: sensor.set_state(1, midnight.isoformat()) - mock_publish.assert_called_with(sensor.state_topic, '1', retain=True) - # Check the last_reset parameter - parameter1 = mock_publish.call_args.kwargs['payload'] - print(f"parameter {parameter1}") + parameter = mock_publish.call_args.args[1] import json - parameter1_json = json.loads(parameter1) - assert parameter1_json['last_reset'] == midnight.isoformat() + parameter_json = json.loads(parameter) + assert parameter_json['last_reset'] == midnight.isoformat() From b4522bfa72abc00d25245633f5fc1fe2a4853f7b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 Dec 2024 22:48:51 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_sensor.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 2e558e8..7adc4ce 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -47,19 +47,21 @@ def test_generate_config(sensor: Sensor): def test_update_state(sensor: Sensor): - with patch.object(sensor.mqtt_client, 'publish') as mock_publish: + with patch.object(sensor.mqtt_client, "publish") as mock_publish: sensor.set_state(1) - mock_publish.assert_called_with(sensor.state_topic, '1', retain=True) + mock_publish.assert_called_with(sensor.state_topic, "1", retain=True) + def test_update_state_with_last_reset(sensor: Sensor): from datetime import datetime, timedelta, timezone + now = datetime.now(timezone(timedelta(hours=1))) midnight = now.replace(hour=0, minute=0, second=0, microsecond=0) - with patch.object(sensor.mqtt_client, 'publish') as mock_publish: + with patch.object(sensor.mqtt_client, "publish") as mock_publish: sensor.set_state(1, midnight.isoformat()) parameter = mock_publish.call_args.args[1] import json - parameter_json = json.loads(parameter) - assert parameter_json['last_reset'] == midnight.isoformat() + parameter_json = json.loads(parameter) + assert parameter_json["last_reset"] == midnight.isoformat()