From 6413b9a5a9da3b75f71e30fc6fa31abf05fd0891 Mon Sep 17 00:00:00 2001 From: Mario Coray Date: Wed, 13 Mar 2024 10:56:02 +0100 Subject: [PATCH] replace deprecated utcnow() with now(timezone.utc) --- .../smartmeter/hdlc_dlms_parser.py | 4 ++-- tests/test_collector.py | 10 +++++----- tests/test_meter_data.py | 4 ++-- tests/test_mqtt_sink.py | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/smartmeter_datacollector/smartmeter/hdlc_dlms_parser.py b/smartmeter_datacollector/smartmeter/hdlc_dlms_parser.py index 19d11b1..b35d2b6 100644 --- a/smartmeter_datacollector/smartmeter/hdlc_dlms_parser.py +++ b/smartmeter_datacollector/smartmeter/hdlc_dlms_parser.py @@ -122,7 +122,7 @@ def convert_dlms_bundle_to_reader_data(self, dlms_objects: List[GXDLMSObject], timestamp = None if self._use_system_time: - timestamp = datetime.utcnow() + timestamp = datetime.now(timezone.utc) if not timestamp: timestamp = self._cosem.retrieve_time_from_dlms_registers(obis_obj_pairs) @@ -131,7 +131,7 @@ def convert_dlms_bundle_to_reader_data(self, dlms_objects: List[GXDLMSObject], if not timestamp: LOGGER.warning("Unable to get timestamp from message. Falling back to system time.") self._use_system_time = True - timestamp = datetime.utcnow() + timestamp = datetime.now(timezone.utc) if not timestamp.tzinfo: # if timezone info not set, assume UTC diff --git a/tests/test_collector.py b/tests/test_collector.py index 0346174..0c01aa3 100644 --- a/tests/test_collector.py +++ b/tests/test_collector.py @@ -7,7 +7,7 @@ # import asyncio import sys -from datetime import datetime +from datetime import datetime, timezone import pytest from pytest_mock import MockerFixture @@ -27,7 +27,7 @@ def test_type() -> MeterDataPointType: async def test_collector_with_one_sink(mocker: MockerFixture, test_type: MeterDataPointType): coll = Collector() sink = mocker.AsyncMock(DataSink) - data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.utcnow()) + data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.now(timezone.utc)) coll.register_sink(sink) coll.notify([data_point]) @@ -46,8 +46,8 @@ async def test_collector_with_one_sink_multiple_data_points(mocker: MockerFixtur sink = mocker.AsyncMock(DataSink) coll.register_sink(sink) - point0 = MeterDataPoint(test_type, 0.0, "test_source", datetime.utcnow()) - point1 = MeterDataPoint(test_type, 1.0, "test_source", datetime.utcnow()) + point0 = MeterDataPoint(test_type, 0.0, "test_source", datetime.now(timezone.utc)) + point1 = MeterDataPoint(test_type, 1.0, "test_source", datetime.now(timezone.utc)) coll.notify([point0, point1]) routine = coll.process_queue() @@ -65,7 +65,7 @@ async def test_collector_with_two_sinks(mocker: MockerFixture, test_type: MeterD sink0 = mocker.AsyncMock(DataSink) sink1 = mocker.AsyncMock(DataSink) - data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.utcnow()) + data_point = MeterDataPoint(test_type, 0.0, "test_source", datetime.now(timezone.utc)) coll.register_sink(sink0) coll.register_sink(sink1) diff --git a/tests/test_meter_data.py b/tests/test_meter_data.py index 2a473bb..c54449a 100644 --- a/tests/test_meter_data.py +++ b/tests/test_meter_data.py @@ -6,14 +6,14 @@ # See LICENSES/README.md for more information. # import json -from datetime import datetime +from datetime import datetime, timezone from smartmeter_datacollector.smartmeter.meter_data import MeterDataPoint, MeterDataPointType def test_meter_data_serialize(): test_type = MeterDataPointType("TEST_TYPE", "test type", "unit") - data_point = MeterDataPoint(test_type, 1.0, "TestSource", datetime.utcnow()) + data_point = MeterDataPoint(test_type, 1.0, "TestSource", datetime.now(timezone.utc)) data_point_json = data_point.to_json() diff --git a/tests/test_mqtt_sink.py b/tests/test_mqtt_sink.py index 23e34c9..86dc745 100644 --- a/tests/test_mqtt_sink.py +++ b/tests/test_mqtt_sink.py @@ -8,7 +8,7 @@ import configparser import json import sys -from datetime import datetime +from datetime import datetime, timezone from unittest import mock import pytest @@ -42,7 +42,7 @@ async def test_mqtt_sink_send_point_when_started(mocker: MockerFixture): config = MqttConfig("localhost") sink = MqttDataSink(config) client_mock = mocker.patch.object(sink, "_client", autospec=True) - data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.utcnow()) + data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.now(timezone.utc)) expected_topic = f"smartmeter/test_source/{TEST_TYPE.identifier}" expected_payload = json.dumps({ "value": data_point.value, @@ -61,7 +61,7 @@ async def test_mqtt_sink_send_reconnect_when_not_started(mocker: MockerFixture): config = MqttConfig("localhost") sink = MqttDataSink(config) client_mock = mocker.patch.object(sink, "_client", autospec=True) - data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.utcnow()) + data_point = MeterDataPoint(TEST_TYPE, 1.0, "test_source", datetime.now(timezone.utc)) client_mock.publish.side_effect = MqttCodeError(MQTT_ERR_NO_CONN) await sink.send(data_point)