From 03610c50f44f2fbb4d3c7e4aa8122d42ed409cc1 Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Wed, 14 May 2025 09:56:32 +0200 Subject: [PATCH] Fix `end_time` not handling `None` values correctly Signed-off-by: Mathias L. Baumann --- RELEASE_NOTES.md | 4 ++++ src/frequenz/client/dispatch/types.py | 10 ++++++++-- tests/test_proto.py | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 147d7768..85c7b564 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -4,6 +4,10 @@ * The dispatch client now supports the official dispatch domain: `grpc://dispatch.eu-1.staging.api.frequenz.com:443` +## Bug Fixes + +* `end_time` was not correctly handling the `None` value when converted from protobuf to pythons `Dispatch` class. + ## Upgrading * Renamed `Client` class to `DispatchApiClient` for clarity. diff --git a/src/frequenz/client/dispatch/types.py b/src/frequenz/client/dispatch/types.py index d47fca53..1f8cc6bf 100644 --- a/src/frequenz/client/dispatch/types.py +++ b/src/frequenz/client/dispatch/types.py @@ -296,7 +296,11 @@ def from_protobuf(cls, pb_object: PBDispatch) -> "Dispatch": type=pb_object.data.type, create_time=to_datetime(pb_object.metadata.create_time), update_time=to_datetime(pb_object.metadata.modification_time), - end_time=to_datetime(pb_object.metadata.end_time), + end_time=( + to_datetime(pb_object.metadata.end_time) + if pb_object.metadata.HasField("end_time") + else None + ), start_time=to_datetime(pb_object.data.start_time), duration=( timedelta(seconds=pb_object.data.duration) @@ -324,7 +328,9 @@ def to_protobuf(self) -> PBDispatch: dispatch_id=self.id, create_time=to_timestamp(self.create_time), modification_time=to_timestamp(self.update_time), - end_time=to_timestamp(self.end_time), + end_time=( + to_timestamp(self.end_time) if self.end_time is not None else None + ), ), data=DispatchData( type=self.type, diff --git a/tests/test_proto.py b/tests/test_proto.py index 0d28f1fd..1a4f1e8c 100644 --- a/tests/test_proto.py +++ b/tests/test_proto.py @@ -132,6 +132,26 @@ def test_dispatch() -> None: byweekdays=[Weekday.SUNDAY, Weekday.FRIDAY], ), ), + Dispatch( + id=125, + type="test-3", + create_time=datetime(2024, 3, 10, tzinfo=timezone.utc), + update_time=datetime(2024, 3, 11, tzinfo=timezone.utc), + start_time=datetime(2024, 11, 10, tzinfo=timezone.utc), + end_time=None, + duration=timedelta(seconds=20), + target=[ComponentCategory.BATTERY], + active=False, + dry_run=True, + payload={"key": "value1"}, + recurrence=RecurrenceRule( + frequency=Frequency.MONTHLY, + interval=3, + end_criteria=EndCriteria(count=20), + byhours=[1, 12, 23], + byweekdays=[Weekday.SUNDAY, Weekday.FRIDAY], + ), + ), ): assert Dispatch.from_protobuf(dispatch.to_protobuf()) == dispatch