Skip to content

Commit 53d93cb

Browse files
Add example with BatteryStatus working alone
Signed-off-by: ela-kotulska-frequenz <[email protected]>
1 parent d0e236e commit 53d93cb

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

examples/battery_status.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# License: MIT
2+
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Example how to run BatteryPoolStatus as separate instance.
5+
6+
This is not needed for user but simplifies testing and debugging and understanding
7+
this feature.
8+
"""
9+
10+
import asyncio
11+
import logging
12+
13+
from frequenz.sdk import microgrid
14+
from frequenz.sdk.actor.power_distributing._battery_pool_status import BatteryPoolStatus
15+
from frequenz.sdk.microgrid.component import ComponentCategory
16+
17+
_logger = logging.getLogger(__name__)
18+
HOST = "microgrid.sandbox.api.frequenz.io" # it should be the host name.
19+
PORT = 61060
20+
21+
22+
async def main() -> None:
23+
"""Start BatteryPoolStatus to see how it works."""
24+
logging.basicConfig(
25+
level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)s:%(message)s"
26+
)
27+
await microgrid.initialize(HOST, PORT)
28+
batteries = {
29+
bat.component_id
30+
for bat in microgrid.get().component_graph.components(
31+
component_category={ComponentCategory.BATTERY}
32+
)
33+
}
34+
35+
batteries_status = BatteryPoolStatus(
36+
battery_ids=batteries,
37+
max_data_age_sec=5,
38+
max_blocking_duration_sec=30,
39+
)
40+
41+
await batteries_status.join()
42+
43+
44+
asyncio.run(main())

src/frequenz/sdk/actor/power_distributing/_battery_pool_status.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ def __init__(
123123

124124
self._task = asyncio.create_task(self._run())
125125

126+
async def join(self) -> None:
127+
"""Await for the battery pool, and return when the task completes.
128+
129+
It will not terminate the program while BatteryPool is working.
130+
BatteryPool can be stopped with the `stop` method.
131+
This method is not needed in source code, because BatteryPool is owned
132+
by the internal code.
133+
It is needed only when user needs to run his own instance of the BatteryPool.
134+
"""
135+
await self._task
136+
126137
async def stop(self) -> None:
127138
"""Stop tracking batteries status."""
128139
await cancel_and_await(self._task)

0 commit comments

Comments
 (0)