python-homewizard-energy 8.0.0a1__tar.gz → 8.0.0a2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (17) hide show
  1. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/PKG-INFO +1 -1
  2. python_homewizard_energy-8.0.0a2/homewizard_energy/const.py +45 -0
  3. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/homewizard_energy.py +37 -3
  4. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/models.py +24 -1
  5. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/v1/__init__.py +42 -16
  6. python_homewizard_energy-8.0.0a2/homewizard_energy/v1/const.py +1 -0
  7. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/v2/__init__.py +2 -8
  8. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/pyproject.toml +1 -1
  9. python_homewizard_energy-8.0.0a1/homewizard_energy/const.py +0 -28
  10. python_homewizard_energy-8.0.0a1/homewizard_energy/v1/const.py +0 -6
  11. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/LICENSE +0 -0
  12. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/README.md +0 -0
  13. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/__init__.py +0 -0
  14. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/errors.py +0 -0
  15. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/py.typed +0 -0
  16. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/v2/cacert.py +0 -0
  17. {python_homewizard_energy-8.0.0a1 → python_homewizard_energy-8.0.0a2}/homewizard_energy/v2/const.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-homewizard-energy
3
- Version: 8.0.0a1
3
+ Version: 8.0.0a2
4
4
  Summary: Asynchronous Python client for the HomeWizard Energy
5
5
  Home-page: https://github.com/homewizard/python-homewizard-energy
6
6
  License: Apache-2.0
@@ -0,0 +1,45 @@
1
+ """Constants for HomeWizard Energy."""
2
+
3
+ import logging
4
+ from enum import StrEnum
5
+
6
+ LOGGER = logging.getLogger(__name__)
7
+
8
+
9
+ class Model(StrEnum):
10
+ """Model of the HomeWizard Energy device."""
11
+
12
+ P1_METER = "HWE-P1"
13
+ ENERGY_SOCKET = "HWE-SKT"
14
+ WATER_METER = "HWE-WTR"
15
+ DISPLAY = "HWE-DSP"
16
+ ENERGY_METER_1_PHASE = "HWE-KWH1"
17
+ ENERGY_METER_3_PHASE = "HWE-KWH3"
18
+ ENERGY_METER_EASTRON_SDM230 = "SDM230-wifi"
19
+ ENERGY_METER_EASTRON_SDM630 = "SDM630-wifi"
20
+ BATTERY = "HWE-BAT"
21
+
22
+
23
+ MODEL_TO_ID = {
24
+ Model.P1_METER: "p1dongle",
25
+ Model.ENERGY_SOCKET: "energysocket",
26
+ Model.WATER_METER: "watermeter",
27
+ Model.DISPLAY: "display",
28
+ Model.ENERGY_METER_1_PHASE: "energymeter",
29
+ Model.ENERGY_METER_3_PHASE: "energymeter",
30
+ Model.ENERGY_METER_EASTRON_SDM230: "energymeter",
31
+ Model.ENERGY_METER_EASTRON_SDM630: "energymeter",
32
+ Model.BATTERY: "battery",
33
+ }
34
+
35
+ MODEL_TO_NAME = {
36
+ Model.P1_METER: "Wi-Fi P1 Meter",
37
+ Model.ENERGY_SOCKET: "Wi-Fi Energy Socket",
38
+ Model.WATER_METER: "Wi-Fi Watermeter",
39
+ Model.DISPLAY: "Energy Display",
40
+ Model.ENERGY_METER_1_PHASE: "Wi-Fi kWh Meter 1-phase",
41
+ Model.ENERGY_METER_3_PHASE: "Wi-Fi kWh Meter 3-phase",
42
+ Model.ENERGY_METER_EASTRON_SDM230: "Wi-Fi kWh Meter 1-phase",
43
+ Model.ENERGY_METER_EASTRON_SDM630: "Wi-Fi kWh Meter 3-phase",
44
+ Model.BATTERY: "Plug-In Battery",
45
+ }
@@ -2,12 +2,22 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import asyncio
5
6
  from typing import Any
6
7
 
7
8
  from aiohttp.client import ClientSession
8
9
 
9
10
  from .const import LOGGER
10
- from .models import Device, Measurement, State, StateUpdate, System, SystemUpdate
11
+ from .errors import UnsupportedError
12
+ from .models import (
13
+ CombinedModels,
14
+ Device,
15
+ Measurement,
16
+ State,
17
+ StateUpdate,
18
+ System,
19
+ SystemUpdate,
20
+ )
11
21
 
12
22
 
13
23
  class HomeWizardEnergy:
@@ -18,6 +28,8 @@ class HomeWizardEnergy:
18
28
  _request_timeout: int = 10
19
29
  _host: str
20
30
 
31
+ _device: Device | None = None
32
+
21
33
  def __init__(
22
34
  self,
23
35
  host: str,
@@ -46,6 +58,28 @@ class HomeWizardEnergy:
46
58
  """
47
59
  return self._host
48
60
 
61
+ async def combined(self) -> CombinedModels:
62
+ """Get all information."""
63
+
64
+ async def fetch_data(coroutine):
65
+ try:
66
+ return await coroutine
67
+ except (UnsupportedError, NotImplementedError):
68
+ return None
69
+
70
+ if self._device is None:
71
+ self._device = await fetch_data(self.device())
72
+
73
+ measurement, system, state = await asyncio.gather(
74
+ fetch_data(self.measurement()),
75
+ fetch_data(self.system()),
76
+ fetch_data(self.state()),
77
+ )
78
+
79
+ return CombinedModels(
80
+ device=self._device, measurement=measurement, system=system, state=state
81
+ )
82
+
49
83
  async def device(self) -> Device:
50
84
  """Get the device information."""
51
85
  raise NotImplementedError
@@ -66,7 +100,7 @@ class HomeWizardEnergy:
66
100
  update: StateUpdate | None = None,
67
101
  ) -> State:
68
102
  """Get/set the state."""
69
- raise NotImplementedError
103
+ raise UnsupportedError("State is not supported")
70
104
 
71
105
  async def identify(
72
106
  self,
@@ -78,7 +112,7 @@ class HomeWizardEnergy:
78
112
  self,
79
113
  ) -> None:
80
114
  """Reboot the device."""
81
- raise NotImplementedError
115
+ raise UnsupportedError("Reboot is not supported")
82
116
 
83
117
  async def close(self) -> None:
84
118
  """Close client session."""
@@ -11,7 +11,7 @@ from mashumaro.config import BaseConfig
11
11
  from mashumaro.exceptions import MissingField
12
12
  from mashumaro.mixins.orjson import DataClassORJSONMixin
13
13
 
14
- from .const import LOGGER, MODEL_TO_ID, MODEL_TO_NAME
14
+ from .const import LOGGER, MODEL_TO_ID, MODEL_TO_NAME, Model
15
15
 
16
16
 
17
17
  class BaseModel(DataClassORJSONMixin):
@@ -38,6 +38,16 @@ class UpdateBaseModel(BaseModel):
38
38
  return d
39
39
 
40
40
 
41
+ @dataclass(kw_only=True)
42
+ class CombinedModels:
43
+ """All values."""
44
+
45
+ device: Device
46
+ measurement: Measurement | None
47
+ state: State | None
48
+ system: System | None
49
+
50
+
41
51
  def get_verification_hostname(model: str, serial_number: str) -> str:
42
52
  """Helper method to convert device model and serial to identifier
43
53
 
@@ -72,6 +82,19 @@ class Device(BaseModel):
72
82
  obj.id = get_verification_hostname(obj.product_type, obj.serial)
73
83
  return obj
74
84
 
85
+ def supports_state(self) -> bool:
86
+ """Return if the device supports state."""
87
+ return self.product_type == Model.ENERGY_SOCKET
88
+
89
+ def support_identify(self) -> bool:
90
+ """Return if the device supports identify."""
91
+ return self.product_type not in (
92
+ Model.ENERGY_METER_1_PHASE,
93
+ Model.ENERGY_METER_3_PHASE,
94
+ Model.ENERGY_METER_EASTRON_SDM230,
95
+ Model.ENERGY_METER_EASTRON_SDM630,
96
+ )
97
+
75
98
 
76
99
  @dataclass(kw_only=True)
77
100
  class Measurement(BaseModel):
@@ -12,17 +12,18 @@ import backoff
12
12
  from aiohttp.client import ClientError, ClientResponseError, ClientSession
13
13
  from aiohttp.hdrs import METH_GET, METH_PUT
14
14
 
15
- from homewizard_energy.errors import (
16
- DisabledError,
17
- NotFoundError,
18
- RequestError,
19
- UnsupportedError,
20
- )
21
-
22
15
  from ..const import LOGGER
16
+ from ..errors import DisabledError, NotFoundError, RequestError, UnsupportedError
23
17
  from ..homewizard_energy import HomeWizardEnergy
24
- from ..models import Device, Measurement, State, StateUpdate, System, SystemUpdate
25
- from .const import SUPPORTED_API_VERSION
18
+ from ..models import (
19
+ CombinedModels,
20
+ Device,
21
+ Measurement,
22
+ State,
23
+ StateUpdate,
24
+ System,
25
+ SystemUpdate,
26
+ )
26
27
 
27
28
  T = TypeVar("T")
28
29
 
@@ -45,16 +46,33 @@ def optional_method(
45
46
  class HomeWizardEnergyV1(HomeWizardEnergy):
46
47
  """Communicate with a HomeWizard Energy device."""
47
48
 
49
+ async def combined(self) -> CombinedModels:
50
+ """Get all information."""
51
+
52
+ models: CombinedModels = await super().combined()
53
+
54
+ # Move things around for backwards compatibility
55
+ ## measurement.wifi_ssid -> system.wifi_ssid
56
+ if models.measurement is not None and models.measurement.wifi_ssid is not None:
57
+ if models.system is None:
58
+ models.system = System()
59
+ models.system.wifi_ssid = models.measurement.wifi_ssid
60
+
61
+ ## state.brightness -> system.status_led_brightness_pct
62
+ if models.state is not None and models.state.brightness is not None:
63
+ if models.system is None:
64
+ models.system = System()
65
+ models.system.status_led_brightness_pct = (
66
+ models.state.brightness / 255
67
+ ) * 100
68
+
69
+ return models
70
+
48
71
  async def device(self) -> Device:
49
72
  """Return the device object."""
50
73
  _, response = await self._request("api")
51
74
  device = Device.from_json(response)
52
75
 
53
- if device.api_version != SUPPORTED_API_VERSION:
54
- raise UnsupportedError(
55
- f"Unsupported API version, expected version '{SUPPORTED_API_VERSION}'"
56
- )
57
-
58
76
  return device
59
77
 
60
78
  async def measurement(self) -> Measurement:
@@ -64,7 +82,11 @@ class HomeWizardEnergyV1(HomeWizardEnergy):
64
82
 
65
83
  @optional_method
66
84
  async def state(self, update: StateUpdate | None = None) -> State:
67
- """Return the state object."""
85
+ """Return or update the state object."""
86
+
87
+ if self._device is not None and self._device.supports_state is False:
88
+ raise UnsupportedError("State is not supported")
89
+
68
90
  if update is not None:
69
91
  data = update.to_dict()
70
92
  status, response = await self._request(
@@ -111,10 +133,14 @@ class HomeWizardEnergyV1(HomeWizardEnergy):
111
133
  self,
112
134
  ) -> bool:
113
135
  """Send identify request."""
136
+
137
+ if self._device is not None and self._device.supports_state is False:
138
+ raise UnsupportedError("State is not supported")
139
+
114
140
  await self._request("api/v1/identify", method=METH_PUT)
115
141
  return True
116
142
 
117
- @backoff.on_exception(backoff.expo, RequestError, max_tries=5, logger=None)
143
+ @backoff.on_exception(backoff.expo, RequestError, max_tries=3, logger=None)
118
144
  async def _request(
119
145
  self, path: str, method: str = METH_GET, data: object = None
120
146
  ) -> tuple[HTTPStatus, dict[str, Any] | None]:
@@ -0,0 +1 @@
1
+ """Constants for HomeWizard Energy v1."""
@@ -21,14 +21,8 @@ from aiohttp.client import (
21
21
  from aiohttp.hdrs import METH_DELETE, METH_GET, METH_POST, METH_PUT
22
22
  from mashumaro.exceptions import InvalidFieldValue, MissingField
23
23
 
24
- from homewizard_energy.errors import (
25
- DisabledError,
26
- RequestError,
27
- ResponseError,
28
- UnauthorizedError,
29
- )
30
-
31
24
  from ..const import LOGGER
25
+ from ..errors import DisabledError, RequestError, ResponseError, UnauthorizedError
32
26
  from ..homewizard_energy import HomeWizardEnergy
33
27
  from ..models import Device, Measurement, System, SystemUpdate, Token
34
28
  from .cacert import CACERT
@@ -211,7 +205,7 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
211
205
  connector=connector, timeout=ClientTimeout(total=self._request_timeout)
212
206
  )
213
207
 
214
- @backoff.on_exception(backoff.expo, RequestError, max_tries=5, logger=None)
208
+ @backoff.on_exception(backoff.expo, RequestError, max_tries=3, logger=None)
215
209
  async def _request(
216
210
  self, path: str, method: str = METH_GET, data: object = None
217
211
  ) -> tuple[HTTPStatus, dict[str, Any] | None]:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-homewizard-energy"
3
- version = "8.0.0-alpha-1"
3
+ version = "8.0.0-alpha-2"
4
4
  description = "Asynchronous Python client for the HomeWizard Energy"
5
5
  authors = ["DCSBL"]
6
6
  maintainers = ["DCSBL"]
@@ -1,28 +0,0 @@
1
- """Constants for HomeWizard Energy."""
2
-
3
- import logging
4
-
5
- LOGGER = logging.getLogger(__name__)
6
-
7
- MODEL_TO_ID = {
8
- "HWE-P1": "p1dongle",
9
- "HWE-SKT": "energysocket",
10
- "HWE-WTR": "watermeter",
11
- "HWE-DSP": "display",
12
- "HWE-KWH1": "energymeter",
13
- "SDM230-wifi": "energymeter",
14
- "HWE-KWH3": "energymeter",
15
- "SDM630-wifi": "energymeter",
16
- "HWE-BAT": "battery",
17
- }
18
-
19
- MODEL_TO_NAME = {
20
- "HWE-P1": "Wi-Fi P1 Meter",
21
- "HWE-SKT": "Wi-Fi Energy Socket",
22
- "HWE-WTR": "Wi-Fi Watermeter",
23
- "HWE-KWH1": "Wi-Fi kWh Meter 1-phase",
24
- "HWE-KWH3": "Wi-Fi kWh Meter 3-phase",
25
- "SDM230-wifi": "Wi-Fi kWh Meter 1-phase",
26
- "SDM630-wifi": "Wi-Fi kWh Meter 3-phase",
27
- "HWE-BAT": "Plug-In Battery",
28
- }
@@ -1,6 +0,0 @@
1
- """Constants for HomeWizard Energy v1."""
2
-
3
- SUPPORTED_API_VERSION = "v1"
4
-
5
- SUPPORTS_STATE = ["HWE-SKT"]
6
- SUPPORTS_IDENTIFY = ["HWE-SKT", "HWE-P1", "HWE-WTR"]