-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
7b68e85
to
9b9100a
Compare
There was a problem hiding this 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
"""A base class from which individual component types are derived.""" | ||
|
||
|
||
# pylint: disable=no-member |
There was a problem hiding this comment.
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.
FYI @frequenz-floss/api-team
return hash(self.component_id) | ||
|
||
|
||
class ComponentMetricId(Enum): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Straight from the SDK...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
9b9100a
to
98a34d1
Compare
I removed now everything that I don't really need for dispatch and put it in the right place in the structure. |
5115eb5
to
0a3abd3
Compare
# License: MIT | ||
# Copyright © 2023 Frequenz Energy-as-a-Service GmbH | ||
|
||
"""Common code and utilities for Frequenz API clients.""" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ;)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
"""Possible types of microgrid component.""" | ||
|
||
NONE = PBComponentCategory.COMPONENT_CATEGORY_UNSPECIFIED | ||
"""Unspecified component category.""" |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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).
0a3abd3
to
ac9cac4
Compare
Added more docu, made to/from proto methods members. |
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
ac9cac4
to
9d4b9bf
Compare
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
9d4b9bf
to
d62dfa4
Compare
Moved main types to init as requested |
Add component state codes and error codes similar to #11
Depends on #10