Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add component types #11

Merged
merged 2 commits into from
Feb 13, 2024
Merged

Conversation

Marenz
Copy link
Contributor

@Marenz Marenz commented Feb 6, 2024

Depends on #10

  • Delete cookiecutter template leftovers
  • Add components types from SDK

@Marenz Marenz requested a review from a team as a code owner February 6, 2024 10:33
@Marenz Marenz requested review from daniel-zullo-frequenz and removed request for a team February 6, 2024 10:33
@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.) labels Feb 6, 2024
@Marenz Marenz requested review from llucax and removed request for daniel-zullo-frequenz February 6, 2024 10:33
@Marenz Marenz force-pushed the add_component_types branch from 7b68e85 to 9b9100a Compare February 6, 2024 10:44
@github-actions github-actions bot removed the part:docs Affects the documentation label Feb 6, 2024
@Marenz Marenz added the cmd:skip-release-notes It is not necessary to update release notes for this PR label Feb 6, 2024
@llucax llucax changed the title add component types Add component types Feb 6, 2024
Copy link
Contributor

@llucax llucax left a comment

Choose a reason for hiding this comment

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

This probably need a better structure, closer to the API definitions structure, but like with #8 I'm OK to merge it if the idea is just to get things moving an improve in followup PRs.

The dependency to microgrid-api is kind of big an problematic though, so I probably would not merge that.

pyproject.toml Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

I would make the module public instead, all the entities in the different directories in the protobuf are pretty much independent from each other.

I think also for this particular repo it would be very useful to stay as close as possible to the api-common repository, so I would rename this file as: src/frequenz/client/common/microgrid/components/__init__.py and put there everything related to proto/frequenz/api/common/v1/microgrid/components/components.proto there, and create one public file per any other proto/frequenz/api/common/v1/microgrid/components/*.py inside it, for example:

proto/frequenz/api/common/v1/microgrid/components/battery.proto src/frequenz/client/common/microgrid/components/battery.py`.

I would probably even go as far as including the version in the python path too, as I don't think it would be crazy to think that a client might need to support with multiple versions at the same time, so src/frequenz/client/common/v1/microgrid/components/__init__.py and src/frequenz/client/common/v1/microgrid/components/battery.py for example.

I would not re-export symbols in src/frequenz/client/common/__init__.py, as this repo has many unrelated stuff I think it is better if uses just use the full path when importing.

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'd abstract away the version here because it's easy to support multiple on this level, compared to the protobuf level

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, true. At some point we'll need to make breaking changes at this level too, but that's a broader discussion if we want to do something like using vX packages to allow users use multiple incompatible versions in the same code. I'm really starting to believe that could be a good idea in general... But again, separate discussion.

src/frequenz/client/common/_component.py Outdated Show resolved Hide resolved
"""A base class from which individual component types are derived."""


# pylint: disable=no-member
Copy link
Contributor

Choose a reason for hiding this comment

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

It is better to use this scheme:

# pylint: disable=no-name-in-module
import frequenz.api.common.components_pb2 import ComponentCategory 
# pylint: enable=no-name-in-module

Otherwise mistake trying to use non-existing names in a module will not be caught by mypy and will appear only in runtime.

For example: https://github.com/frequenz-floss/frequenz-sdk-python/blob/91821a3e2719024c0b2a114d6a11aa692d3429d6/src/frequenz/sdk/actor/power_distributing/_component_status/_battery_status_tracker.py#L12-L18

FYI @frequenz-floss/api-team

return hash(self.component_id)


class ComponentMetricId(Enum):
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these defined anywhere in the api-common repo? Or where do they come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Straight from the SDK...

Copy link
Contributor Author

@Marenz Marenz Feb 6, 2024

Choose a reason for hiding this comment

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

What I am trying to say: I didn't check where anything came from, I just copied the file and removed Fuse-dependent stuff

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, again, if we want to first commit the code copied from the SDK as is, then great, we can merge as is, but I would make it very clear in the commit message and PR description that this is the intention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nah, fuck that, I'll do it properly from the beginning

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I think components/components.proto should be translated to components/__init__.py in here, so it should be frequenz.client.common.microgrid.components import ComponentCategory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And how would the other proto files then be translated?

components/
  battery.proto
  components.proto
  ev_charger.proto
  fuse.proto
  grid.proto
  inverter.proto
  transformer.proto

Copy link
Contributor

Choose a reason for hiding this comment

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

components/battery.py, etc. I think we just treat xxx/xxx.proto specially and have it as xxx/__init__.py instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as I see this pattern is used in other dirs too, for example microgrid/microgrid.proto, and usually the xxx/xxx.proto file uses other xxx/yyy.proto files and not the other way around.

@tiyash-basu-frequenz FYI, just to confirm that his pattern is used consciously and not just a coincidence. It would be important to have a consistent scheme to make the python modules make sense and make sure we don't run into cyclic dependencies.

In the case of microgrid/sensor/sensor.proto, where there is only that file in the microgrid/sensor/ directory, I would just add that file as a plain microgrid/sensor.py. If more files are added in the future we can rename sensor.py to sensor/init.py` and it will be transparent to the end user.

Copy link

@tiyash-basu-frequenz tiyash-basu-frequenz Feb 12, 2024

Choose a reason for hiding this comment

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

This is a pattern we consciously use (for protobuf), and this is a result of how we want to define packages in our protobuf definitions. microgrid/sensor/sensor.proto means that the entities in sensor.proto are a member of the package ...microgrid.sensor. The same applies to all other protobuf files.

For references, check these links: frequenz-floss/frequenz-api-common#127 (comment), frequenz-floss/frequenz-api-common#131

@Marenz Marenz force-pushed the add_component_types branch from 9b9100a to 98a34d1 Compare February 6, 2024 16:20
@github-actions github-actions bot added the part:microgrid Affects the microgrid protobuf definitions label Feb 6, 2024
@Marenz
Copy link
Contributor Author

Marenz commented Feb 6, 2024

I removed now everything that I don't really need for dispatch and put it in the right place in the structure.

@Marenz Marenz requested a review from llucax February 6, 2024 16:24
@Marenz Marenz force-pushed the add_component_types branch 2 times, most recently from 5115eb5 to 0a3abd3 Compare February 6, 2024 16:55
# License: MIT
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH

"""Common code and utilities for Frequenz API clients."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe copy the module-level docs from https://github.com/frequenz-floss/frequenz-api-common/blob/v0.x.x/proto/frequenz/api/common/v1/microgrid/microgrid.proto?

It doesn't seem to be very useful to copy the same generic string on every module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The proto module-level docs seem even more generic...

// Frequenz microgrid definition.
//
// Copyright 2023 Frequenz Energy-as-a-Service GmbH
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to be fair, I think none of these init files seem very useful to me ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

to be fair, I think none of these init files seem very useful to me ;)

I agree on this one

The proto module-level docs seem even more generic...

But the one in the proto at least has microgrid in it :D

Anyway, if in this case in the proto file there is nothing useful, all good, my main point is the documentation in here has to be at least as "good" as the one in the proto files.

Copy link
Contributor

Choose a reason for hiding this comment

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

-> src/frequenz/client/common/microgrid/components/__init__.py

Comment on lines 18 to 21
"""Possible types of microgrid component."""

NONE = PBComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED
"""Unspecified component category."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Same about the documentation of all these items, we should use the same as in the protobuf stuff, or at least that at the minimum/base, maybe we can extend it with python-specific stuff too (or remove stuff that it is too protobuf-specific).

"""CHP component."""


def _component_category_from_protobuf(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are starting from scratch here, I would add a @classmethod from_proto() to the enum as we do with classes.

I'm not sure if I mentioned this, but I was thinking that if the idea is not to expose the protobuf stuff, when it might be better to have the to/from proto conversion separate (even in a separate module, so protobuf stuff is not imported at all when only using the wrappers), but then for enums that would be super inconvenient because we wouldn't be able to use the same values as in the protobuf stuff, which could be very useful when debugging (or we'll need to duplicate everything manually), so I guess for a first version it might make more sense to keep it simple (and coupled).

@Marenz Marenz force-pushed the add_component_types branch from 0a3abd3 to ac9cac4 Compare February 8, 2024 09:54
@Marenz
Copy link
Contributor Author

Marenz commented Feb 8, 2024

Added more docu, made to/from proto methods members.

Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
@Marenz Marenz force-pushed the add_component_types branch from ac9cac4 to 9d4b9bf Compare February 8, 2024 10:55
@llucax llucax mentioned this pull request Feb 9, 2024
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
@Marenz Marenz force-pushed the add_component_types branch from 9d4b9bf to d62dfa4 Compare February 12, 2024 11:22
@Marenz
Copy link
Contributor Author

Marenz commented Feb 12, 2024

Moved main types to init as requested

@Marenz Marenz requested a review from llucax February 12, 2024 11:22
@Marenz Marenz added this pull request to the merge queue Feb 13, 2024
Merged via the queue into frequenz-floss:v0.x.x with commit 5e6e7b7 Feb 13, 2024
14 checks passed
@Marenz Marenz deleted the add_component_types branch February 13, 2024 09:23
github-merge-queue bot pushed a commit that referenced this pull request Nov 20, 2024
Add component state codes and error codes similar to #11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd:skip-release-notes It is not necessary to update release notes for this PR 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.

3 participants