Skip to content

Commit 727f451

Browse files
authored
Add started_at (#187)
2 parents 4948f59 + 2b53d83 commit 727f451

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,15 @@
22

33
## Summary
44

5-
While the new TargetCategory class supports subtypes, only reading them is currently available; setting subtypes will be introduced in a future release.
5+
<!-- Here goes a general summary of what this release is about -->
66

77
## Upgrading
88

9-
* `TargetComponents` was reworked. It now is a type alias for `TargetIds | TargetCategories`:
10-
* `TargetIds` can be used to specify one or more specific target IDs:
11-
* `TargetIds(1, 2, 3)` or
12-
* `TargetIds(ComponentIds(1), ComponentIds(2), ComponentIds(3))`
13-
* `TargetCategories` can be used to specify one or more target categories:
14-
* `TargetCategories(ComponentCategory.BATTERY, ComponentCategory.INVERTER)`
15-
* Dispatch ids and microgrid ids are no longer simple `int` types but are now wrapped in `DispatchId` and `MicrogridId` classes, respectively. This allows for better type safety and clarity in the codebase.
9+
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
1610

1711
## New Features
1812

19-
* With the new `TargetCategory` class (providing `.category` and `.type`) we can now specify subtypes of the categories:
20-
* `ComponentCategory.BATTERY` uses `BatteryType` with possible values: `LI_ION`, `NA_ION`
21-
* `ComponentCategory.INVERTER` uses `InverterType` with possible values: `BATTERY`, `SOLAR`, `HYBRID`
22-
* `ComponentCategory.EV_CHARGER` uses `EvChargerType`: with possible values `AC`, `DC`, `HYBRID`
23-
* A few examples on how to use the new `TargetCategory`:
24-
* `TargetCategory(BatteryType.LI_ION)`
25-
* `category` is `ComponentCategory.BATTERY`
26-
* `type` is `BatteryType.LI_ION`
27-
* `TargetCategory(ComponentCategory.BATTERY)`
28-
* `category` is `ComponentCategory.BATTERY`
29-
* `type` is `None`
30-
* `TargetCategories(InverterType.SOLAR)`
31-
* `category` is `ComponentCategory.INVERTER`
32-
* `type` is `InverterType.SOLAR`
33-
13+
* `Dispatch.started_at(now: datetime)` was added as alternative to the `started` property for when users want to use the same `now` for multiple calls, ensuring deterministic return values with respect to the same `now`.
3414

3515
## Bug Fixes
3616

src/frequenz/client/dispatch/test/_service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
NONE_KEY = "none"
4747
"""Key that has no access to any resources in the FakeService."""
4848

49+
_logger = logging.getLogger(__name__)
50+
4951

5052
class FakeService:
5153
"""Dispatch mock service for testing."""
@@ -178,7 +180,7 @@ async def StreamMicrogridDispatches(
178180
receiver = self._stream_channel.new_receiver()
179181

180182
async for message in receiver:
181-
logging.debug("Received message: %s", message)
183+
_logger.debug("Received message: %s", message)
182184
if message.microgrid_id == MicrogridId(request.microgrid_id):
183185
response = StreamMicrogridDispatchesResponse(
184186
event=message.event.event.value,

src/frequenz/client/dispatch/types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,26 @@ def started(self) -> bool:
427427
return False
428428

429429
now = datetime.now(tz=timezone.utc)
430+
return self.started_at(now)
431+
432+
def started_at(self, now: datetime) -> bool:
433+
"""Check if the dispatch has started.
434+
435+
A dispatch is considered started if the current time is after the start
436+
time but before the end time.
437+
438+
Recurring dispatches are considered started if the current time is after
439+
the start time of the last occurrence but before the end time of the
440+
last occurrence.
441+
442+
Args:
443+
now: time to use as now
444+
445+
Returns:
446+
True if the dispatch is started
447+
"""
448+
if not self.active:
449+
return False
430450

431451
if now < self.start_time:
432452
return False

0 commit comments

Comments
 (0)