Skip to content

Conversation

@llucax
Copy link
Contributor

@llucax llucax commented May 15, 2025

This pull request introduces support for sensors to the microgrid client.

A key focus of this update is forward-compatibility with the upcoming microgrid API v0.17. The new sensor-related RPCs (list_sensors(), stream_sensor_data()) and their associated data structures have been designed based on the v0.17 specifications. This approach aims to simplify the future migration process for users.

Fixes #136.

Signed-off-by: Leandro Lucarella <[email protected]>
@Copilot Copilot AI review requested due to automatic review settings May 15, 2025 15:46
@llucax llucax requested review from a team as code owners May 15, 2025 15:46
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:client Affects the client code labels May 15, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds sensor support to the microgrid client by introducing new RPCs for listing sensors and streaming sensor data, while updating related data structures and tests. Key changes include:

  • New sensor-related classes and methods (e.g. Sensor, SensorMetricSample) in the library.
  • Updates to the client to support list_sensors() and stream_sensor_data(), along with corresponding test cases.
  • Adjustments to ID types and release notes to include sensor functionality.

Reviewed Changes

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_sensor.py Added tests for sensor creation, equality, and hashing.
tests/test_metrics.py Added tests for metric sample creation and aggregation.
tests/test_lifetime.py Updated tests for lifetime validation and activity.
tests/test_id.py Updated ID tests to include SensorId.
tests/test_client.py Added tests for list_sensors(), stream_sensor_data(), and grpc error handling.
src/frequenz/client/microgrid/sensor.py New sensor-related classes and utilities.
src/frequenz/client/microgrid/metrics.py New definitions for metric aggregation.
src/frequenz/client/microgrid/_util.py Utility function for enum conversion.
src/frequenz/client/microgrid/_sensor_proto.py Converters from protobuf messages to sensor objects.
src/frequenz/client/microgrid/_lifetime.py Lifetime validation and activity checking.
src/frequenz/client/microgrid/_id.py Updated ID definitions to include sensors.
src/frequenz/client/microgrid/_client.py Extended client with sensor listing and streaming functionality.
src/frequenz/client/microgrid/init.py Updated exports to include sensor types.
RELEASE_NOTES.md Updated release notes to describe new sensor features.

@llucax llucax self-assigned this May 15, 2025
@llucax llucax added this to the v0.8.0 milestone May 15, 2025
@llucax
Copy link
Contributor Author

llucax commented May 15, 2025

@shsms I should have addressed all comments in #144. @tiyash-basu-frequenz maybe it would be good if you have a look to see if there is nothing that goes against future directions.

@llucax
Copy link
Contributor Author

llucax commented May 15, 2025

OK, it looks like we are going to unify the SensorMetric and Metric for components and the metric samples too. But I think I would do that in a separate PR once it is more settled, as this is super fresh.

@llucax
Copy link
Contributor Author

llucax commented May 15, 2025

BTW, now I tested this in a real microgrid and it seems to be working fine.

@llucax
Copy link
Contributor Author

llucax commented May 15, 2025

@shsms I hope you are happier with the commit split now too 🙏

@llucax
Copy link
Contributor Author

llucax commented May 16, 2025

Will rename Lifetime.active to Lifetime.is_operational_now() (no property, since it is an "expensive" operation), and active_at -> is_operational_at().

The lifetime will be introduced in the next microgrid API version, but
we add it now so we can add a rpc call to get sensors information in a
way that is compatible with future microgrid API versions.

Signed-off-by: Leandro Lucarella <[email protected]>
@llucax llucax force-pushed the sensors2 branch 2 times, most recently from a4494c3 to d957b8f Compare May 19, 2025 10:18
Copy link
Contributor

@shsms shsms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to continue from Add wrappers for sensor data.

UNSPECIFIED = 0
"""Default value (this should not be normally used and usually indicates an issue)."""

TEMPERATURE = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Time to switch to pb2 names instead of numbers now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not convinced. This is a pure-python class/interface, all the dealing with protobuf conversion is done in the _from_proto() functions. I just happen to pick the same numeric IDs for convenience, to make conversion more easy.

What arguments do you have in favor of using the PB values here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pure-python class/interface

We are trying to build a client for the microgrid API. Not a stand-alone thing. This is just increasing the chances for bugs, especially for esoteric component types, which we might add support for only later. To me, this is just overcomplicating things for no apparent benefit.

What arguments do you have in favor of using the PB values here?

Single source of truth for enums without any manual work.
and just a few days back, I saw some API PRs changing the numbers for enums.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point about this being very dependant on the defined protobuf stuff, but all wrappers are protobuf free. I know it is a very theoretical thing but I like it. :D

It is a very minor thing, but the alternative to me is also a very minor thing. The single point of truth is pointless once this is stable, is a write once, never ever touch it again, ever. So I'm not concerned at about about it.

That said, since it is controversial, and the API is currently NOT stable, I can see it might be problematic until we get there, and if we have some issue because of this, I don't want you in my face telling me I told you so, so I'll change it and leave the discussion for a distant future :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I told you so

Oh, I really enjoy doing it, too bad. But there will be other opportunities. :D

@llucax
Copy link
Contributor Author

llucax commented May 20, 2025

Added some new and some fixup! commits to address the comments. One minor advantage of using ints is we can add values that are not present in the protobuf yet (but OK, we could still do that only for the non-existing values, but I decided to remove them for now).

@llucax llucax requested review from Copilot and shsms May 20, 2025 14:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds support for sensor management in the microgrid client by introducing sensor listing and sensor data streaming functionality. Key changes include:

  • Adding tests for sensor-related functionality in test_client.py.
  • Introducing new sensor types, data structures, and conversion utilities in the sensor modules.
  • Updating client code to support sensor streaming and sensor ID handling.

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_lifetime.py Adds tests for asset lifetimes with no sensor-specific changes.
tests/test_id.py Updates ID tests to reflect new import and parameters changes.
tests/test_connection.py Adjusts imports for connection tests.
tests/test_component_data.py Updates ComponentData imports with new ID module.
tests/test_component.py Imports updated for component tests.
tests/test_client.py Introduces tests for list_sensors() and stream_sensor_data() methods.
src/frequenz/client/microgrid/sensor.py Adds sensor classes and enums for sensor metrics and states.
src/frequenz/client/microgrid/metrics.py Introduces aggregated metric value logic.
src/frequenz/client/microgrid/id.py Implements strongly typed IDs including SensorId.
src/frequenz/client/microgrid/_sensor_proto.py Provides conversion functions for sensor-related protobuf messages.
src/frequenz/client/microgrid/_client.py Adds sensor listing and data streaming methods to the client.
Others Miscellaneous import updates and deprecation removals.

@shsms
Copy link
Contributor

shsms commented May 21, 2025

Will check in the morning.

# Copyright © 2025 Frequenz Energy-as-a-Service GmbH

"""Strongly typed IDs for microgrids, components and sensors."""
r'''Provides strongly-typed unique identifiers for entities.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very nice, also useful outside the microgrid like for the GridpoolID, etc. and also the reporting client. Maybe this /and/ the subclasses should go to the base client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, next step is move lots of stuff in here to client-common and client-base.

llucax added 10 commits May 22, 2025 15:40
This class will be used to list and retrieve sensors information from
the microgrid. The class is based on version 0.17 of the microgrid API
instead of the current 0.15 version, so users can already use the new
interface and make migrating to 0.17 more straight forward.

It is also implemented having in mind that sensor types will be removed
in future releases (and it is actually not being set properly in the
0.15 service), so sensor types are being ignored.

To be compatible with 0.17, sensors also have a "fake" lifetime that is
not really retrieved from the microgrid (it is filled with a "eternal"
lifetime, so sensors are present always).

Signed-off-by: Leandro Lucarella <[email protected]>
This RPC is also based on version 0.17 of the microgrid API instead of
the current 0.15 version, so users can already use the new interface and
make migrating to 0.17 more straight forward.

It uses the `ListComponents` RPC filtering to retrieve only sensors.

Signed-off-by: Leandro Lucarella <[email protected]>
These wrappers are also not present in 0.15, but are added so users can
already start writing code that supports aggregates to ease the
migration to 0.17.

Because of this, no protobuf parsing code is added yet.

Signed-off-by: Leandro Lucarella <[email protected]>
Again, these wrappers are done with 0.17 in mind, and trying to be
forward-compatible, although sensor data in 0.15 is already more similar
to 0.17 (compared to component data).

Signed-off-by: Leandro Lucarella <[email protected]>
This RPC is also based on version 0.17 of the microgrid API instead of
the current 0.15 version, so users can already use the new interface
and make migrating to 0.17 more straight forward.

Signed-off-by: Leandro Lucarella <[email protected]>
Signed-off-by: Leandro Lucarella <[email protected]>
This module will be moved to `client-common` in the future, and there
will be more additions, so it makes sense that it is separated from the
microgrid module.

In an upcoming commit we'll also add a `BaseId` class to make defining
new IDs easier.

Signed-off-by: Leandro Lucarella <[email protected]>
Defining new IDs via the new base class is much simpler and concise now,
and avoids a lot of code duplication.

Please see the module documentation for more details.

Signed-off-by: Leandro Lucarella <[email protected]>
We need to remove some `SensorErrorCode`s because they are not defined
in 0.15, they were included as a future-compatibility with 0.17.

Signed-off-by: Leandro Lucarella <[email protected]>
@llucax llucax enabled auto-merge May 22, 2025 13:41
@llucax llucax added this pull request to the merge queue May 22, 2025
Merged via the queue into frequenz-floss:v0.x.x with commit ec02eeb May 22, 2025
5 checks passed
@llucax llucax deleted the sensors2 branch May 22, 2025 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:client Affects the client code part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose sensors

2 participants