99
1010from __future__ import annotations
1111
12- from dataclasses import dataclass
12+ import json
13+ from dataclasses import asdict , dataclass
1314from datetime import datetime , timezone
14- from typing import Any , Optional
1515
1616from frequenz .api .common .v1alpha8 .grid .delivery_area_pb2 import (
1717 DeliveryArea as PBDeliveryArea ,
@@ -44,16 +44,10 @@ def from_protobuf(pb: PBDeliveryArea) -> "DeliveryArea":
4444 Returns:
4545 A new DeliveryArea instance populated with data from the protobuf message.
4646 """
47- return DeliveryArea (code = pb .code , code_type = str (pb .code_type ))
48-
49- def to_dict (self ) -> dict [str , Any ]:
50- """
51- Convert the DeliveryArea instance to a dictionary.
52-
53- Returns:
54- A dictionary containing the delivery area details.
55- """
56- return {"code" : self .code , "code_type" : self .code_type }
47+ return DeliveryArea (
48+ code = pb .code ,
49+ code_type = str (pb .code_type ),
50+ )
5751
5852
5953@dataclass (frozen = True )
@@ -67,19 +61,6 @@ class Location:
6761 longitude : float
6862 country_code : str
6963
70- def to_dict (self ) -> dict [str , Any ]:
71- """
72- Convert the Location instance to a dictionary.
73-
74- Returns:
75- A dictionary containing the location details.
76- """
77- return {
78- "latitude" : self .latitude ,
79- "longitude" : self .longitude ,
80- "country_code" : self .country_code ,
81- }
82-
8364 @staticmethod
8465 def from_protobuf (pb : PBLocation ) -> "Location" :
8566 """
@@ -92,7 +73,9 @@ def from_protobuf(pb: PBLocation) -> "Location":
9273 A new Location instance populated with data from the protobuf message.
9374 """
9475 return Location (
95- latitude = pb .latitude , longitude = pb .longitude , country_code = pb .country_code
76+ latitude = pb .latitude ,
77+ longitude = pb .longitude ,
78+ country_code = pb .country_code ,
9679 )
9780
9881
@@ -120,30 +103,11 @@ class Microgrid:
120103 id : MicrogridId
121104 enterprise_id : EnterpriseId
122105 name : str
123- delivery_area : Optional [ DeliveryArea ]
124- location : Optional [ Location ]
106+ delivery_area : DeliveryArea | None
107+ location : Location | None
125108 status : int
126109 create_time : datetime
127110
128- def to_dict (self ) -> dict [str , Any ]:
129- """
130- Convert the Microgrid instance to a dictionary.
131-
132- Returns:
133- A dictionary containing the microgrid details.
134- """
135- return {
136- "id" : int (self .id ),
137- "enterprise_id" : int (self .enterprise_id ),
138- "name" : self .name ,
139- "delivery_area" : (
140- self .delivery_area .to_dict () if self .delivery_area else None
141- ),
142- "location" : self .location .to_dict () if self .location else None ,
143- "status" : self .status ,
144- "create_time" : self .create_time .isoformat () if self .create_time else None ,
145- }
146-
147111 @staticmethod
148112 def from_protobuf (pb : PBMicrogrid ) -> "Microgrid" :
149113 """
@@ -155,11 +119,11 @@ def from_protobuf(pb: PBMicrogrid) -> "Microgrid":
155119 Returns:
156120 A new Microgrid instance populated with data from the protobuf message.
157121 """
158- delivery_area : Optional [ DeliveryArea ] = None
122+ delivery_area : DeliveryArea = None
159123 if pb .HasField ("delivery_area" ):
160124 delivery_area = DeliveryArea .from_protobuf (pb .delivery_area )
161125
162- location : Optional [ Location ] = None
126+ location : Location = None
163127 if pb .HasField ("location" ):
164128 location = Location .from_protobuf (pb .location )
165129
@@ -172,3 +136,12 @@ def from_protobuf(pb: PBMicrogrid) -> "Microgrid":
172136 status = pb .status ,
173137 create_time = pb .create_timestamp .ToDatetime ().replace (tzinfo = timezone .utc ),
174138 )
139+
140+ def to_json (self ) -> str :
141+ """Convert the Microgrid instance to a JSON string."""
142+ microgrid_dict = asdict (self )
143+ microgrid_dict ["id" ] = int (self .id )
144+ microgrid_dict ["enterprise_id" ] = int (self .enterprise_id )
145+ microgrid_dict ["create_time" ] = self .create_time .isoformat ()
146+
147+ return json .dumps (microgrid_dict , indent = 2 )
0 commit comments