python-homewizard-energy 7.1.0b1__tar.gz → 8.0.0__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 (20) hide show
  1. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/PKG-INFO +15 -8
  2. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/README.md +11 -6
  3. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/homewizard_energy/__init__.py +6 -24
  4. python_homewizard_energy-8.0.0/homewizard_energy/const.py +45 -0
  5. python_homewizard_energy-8.0.0/homewizard_energy/homewizard_energy.py +150 -0
  6. python_homewizard_energy-8.0.0/homewizard_energy/models.py +528 -0
  7. python_homewizard_energy-8.0.0/homewizard_energy/v1/__init__.py +175 -0
  8. python_homewizard_energy-8.0.0/homewizard_energy/v1/const.py +1 -0
  9. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/homewizard_energy/v2/__init__.py +57 -91
  10. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/pyproject.toml +8 -5
  11. python_homewizard_energy-7.1.0b1/homewizard_energy/brand.py +0 -95
  12. python_homewizard_energy-7.1.0b1/homewizard_energy/v1/__init__.py +0 -227
  13. python_homewizard_energy-7.1.0b1/homewizard_energy/v1/const.py +0 -6
  14. python_homewizard_energy-7.1.0b1/homewizard_energy/v1/models.py +0 -359
  15. python_homewizard_energy-7.1.0b1/homewizard_energy/v2/models.py +0 -318
  16. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/LICENSE +0 -0
  17. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/homewizard_energy/errors.py +0 -0
  18. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/homewizard_energy/py.typed +0 -0
  19. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/homewizard_energy/v2/cacert.py +0 -0
  20. {python_homewizard_energy-7.1.0b1 → python_homewizard_energy-8.0.0}/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: 7.1.0b1
3
+ Version: 8.0.0
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
@@ -12,9 +12,11 @@ Classifier: Programming Language :: Python :: 3
12
12
  Classifier: Programming Language :: Python :: 3.12
13
13
  Classifier: Programming Language :: Python :: 3.13
14
14
  Requires-Dist: aiohttp (>=3.0.0)
15
- Requires-Dist: async-timeout (>=4.0.3,<5.0.0)
15
+ Requires-Dist: async-timeout (>=4.0.3,<6.0.0)
16
16
  Requires-Dist: backoff (>=2.2.1,<3.0.0)
17
+ Requires-Dist: mashumaro (>=3.15,<4.0)
17
18
  Requires-Dist: multidict (>=6.0.5,<7.0.0)
19
+ Requires-Dist: orjson (>=3.10,<4.0)
18
20
  Project-URL: Bug Tracker, https://github.com/homewizard/python-homewizard-energy/issues
19
21
  Project-URL: Changelog, https://github.com/homewizard/python-homewizard-energy/releases
20
22
  Project-URL: Documentation, https://github.com/homewizard/python-homewizard-energy
@@ -42,6 +44,8 @@ python3 -m pip install python-homewizard-energy
42
44
  ```
43
45
 
44
46
  ## Example
47
+
48
+ ### API v1
45
49
  ```python
46
50
  import asyncio
47
51
  from homewizard_energy import HomeWizardEnergyV1V1
@@ -53,15 +57,18 @@ async def main():
53
57
 
54
58
  async with HomeWizardEnergyV1(host=IP_ADDRESS) as api:
55
59
 
56
- # Get device information, like firmware version
60
+ # Get device information, like firmware version
57
61
  print(await api.device())
58
62
 
59
- # Get measurements, like energy or water usage
60
- data = await api.data()
61
- print(data.total_energy_import_kwh)
63
+ # Get measurements, like energy or water usage
64
+ measurement = await api.measurement()
65
+ print(measurement.energy_import_kwh)
66
+
67
+ # Get all data and remap v1 data to new v2 structure
68
+ print(await api.combined())
62
69
 
63
- # Turn on the Energy Socket outlet
64
- await api.state_set(power_on=True)
70
+ # Turn on the Energy Socket outlet
71
+ await api.state(power_on=True)
65
72
 
66
73
 
67
74
  asyncio.run(main())
@@ -19,6 +19,8 @@ python3 -m pip install python-homewizard-energy
19
19
  ```
20
20
 
21
21
  ## Example
22
+
23
+ ### API v1
22
24
  ```python
23
25
  import asyncio
24
26
  from homewizard_energy import HomeWizardEnergyV1V1
@@ -30,15 +32,18 @@ async def main():
30
32
 
31
33
  async with HomeWizardEnergyV1(host=IP_ADDRESS) as api:
32
34
 
33
- # Get device information, like firmware version
35
+ # Get device information, like firmware version
34
36
  print(await api.device())
35
37
 
36
- # Get measurements, like energy or water usage
37
- data = await api.data()
38
- print(data.total_energy_import_kwh)
38
+ # Get measurements, like energy or water usage
39
+ measurement = await api.measurement()
40
+ print(measurement.energy_import_kwh)
41
+
42
+ # Get all data and remap v1 data to new v2 structure
43
+ print(await api.combined())
39
44
 
40
- # Turn on the Energy Socket outlet
41
- await api.state_set(power_on=True)
45
+ # Turn on the Energy Socket outlet
46
+ await api.state(power_on=True)
42
47
 
43
48
 
44
49
  asyncio.run(main())
@@ -3,11 +3,13 @@
3
3
  from aiohttp import ClientSession
4
4
 
5
5
  from .errors import DisabledError, InvalidStateError, RequestError, UnsupportedError
6
+ from .homewizard_energy import HomeWizardEnergy
6
7
  from .v1 import HomeWizardEnergyV1
7
8
  from .v2 import HomeWizardEnergyV2
8
9
 
9
10
  __all__ = [
10
11
  "DisabledError",
12
+ "HomeWizardEnergy",
11
13
  "HomeWizardEnergyV1",
12
14
  "HomeWizardEnergyV2",
13
15
  "InvalidStateError",
@@ -25,33 +27,13 @@ async def has_v2_api(host: str, websession: ClientSession | None = None) -> bool
25
27
  # v2 api is https only and returns a 401 Unauthorized when no key provided,
26
28
  # no connection can be made if the device is not v2
27
29
  url = f"https://{host}/api"
28
- async with websession.get(
29
- url, ssl=False, raise_for_status=False, timeout=15
30
- ) as res:
31
- return res.status == 401
30
+ res = await websession.get(url, ssl=False, raise_for_status=False, timeout=5)
31
+ res.close()
32
+
33
+ return res.status == 401
32
34
  except Exception: # pylint: disable=broad-except
33
35
  # all other status/exceptions means the device is not v2 or not reachable at this time
34
36
  return False
35
37
  finally:
36
38
  if not websession_provided:
37
39
  await websession.close()
38
-
39
-
40
- def get_verification_hostname(model: str, serial_number: str) -> str:
41
- """Get the verification name for the device."""
42
- model_map = {
43
- "HWE-P1": "p1dongle",
44
- "HWE-SKT": "energysocket",
45
- "HWE-WTR": "watermeter",
46
- "HWE-DSP": "display",
47
- "HWE-KWH1": "energymeter",
48
- "SDM230-wifi": "energymeter",
49
- "HWE-KWH3": "energymeter",
50
- "SDM630-wifi": "energymeter",
51
- "HWE-BAT": "battery",
52
- }
53
-
54
- if model not in model_map:
55
- raise ValueError(f"Unsupported model: {model}")
56
-
57
- return f"appliance/{model_map[model]}/{serial_number}"
@@ -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
+ }
@@ -0,0 +1,150 @@
1
+ """HomeWizard Energy base class"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ from typing import Any
7
+
8
+ from aiohttp.client import ClientSession, ClientTimeout, TCPConnector
9
+
10
+ from .const import LOGGER
11
+ from .errors import UnsupportedError
12
+ from .models import CombinedModels, Device, Measurement, State, System
13
+
14
+
15
+ class HomeWizardEnergy:
16
+ """Base class for HomeWizard Energy API."""
17
+
18
+ _session: ClientSession | None = None
19
+ _close_session: bool = False
20
+ _request_timeout: int = 10
21
+ _host: str
22
+
23
+ _device: Device | None = None
24
+
25
+ def __init__(
26
+ self,
27
+ host: str,
28
+ clientsession: ClientSession = None,
29
+ timeout: int = 10,
30
+ ):
31
+ """Create a HomeWizard Energy object.
32
+
33
+ Args:
34
+ host: IP or URL for device.
35
+ clientsession: The clientsession.
36
+ timeout: Request timeout in seconds.
37
+ """
38
+ self._host = host
39
+ self._session = clientsession
40
+ self._close_session = clientsession is None
41
+ self._request_timeout = timeout
42
+
43
+ @property
44
+ def host(self) -> str:
45
+ """Return the hostname of the device.
46
+
47
+ Returns:
48
+ host: The used host
49
+
50
+ """
51
+ return self._host
52
+
53
+ async def combined(self) -> CombinedModels:
54
+ """Get all information."""
55
+
56
+ async def fetch_data(coroutine):
57
+ try:
58
+ return await coroutine
59
+ except (UnsupportedError, NotImplementedError):
60
+ return None
61
+
62
+ device, measurement, system, state = await asyncio.gather(
63
+ fetch_data(self.device()),
64
+ fetch_data(self.measurement()),
65
+ fetch_data(self.system()),
66
+ fetch_data(self.state()),
67
+ )
68
+
69
+ return CombinedModels(
70
+ device=device, measurement=measurement, system=system, state=state
71
+ )
72
+
73
+ async def device(self, reset_cache: bool = False) -> Device:
74
+ """Get the device information."""
75
+ raise NotImplementedError
76
+
77
+ async def measurement(self) -> Measurement:
78
+ """Get the current measurement."""
79
+ raise NotImplementedError
80
+
81
+ async def system(
82
+ self,
83
+ cloud_enabled: bool | None = None,
84
+ status_led_brightness_pct: int | None = None,
85
+ api_v1_enabled: bool | None = None,
86
+ ) -> System:
87
+ """Get/set the system."""
88
+ raise NotImplementedError
89
+
90
+ async def state(
91
+ self,
92
+ power_on: bool | None = None,
93
+ switch_lock: bool | None = None,
94
+ brightness: int | None = None,
95
+ ) -> State:
96
+ """Get/set the state."""
97
+ raise UnsupportedError("State is not supported")
98
+
99
+ async def identify(
100
+ self,
101
+ ) -> None:
102
+ """Identify the device."""
103
+ raise NotImplementedError
104
+
105
+ async def reboot(
106
+ self,
107
+ ) -> None:
108
+ """Reboot the device."""
109
+ raise UnsupportedError("Reboot is not supported")
110
+
111
+ async def close(self) -> None:
112
+ """Close client session."""
113
+ LOGGER.debug("Closing clientsession")
114
+ if self._session and self._close_session:
115
+ await self._session.close()
116
+
117
+ async def _create_clientsession(self) -> None:
118
+ """Create a client session."""
119
+
120
+ if self._session is not None:
121
+ raise RuntimeError("Session already exists")
122
+
123
+ connector = TCPConnector(
124
+ enable_cleanup_closed=True,
125
+ limit_per_host=1,
126
+ )
127
+
128
+ self._close_session = True
129
+
130
+ self._session = ClientSession(
131
+ connector=connector, timeout=ClientTimeout(total=self._request_timeout)
132
+ )
133
+
134
+ # self._session = self._create_clientsession(connector, timeout=ClientTimeout(total=self._request_timeout))
135
+
136
+ async def __aenter__(self) -> HomeWizardEnergy:
137
+ """Async enter.
138
+
139
+ Returns:
140
+ The HomeWizardEnergy object.
141
+ """
142
+ return self
143
+
144
+ async def __aexit__(self, *_exc_info: Any) -> None:
145
+ """Async exit.
146
+
147
+ Args:
148
+ _exc_info: Exec type.
149
+ """
150
+ await self.close()