-
-
Notifications
You must be signed in to change notification settings - Fork 238
Add common energy module and deprecate device emeter attributes #976
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6fb5171
Add common energy module and deprecate device emeter attributes
sdb9696 14a64c6
Fix tests
sdb9696 3510e7f
Return 0 for periodic stats if device off
sdb9696 87dc9ca
Merge branch 'master' into feat/common_energy
sdb9696 2daba59
Update post review
sdb9696 4c120c4
Replace has attributes with supported flags
sdb9696 b98b3f7
Update post review
sdb9696 323ead4
Merge remote-tracking branch 'upstream/master' into feat/common_energy
sdb9696 2fd5a1b
Merge remote-tracking branch 'upstream/master' into feat/common_energy
sdb9696 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| """Module for base energy module.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from enum import IntFlag, auto | ||
| from warnings import warn | ||
|
|
||
| from ..emeterstatus import EmeterStatus | ||
| from ..feature import Feature | ||
| from ..module import Module | ||
|
|
||
|
|
||
| class Energy(Module, ABC): | ||
| """Base interface to represent an Energy module.""" | ||
|
|
||
| class ModuleFeature(IntFlag): | ||
| """Features supported by the device.""" | ||
|
|
||
| #: Device reports :attr:`voltage` and :attr:`current` | ||
| VOLTAGE_CURRENT = auto() | ||
| #: Device reports :attr:`consumption_total` | ||
| CONSUMPTION_TOTAL = auto() | ||
| #: Device reports periodic stats via :meth:`get_daily_stats` | ||
| #: and :meth:`get_monthly_stats` | ||
| PERIODIC_STATS = auto() | ||
|
|
||
| _supported: ModuleFeature = ModuleFeature(0) | ||
|
|
||
| def supports(self, module_feature: ModuleFeature) -> bool: | ||
| """Return True if module supports the feature.""" | ||
| return module_feature in self._supported | ||
|
|
||
| def _initialize_features(self): | ||
| """Initialize features.""" | ||
| device = self._device | ||
| self._add_feature( | ||
| Feature( | ||
| device, | ||
| name="Current consumption", | ||
| attribute_getter="current_consumption", | ||
| container=self, | ||
| unit="W", | ||
| id="current_consumption", | ||
| precision_hint=1, | ||
| category=Feature.Category.Primary, | ||
| ) | ||
| ) | ||
| self._add_feature( | ||
| Feature( | ||
| device, | ||
| name="Today's consumption", | ||
| attribute_getter="consumption_today", | ||
| container=self, | ||
| unit="kWh", | ||
| id="consumption_today", | ||
| precision_hint=3, | ||
| category=Feature.Category.Info, | ||
| ) | ||
| ) | ||
| self._add_feature( | ||
| Feature( | ||
| device, | ||
| id="consumption_this_month", | ||
| name="This month's consumption", | ||
| attribute_getter="consumption_this_month", | ||
| container=self, | ||
| unit="kWh", | ||
| precision_hint=3, | ||
| category=Feature.Category.Info, | ||
| ) | ||
| ) | ||
| if self.supports(self.ModuleFeature.CONSUMPTION_TOTAL): | ||
| self._add_feature( | ||
| Feature( | ||
| device, | ||
| name="Total consumption since reboot", | ||
| attribute_getter="consumption_total", | ||
| container=self, | ||
| unit="kWh", | ||
| id="consumption_total", | ||
| precision_hint=3, | ||
| category=Feature.Category.Info, | ||
| ) | ||
| ) | ||
| if self.supports(self.ModuleFeature.VOLTAGE_CURRENT): | ||
| self._add_feature( | ||
| Feature( | ||
| device, | ||
| name="Voltage", | ||
| attribute_getter="voltage", | ||
| container=self, | ||
| unit="V", | ||
| id="voltage", | ||
| precision_hint=1, | ||
| category=Feature.Category.Primary, | ||
| ) | ||
| ) | ||
| self._add_feature( | ||
| Feature( | ||
| device, | ||
| name="Current", | ||
| attribute_getter="current", | ||
| container=self, | ||
| unit="A", | ||
| id="current", | ||
| precision_hint=2, | ||
| category=Feature.Category.Primary, | ||
| ) | ||
| ) | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def status(self) -> EmeterStatus: | ||
| """Return current energy readings.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def current_consumption(self) -> float | None: | ||
| """Get the current power consumption in Watt.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def consumption_today(self) -> float | None: | ||
| """Return today's energy consumption in kWh.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def consumption_this_month(self) -> float | None: | ||
| """Return this month's energy consumption in kWh.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def consumption_total(self) -> float | None: | ||
| """Return total consumption since last reboot in kWh.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def current(self) -> float | None: | ||
| """Return the current in A.""" | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def voltage(self) -> float | None: | ||
| """Get the current voltage in V.""" | ||
|
|
||
| @abstractmethod | ||
| async def get_status(self): | ||
| """Return real-time statistics.""" | ||
|
|
||
| @abstractmethod | ||
| async def erase_stats(self): | ||
| """Erase all stats.""" | ||
|
|
||
| @abstractmethod | ||
| async def get_daily_stats(self, *, year=None, month=None, kwh=True) -> dict: | ||
| """Return daily stats for the given year & month. | ||
|
|
||
| The return value is a dictionary of {day: energy, ...}. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| async def get_monthly_stats(self, *, year=None, kwh=True) -> dict: | ||
| """Return monthly stats for the given year.""" | ||
|
|
||
| _deprecated_attributes = { | ||
| "emeter_today": "consumption_today", | ||
| "emeter_this_month": "consumption_this_month", | ||
| "realtime": "status", | ||
| "get_realtime": "get_status", | ||
| "erase_emeter_stats": "erase_stats", | ||
| "get_daystat": "get_daily_stats", | ||
| "get_monthstat": "get_monthly_stats", | ||
| } | ||
|
|
||
| def __getattr__(self, name): | ||
| if attr := self._deprecated_attributes.get(name): | ||
| msg = f"{name} is deprecated, use {attr} instead" | ||
| warn(msg, DeprecationWarning, stacklevel=1) | ||
| return getattr(self, attr) | ||
| raise AttributeError(f"Energy module has no attribute {name!r}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.