|
| 1 | +"""Module for base energy module.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from abc import ABC, abstractmethod |
| 6 | +from enum import IntFlag, auto |
| 7 | +from warnings import warn |
| 8 | + |
| 9 | +from ..emeterstatus import EmeterStatus |
| 10 | +from ..feature import Feature |
| 11 | +from ..module import Module |
| 12 | + |
| 13 | + |
| 14 | +class Energy(Module, ABC): | <
10000
/tr>
| 15 | + """Base interface to represent an Energy module.""" |
| 16 | + |
| 17 | + class ModuleFeature(IntFlag): |
| 18 | + """Features supported by the device.""" |
| 19 | + |
| 20 | + #: Device reports :attr:`voltage` and :attr:`current` |
| 21 | + VOLTAGE_CURRENT = auto() |
| 22 | + #: Device reports :attr:`consumption_total` |
| 23 | + CONSUMPTION_TOTAL = auto() |
| 24 | + #: Device reports periodic stats via :meth:`get_daily_stats` |
| 25 | + #: and :meth:`get_monthly_stats` |
| 26 | + PERIODIC_STATS = auto() |
| 27 | + |
| 28 | + _supported: ModuleFeature = ModuleFeature(0) |
| 29 | + |
| 30 | + def supports(self, module_feature: ModuleFeature) -> bool: |
| 31 | + """Return True if module supports the feature.""" |
| 32 | + return module_feature in self._supported |
| 33 | + |
| 34 | + def _initialize_features(self): |
| 35 | + """Initialize features.""" |
| 36 | + device = self._device |
| 37 | + self._add_feature( |
| 38 | + Feature( |
| 39 | + device, |
| 40 | + name="Current consumption", |
| 41 | + attribute_getter="current_consumption", |
| 42 | + container=self, |
| 43 | + unit="W", |
| 44 | + id="current_consumption", |
| 45 | + precision_hint=1, |
| 46 | + category=Feature.Category.Primary, |
| 47 | + ) |
| 48 | + ) |
| 49 | + self._add_feature( |
| 50 | + Feature( |
| 51 | + device, |
| 52 | + name="Today's consumption", |
| 53 | + attribute_getter="consumption_today", |
| 54 | + container=self, |
| 55 | + unit="kWh", |
| 56 | + id="consumption_today", |
| 57 | + precision_hint=3, |
| 58 | + category=Feature.Category.Info, |
| 59 | + ) |
| 60 | + ) |
| 61 | + self._add_feature( |
| 62 | + Feature( |
| 63 | + device, |
| 64 | + id="consumption_this_month", |
| 65 | + name="This month's consumption", |
| 66 | + attribute_getter="consumption_this_month", |
| 67 | + container=self, |
| 68 | + unit="kWh", |
| 69 | + precision_hint=3, |
| 70 | + category=Feature.Category.Info, |
| 71 | + ) |
| 72 | + ) |
| 73 | + if self.supports(self.ModuleFeature.CONSUMPTION_TOTAL): |
| 74 | + self._add_feature( |
| 75 | + Feature( |
| 76 | + device, |
| 77 | + name="Total consumption since reboot", |
| 78 | + attribute_getter="consumption_total", |
| 79 | + container=self, |
| 80 | + unit="kWh", |
| 81 | + id="consumption_total", |
| 82 | + precision_hint=3, |
| 83 | + category=Feature.Category.Info, |
| 84 | + ) |
| 85 | + ) |
| 86 | + if self.supports(self.ModuleFeature.VOLTAGE_CURRENT): |
| 87 | + self._add_feature( |
| 88 | + Feature( |
| 89 | + device, |
| 90 | + name="Voltage", |
| 91 | + attribute_getter="voltage", |
| 92 | + container=self, |
| 93 | + unit="V", |
| 94 | + id="voltage", |
| 95 | + precision_hint=1, |
| 96 | + category=Feature.Category.Primary, |
| 97 | + ) |
| 98 | + ) |
| 99 | + self._add_feature( |
| 100 | + Feature( |
| 101 | + device, |
| 102 | + name="Current", |
| 103 | + attribute_getter="current", |
| 104 | + container=self, |
| 105 | + unit="A", |
| 106 | + id="current", |
| 107 | + precision_hint=2, |
| 108 | + category=Feature.Category.Primary, |
| 109 | + ) |
| 110 | + ) |
| 111 | + |
| 112 | + @property |
| 113 | + @abstractmethod |
| 114 | + def status(self) -> EmeterStatus: |
| 115 | + """Return current energy readings.""" |
| 116 | + |
| 117 | + @property |
| 118 | + @abstractmethod |
| 119 | + def current_consumption(self) -> float | None: |
| 120 | + """Get the current power consumption in Watt.""" |
| 121 | + |
| 122 | + @property |
| 123 | + @abstractmethod |
| 124 | + def consumption_today(self) -> float | None: |
| 125 | + """Return today's energy consumption in kWh.""" |
| 126 | + |
| 127 | + @property |
| 128 | + @abstractmethod |
| 129 | + def consumption_this_month(self) -> float | None: |
| 130 | + """Return this month's energy consumption in kWh.""" |
| 131 | + |
| 132 | + @property |
| 133 | + @abstractmethod |
| 134 | + def consumption_total(self) -> float | None: |
| 135 | + """Return total consumption since last reboot in kWh.""" |
| 136 | + |
| 137 | + @property |
| 138 | + @abstractmethod |
| 139 | + def current(self) -> float | None: |
| 140 | + """Return the current in A.""" |
| 141 | + |
| 142 | + @property |
| 143 | + @abstractmethod |
| 144 | + def voltage(self) -> float | None: |
| 145 | + """Get the current voltage in V.""" |
| 146 | + |
| 147 | + @abstractmethod |
| 148 | + async def get_status(self): |
| 149 | + """Return real-time statistics.""" |
| 150 | + |
| 151 | + @abstractmethod |
| 152 | + async def erase_stats(self): |
| 153 | + """Erase all stats.""" |
| 154 | + |
| 155 | + @abstractmethod |
| 156 | + async def get_daily_stats(self, *, year=None, month=None, kwh=True) -> dict: |
| 157 | + """Return daily stats for the given year & month. |
| 158 | +
|
| 159 | + The return value is a dictionary of {day: energy, ...}. |
| 160 | + """ |
| 161 | + |
| 162 | + @abstractmethod |
| 163 | + async def get_monthly_stats(self, *, year=None, kwh=True) -> dict: |
| 164 | + """Return monthly stats for the given year.""" |
| 165 | + |
| 166 | + _deprecated_attributes = { |
| 167 | + "emeter_today": "consumption_today", |
| 168 | + "emeter_this_month": "consumption_this_month", |
| 169 | + "realtime": "status", |
| 170 | + "get_realtime": "get_status", |
| 171 | + "erase_emeter_stats": "erase_stats", |
| 172 | + "get_daystat": "get_daily_stats", |
| 173 | + "get_monthstat": "get_monthly_stats", |
| 174 | + } |
| 175 | + |
| 176 | + def __getattr__(self, name): |
| 177 | + if attr := self._deprecated_attributes.get(name): |
| 178 | + msg = f"{name} is deprecated, use {attr} instead" |
| 179 | + warn(msg, DeprecationWarning, stacklevel=1) |
| 180 | + return getattr(self, attr) |
| 181 | + raise AttributeError(f"Energy module has no attribute {name!r}") |
0 commit comments