-
Couldn't load subscription status.
- Fork 4
Add support for duration=None #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,9 +12,9 @@ | |
| from frequenz.api.dispatch.v1.dispatch_pb2 import ( | ||
| CreateMicrogridDispatchRequest as PBDispatchCreateRequest, | ||
| ) | ||
|
|
||
| # pylint: enable=no-name-in-module | ||
| from frequenz.api.dispatch.v1.dispatch_pb2 import DispatchData | ||
| from google.protobuf.json_format import MessageToDict | ||
| from google.protobuf.struct_pb2 import Struct | ||
|
|
||
| from frequenz.client.base.conversion import to_datetime, to_timestamp | ||
|
|
||
|
|
@@ -25,6 +25,8 @@ | |
| component_selector_to_protobuf, | ||
| ) | ||
|
|
||
| # pylint: enable=no-name-in-module | ||
|
|
||
|
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't look right here, but you could also just disable it globally in the whole project as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is mostly at this place due to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it is weird, it has to be just before the next line. |
||
|
|
||
| # pylint: disable=too-many-instance-attributes | ||
| @dataclass(kw_only=True) | ||
|
|
@@ -42,8 +44,12 @@ class DispatchCreateRequest: | |
| start_time: datetime | ||
| """The start time of the dispatch in UTC.""" | ||
|
|
||
| duration: timedelta | ||
| """The duration of the dispatch, represented as a timedelta.""" | ||
| duration: timedelta | None | ||
| """The duration of the dispatch, represented as a timedelta. | ||
|
|
||
| If None, the dispatch is considered to be "infinite" or "instantaneous", | ||
| like a command to turn on a component. | ||
| """ | ||
|
|
||
| selector: ComponentSelector | ||
| """The component selector specifying which components the dispatch targets.""" | ||
|
|
@@ -78,13 +84,19 @@ def from_protobuf( | |
| Returns: | ||
| The converted dispatch. | ||
| """ | ||
| duration = ( | ||
| timedelta(seconds=pb_object.dispatch_data.duration) | ||
| if pb_object.dispatch_data.HasField("duration") | ||
| else None | ||
| ) | ||
|
|
||
| return DispatchCreateRequest( | ||
| microgrid_id=pb_object.microgrid_id, | ||
| type=pb_object.dispatch_data.type, | ||
| start_time=rounded_start_time( | ||
| to_datetime(pb_object.dispatch_data.start_time) | ||
| ), | ||
| duration=timedelta(seconds=pb_object.dispatch_data.duration), | ||
| duration=duration, | ||
| selector=component_selector_from_protobuf(pb_object.dispatch_data.selector), | ||
| active=pb_object.dispatch_data.is_active, | ||
| dry_run=pb_object.dispatch_data.is_dry_run, | ||
|
|
@@ -98,24 +110,24 @@ def to_protobuf(self) -> PBDispatchCreateRequest: | |
| Returns: | ||
| The converted protobuf dispatch create request. | ||
| """ | ||
| pb_request = PBDispatchCreateRequest() | ||
|
|
||
| pb_request.microgrid_id = self.microgrid_id | ||
| pb_request.dispatch_data.type = self.type | ||
| pb_request.dispatch_data.start_time.CopyFrom(to_timestamp(self.start_time)) | ||
| pb_request.dispatch_data.duration = round(self.duration.total_seconds()) | ||
| pb_request.dispatch_data.selector.CopyFrom( | ||
| component_selector_to_protobuf(self.selector) | ||
| payload = Struct() | ||
| payload.update(self.payload) | ||
|
|
||
| return PBDispatchCreateRequest( | ||
| microgrid_id=self.microgrid_id, | ||
| dispatch_data=DispatchData( | ||
| type=self.type, | ||
| start_time=to_timestamp(self.start_time), | ||
| duration=( | ||
| round(self.duration.total_seconds()) if self.duration else None | ||
| ), | ||
| selector=component_selector_to_protobuf(self.selector), | ||
| is_active=self.active, | ||
| is_dry_run=self.dry_run, | ||
| payload=payload, | ||
| recurrence=self.recurrence.to_protobuf() if self.recurrence else None, | ||
| ), | ||
| ) | ||
| pb_request.dispatch_data.is_active = self.active | ||
| pb_request.dispatch_data.is_dry_run = self.dry_run | ||
| pb_request.dispatch_data.payload.update(self.payload) | ||
| if self.recurrence: | ||
| pb_request.dispatch_data.recurrence.CopyFrom(self.recurrence.to_protobuf()) | ||
| else: | ||
| pb_request.dispatch_data.ClearField("recurrence") | ||
|
|
||
| return pb_request | ||
|
|
||
|
|
||
| def rounded_start_time(start_time: datetime) -> datetime: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.