Skip to content

Remove unnecessary enums#220

Open
llucax wants to merge 10 commits into
frequenz-floss:v0.x.xfrom
llucax:no-unspecified-phase-a
Open

Remove unnecessary enums#220
llucax wants to merge 10 commits into
frequenz-floss:v0.x.xfrom
llucax:no-unspecified-phase-a

Conversation

@llucax

@llucax llucax commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

This is the fist PR in what it will very likely be a series of 3 PRs to remove unnecessary enums and eventually all UNSPECIFIED enum values.

Having UNSPECIFIED enum values is a common anti-pattern, and was only used so far because we started writing wrappers in a very 1-1 fashion. But Python abstractions should be more Pythonic and safer.

The broad plan is to better abstract whole enums where possible, and to remove UNSPECIFIED values for the rest, preferring using None (or omitting the value completely) for the unspecified case.

This PR mainly removes the MicrogridStatus and ElectricalComponentOperationalMode enums, using getters that can raise a new UnspecifiedValueError exception instead. Leaving the enums as low-level artifacts in the protobuf conversion functions only.

It also introduces and related safety feature, forbidding by default the construction of Microgrid and ElectricalComponent objects directly, as on the Python side they should only be retrieved from API services. This avoid users accidentally creating objects with unspecified values, another abstraction leak we want to avoid.

llucax added 10 commits June 19, 2026 15:30
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Using the `ElectricalComponentOperationalMode` enum as an attribute is
not very convenient. Users will need to check for multiple combinations
when trying to find out if either telemetry or control is available,
plus dealing with the possibility of an unspecified value, which should
be rare, but possible.

This new approach completely leaves the operational mode representation
for the protobuf conversion layer, and maps different modes to new
properties that are much safer to use.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Electrical components are normally loaded from protobuf, and Python code
should never create them, but get them from API services.

We forbid constructing electrical components, but add a hidden escape
hatch to be able to create them in `_from_proto()` functions.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The idea is to get rid of all unspecified values in Python code, so this
is a step towards that direction. Having a UNSPECIFIED value in that map
is really not helpful, there is not much a user can do with that value.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The problem was we were using a type too broad that required passing
the category explicitly. If we use a type union of only the types we
are going to test we can get also mypy validation for the ctor we are
calling.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Similar to operational status in electrical components, exposing an enum
with the microgrid status is too low level and inconvenient to use.

This class already exposed a `is_active` property, but an unspecified
value produced a log warning, which is unsafer than it should, not
allowing end users decide how to deal with it, and forcing a default
that might not be suitable for all downstream users.

This commit changes the `is_active` property to a function that raises
if the value is unspecified.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Like electrical components, this object is normally loaded from
protobuf, and Python code should never create it, but get it from API
services.

We forbid microgrid objects, but add a hidden escape hatch to be able to
create them in the `microgrid_from_proto()` function.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Add also a release note about adding the new `electrical_components`
package that was missing.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax llucax requested a review from a team as a code owner June 19, 2026 13:38
@llucax llucax requested review from florian-wagner-frequenz and removed request for a team June 19, 2026 13:38
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:microgrid Affects the microgrid protobuf definitions labels Jun 19, 2026
@llucax llucax self-assigned this Jun 19, 2026
@llucax llucax added this to the v0.4.1 milestone Jun 19, 2026
@llucax

llucax commented Jun 19, 2026

Copy link
Copy Markdown
Contributor Author

This might be a controversial change, but I think it is the right approach towards a more pythonic and save interface.

The plan for the following PRs is, next do the same for stuff that exists in v0.4.0, that should get a deprecation path (I will probably backport these deprecations to v0.3.x and consider the v0.4.0 release a glitch and go for v0.5.0 for the next release). This includes removing UNSPECIFIED for enums that we definitely need to keep, like Metric and most telemetry stuff (diagnostics, etc.).

Then the most disruptive and potentially controversial change: completely remove the ElectricalComponentCategory and XxxType (battery, inverter, etc.) enums. Where we use the enum now, we can pass the class instead, and this repo will provide a convenience function to convert from class type to protobuf enum for client implementors to use (for example for dispatch targets, or microgrid/assets filters when getting components).

Please comment if you have any concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:microgrid Affects the microgrid protobuf definitions part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant