Skip to content

Commit 5f63030

Browse files
committed
Unify the style with the new wrappers
We adjust the documentation and enum lookup to match the newly added wrappers. The new enum conversion also avoid a double linear search when creating enums. We can just try to create it and if it fails create an `UNSPECIFIED` / `UNKNOWN` item, so we do only one search. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent b83d3db commit 5f63030

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/frequenz/client/microgrid/_component_states.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,20 @@ class EVChargerCableState(Enum):
128128
"""The cable is plugged into the EV and locked."""
129129

130130
@classmethod
131-
def from_pb(cls, evc_state: ev_charger.CableState) -> Self:
132-
"""Convert a protobuf CableState value to EVChargerCableState enum.
131+
def from_pb(cls, state: ev_charger.CableState) -> Self:
132+
"""Convert a protobuf state value to this enum.
133133
134134
Args:
135-
evc_state: protobuf cable state to convert.
135+
state: The protobuf cable state to convert.
136136
137137
Returns:
138-
Enum value corresponding to the protobuf message.
138+
The enum value corresponding to the protobuf message.
139139
"""
140-
if not any(t.value == evc_state for t in EVChargerCableState):
140+
try:
141+
return cls(state)
142+
except ValueError:
141143
return cls(cls.UNSPECIFIED)
142144

143-
return cls(evc_state)
144-
145145

146146
class EVChargerComponentState(Enum):
147147
"""Component State of an EV Charger."""
@@ -179,20 +179,20 @@ class EVChargerComponentState(Enum):
179179
"""The state is provided by the device, but it is not one of the above states."""
180180

181181
@classmethod
182-
def from_pb(cls, evc_state: ev_charger.ComponentState) -> Self:
183-
"""Convert a protobuf ComponentState value to EVChargerComponentState enum.
182+
def from_pb(cls, state: ev_charger.ComponentState) -> Self:
183+
"""Convert a protobuf state value to this enum.
184184
185185
Args:
186-
evc_state: protobuf component state to convert.
186+
state: The protobuf component state to convert.
187187
188188
Returns:
189-
Enum value corresponding to the protobuf message.
189+
The enum value corresponding to the protobuf message.
190190
"""
191-
if not any(t.value == evc_state for t in EVChargerComponentState):
191+
try:
192+
return cls(state)
193+
except ValueError:
192194
return cls(cls.UNKNOWN)
193195

194-
return cls(evc_state)
195-
196196

197197
class InverterComponentState(Enum):
198198
"""Component states of an inverter."""

0 commit comments

Comments
 (0)