python-homewizard-energy 8.0.0a5__tar.gz → 8.0.0b1__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 (15) hide show
  1. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/PKG-INFO +12 -7
  2. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/README.md +11 -6
  3. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/homewizard_energy.py +20 -1
  4. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/models.py +14 -15
  5. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/v1/__init__.py +2 -3
  6. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/v2/__init__.py +12 -23
  7. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/pyproject.toml +1 -1
  8. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/LICENSE +0 -0
  9. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/__init__.py +0 -0
  10. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/const.py +0 -0
  11. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/errors.py +0 -0
  12. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/py.typed +0 -0
  13. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/v1/const.py +0 -0
  14. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/homewizard_energy/v2/cacert.py +0 -0
  15. {python_homewizard_energy-8.0.0a5 → python_homewizard_energy-8.0.0b1}/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.0a5
3
+ Version: 8.0.0b1
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
@@ -44,6 +44,8 @@ python3 -m pip install python-homewizard-energy
44
44
  ```
45
45
 
46
46
  ## Example
47
+
48
+ ### API v1
47
49
  ```python
48
50
  import asyncio
49
51
  from homewizard_energy import HomeWizardEnergyV1V1
@@ -55,15 +57,18 @@ async def main():
55
57
 
56
58
  async with HomeWizardEnergyV1(host=IP_ADDRESS) as api:
57
59
 
58
- # Get device information, like firmware version
60
+ # Get device information, like firmware version
59
61
  print(await api.device())
60
62
 
61
- # Get measurements, like energy or water usage
62
- data = await api.data()
63
- 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())
64
69
 
65
- # Turn on the Energy Socket outlet
66
- await api.state_set(power_on=True)
70
+ # Turn on the Energy Socket outlet
71
+ await api.state(power_on=True)
67
72
 
68
73
 
69
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())
@@ -5,7 +5,7 @@ from __future__ import annotations
5
5
  import asyncio
6
6
  from typing import Any
7
7
 
8
- from aiohttp.client import ClientSession
8
+ from aiohttp.client import ClientSession, ClientTimeout, TCPConnector
9
9
 
10
10
  from .const import LOGGER
11
11
  from .errors import UnsupportedError
@@ -114,6 +114,25 @@ class HomeWizardEnergy:
114
114
  if self._session and self._close_session:
115
115
  await self._session.close()
116
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
+
117
136
  async def __aenter__(self) -> HomeWizardEnergy:
118
137
  """Async enter.
119
138
 
@@ -385,8 +385,6 @@ class Measurement(BaseModel):
385
385
  return d
386
386
 
387
387
  d["protocol_version"] = d.get("smr_version")
388
- d["meter_model"] = d.get("meter_model")
389
- d["unique_id"] = d.get("unique_meter_id")
390
388
  d["tariff"] = d.get("active_tariff")
391
389
  d["energy_import_kwh"] = d.get(
392
390
  "total_power_import_kwh", d.get("total_power_import_t1_kwh")
@@ -427,23 +425,24 @@ class Measurement(BaseModel):
427
425
  d["power_factor_l2"] = d.get("active_power_factor_l2")
428
426
  d["power_factor_l3"] = d.get("active_power_factor_l3")
429
427
  d["frequency_hz"] = d.get("active_frequency_hz")
430
- d["voltage_sag_l1_count"] = d.get("voltage_sag_l1_count")
431
- d["voltage_sag_l2_count"] = d.get("voltage_sag_l2_count")
432
- d["voltage_sag_l3_count"] = d.get("voltage_sag_l3_count")
433
- d["voltage_swell_l1_count"] = d.get("voltage_swell_l1_count")
434
- d["voltage_swell_l2_count"] = d.get("voltage_swell_l2_count")
435
- d["voltage_swell_l3_count"] = d.get("voltage_swell_l3_count")
436
- d["any_power_fail_count"] = d.get("any_power_fail_count")
437
- d["long_power_fail_count"] = d.get("long_power_fail_count")
438
428
  d["average_power_15m_w"] = d.get("active_power_average_w")
439
429
  d["monthly_power_peak_w"] = d.get("montly_power_peak_w")
440
430
  d["monthly_power_peak_timestamp"] = d.get("montly_power_peak_timestamp")
441
- d["active_liter_lpm"] = d.get("active_liter_lpm")
442
- d["total_liter_m3"] = d.get("total_liter_m3")
443
431
  d["external_devices"] = d.get("external_devices")
444
432
 
445
433
  return d
446
434
 
435
+ @classmethod
436
+ def __post_deserialize__(cls, obj: Measurement) -> Measurement:
437
+ """Post deserialize hook for Measurement object."""
438
+ _ = cls # Unused
439
+
440
+ # Some smart meters report a tariff other than 1, 2, 3 or 4, which is invalid
441
+ if obj.tariff not in (1, 2, 3, 4):
442
+ obj.tariff = None
443
+
444
+ return obj
445
+
447
446
 
448
447
  @dataclass(kw_only=True)
449
448
  class ExternalDevice(BaseModel):
@@ -465,9 +464,9 @@ class ExternalDevice(BaseModel):
465
464
  type: DeviceType | None = field(
466
465
  default=None,
467
466
  metadata={
468
- "deserialize": lambda x: ExternalDevice.DeviceType(x)
469
- if x in ExternalDevice.DeviceType.__members__.values()
470
- else None
467
+ "deserialize": lambda x: ExternalDevice.DeviceType.__members__.get(
468
+ x.upper(), None
469
+ )
471
470
  },
472
471
  )
473
472
  value: float = field()
@@ -9,7 +9,7 @@ from typing import Any, TypeVar
9
9
 
10
10
  import async_timeout
11
11
  import backoff
12
- from aiohttp.client import ClientError, ClientResponseError, ClientSession
12
+ from aiohttp.client import ClientError, ClientResponseError
13
13
  from aiohttp.hdrs import METH_GET, METH_PUT
14
14
 
15
15
  from ..const import LOGGER
@@ -132,8 +132,7 @@ class HomeWizardEnergyV1(HomeWizardEnergy):
132
132
  """Make a request to the API."""
133
133
 
134
134
  if self._session is None:
135
- self._session = ClientSession()
136
- self._close_session = True
135
+ await self._create_clientsession()
137
136
 
138
137
  # Construct request
139
138
  url = f"http://{self.host}/{path}"
@@ -11,13 +11,7 @@ from typing import Any, TypeVar
11
11
 
12
12
  import async_timeout
13
13
  import backoff
14
- from aiohttp.client import (
15
- ClientError,
16
- ClientResponseError,
17
- ClientSession,
18
- ClientTimeout,
19
- TCPConnector,
20
- )
14
+ from aiohttp.client import ClientError, ClientResponseError, ClientSession
21
15
  from aiohttp.hdrs import METH_DELETE, METH_GET, METH_POST, METH_PUT
22
16
  from mashumaro.exceptions import InvalidFieldValue, MissingField
23
17
 
@@ -49,6 +43,9 @@ def authorized_method(
49
43
  class HomeWizardEnergyV2(HomeWizardEnergy):
50
44
  """Communicate with a HomeWizard Energy device."""
51
45
 
46
+ _ssl: ssl.SSLContext | bool = False
47
+ _identifier: str | None = None
48
+
52
49
  # pylint: disable=too-many-arguments
53
50
  # pylint: disable=too-many-positional-arguments
54
51
  def __init__(
@@ -191,7 +188,7 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
191
188
  if name is None:
192
189
  self._token = None
193
190
 
194
- async def _get_session(self) -> ClientSession:
191
+ async def _get_ssl_context(self) -> ssl.SSLContext:
195
192
  """
196
193
  Get a clientsession that is tuned for communication with the HomeWizard Energy Device
197
194
  """
@@ -208,17 +205,7 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
208
205
 
209
206
  # Creating an SSL context has some blocking IO so need to run it in the executor
210
207
  loop = asyncio.get_running_loop()
211
- context = await loop.run_in_executor(None, _build_ssl_context)
212
-
213
- connector = TCPConnector(
214
- enable_cleanup_closed=True,
215
- ssl=context,
216
- limit_per_host=1,
217
- )
218
-
219
- return ClientSession(
220
- connector=connector, timeout=ClientTimeout(total=self._request_timeout)
221
- )
208
+ return await loop.run_in_executor(None, _build_ssl_context)
222
209
 
223
210
  @backoff.on_exception(backoff.expo, RequestError, max_tries=3, logger=None)
224
211
  async def _request(
@@ -227,7 +214,10 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
227
214
  """Make a request to the API."""
228
215
 
229
216
  if self._session is None:
230
- self._session = await self._get_session()
217
+ await self._create_clientsession()
218
+
219
+ if self._ssl is False:
220
+ self._ssl = await self._get_ssl_context()
231
221
 
232
222
  # Construct request
233
223
  url = f"https://{self.host}{path}"
@@ -246,9 +236,8 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
246
236
  url,
247
237
  json=data,
248
238
  headers=headers,
249
- server_hostname=self._identifier
250
- if self._identifier is not None
251
- else None,
239
+ ssl=self._ssl,
240
+ server_hostname=self._identifier,
252
241
  )
253
242
  LOGGER.debug("%s, %s", resp.status, await resp.text("utf-8"))
254
243
  except asyncio.TimeoutError as exception:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-homewizard-energy"
3
- version = "8.0.0-alpha-5"
3
+ version = "8.0.0-beta-1"
4
4
  description = "Asynchronous Python client for the HomeWizard Energy"
5
5
  authors = ["DCSBL"]
6
6
  maintainers = ["DCSBL"]