Skip to content

Commit cc3e2d6

Browse files
authored
Fix end_time not handling None values correctly (#164)
2 parents bb1f2d9 + 03610c5 commit cc3e2d6

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* The dispatch client now supports the official dispatch domain: `grpc://dispatch.eu-1.staging.api.frequenz.com:443`
66

7+
## Bug Fixes
8+
9+
* `end_time` was not correctly handling the `None` value when converted from protobuf to pythons `Dispatch` class.
10+
711
## Upgrading
812

913
* Renamed `Client` class to `DispatchApiClient` for clarity.

src/frequenz/client/dispatch/types.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ def from_protobuf(cls, pb_object: PBDispatch) -> "Dispatch":
296296
type=pb_object.data.type,
297297
create_time=to_datetime(pb_object.metadata.create_time),
298298
update_time=to_datetime(pb_object.metadata.modification_time),
299-
end_time=to_datetime(pb_object.metadata.end_time),
299+
end_time=(
300+
to_datetime(pb_object.metadata.end_time)
301+
if pb_object.metadata.HasField("end_time")
302+
else None
303+
),
300304
start_time=to_datetime(pb_object.data.start_time),
301305
duration=(
302306
timedelta(seconds=pb_object.data.duration)
@@ -324,7 +328,9 @@ def to_protobuf(self) -> PBDispatch:
324328
dispatch_id=self.id,
325329
create_time=to_timestamp(self.create_time),
326330
modification_time=to_timestamp(self.update_time),
327-
end_time=to_timestamp(self.end_time),
331+
end_time=(
332+
to_timestamp(self.end_time) if self.end_time is not None else None
333+
),
328334
),
329335
data=DispatchData(
330336
type=self.type,

tests/test_proto.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ def test_dispatch() -> None:
132132
byweekdays=[Weekday.SUNDAY, Weekday.FRIDAY],
133133
),
134134
),
135+
Dispatch(
136+
id=125,
137+
type="test-3",
138+
create_time=datetime(2024, 3, 10, tzinfo=timezone.utc),
139+
update_time=datetime(2024, 3, 11, tzinfo=timezone.utc),
140+
start_time=datetime(2024, 11, 10, tzinfo=timezone.utc),
141+
end_time=None,
142+
duration=timedelta(seconds=20),
143+
target=[ComponentCategory.BATTERY],
144+
active=False,
145+
dry_run=True,
146+
payload={"key": "value1"},
147+
recurrence=RecurrenceRule(
148+
frequency=Frequency.MONTHLY,
149+
interval=3,
150+
end_criteria=EndCriteria(count=20),
151+
byhours=[1, 12, 23],
152+
byweekdays=[Weekday.SUNDAY, Weekday.FRIDAY],
153+
),
154+
),
135155
):
136156
assert Dispatch.from_protobuf(dispatch.to_protobuf()) == dispatch
137157

0 commit comments

Comments
 (0)