python-homewizard-energy 8.0.0a2__tar.gz → 8.0.0a4__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.0a2 → python_homewizard_energy-8.0.0a4}/PKG-INFO +1 -1
  2. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/__init__.py +2 -0
  3. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/homewizard_energy.py +11 -17
  4. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/models.py +33 -12
  5. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/v1/__init__.py +50 -68
  6. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/v2/__init__.py +19 -4
  7. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/pyproject.toml +1 -1
  8. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/LICENSE +0 -0
  9. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/README.md +0 -0
  10. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/const.py +0 -0
  11. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/errors.py +0 -0
  12. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/py.typed +0 -0
  13. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/v1/const.py +0 -0
  14. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/homewizard_energy/v2/cacert.py +0 -0
  15. {python_homewizard_energy-8.0.0a2 → python_homewizard_energy-8.0.0a4}/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.0a2
3
+ Version: 8.0.0a4
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
@@ -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",
@@ -9,15 +9,7 @@ from aiohttp.client import ClientSession
9
9
 
10
10
  from .const import LOGGER
11
11
  from .errors import UnsupportedError
12
- from .models import (
13
- CombinedModels,
14
- Device,
15
- Measurement,
16
- State,
17
- StateUpdate,
18
- System,
19
- SystemUpdate,
20
- )
12
+ from .models import CombinedModels, Device, Measurement, State, System
21
13
 
22
14
 
23
15
  class HomeWizardEnergy:
@@ -67,20 +59,18 @@ class HomeWizardEnergy:
67
59
  except (UnsupportedError, NotImplementedError):
68
60
  return None
69
61
 
70
- if self._device is None:
71
- self._device = await fetch_data(self.device())
72
-
73
- measurement, system, state = await asyncio.gather(
62
+ device, measurement, system, state = await asyncio.gather(
63
+ fetch_data(self.device()),
74
64
  fetch_data(self.measurement()),
75
65
  fetch_data(self.system()),
76
66
  fetch_data(self.state()),
77
67
  )
78
68
 
79
69
  return CombinedModels(
80
- device=self._device, measurement=measurement, system=system, state=state
70
+ device=device, measurement=measurement, system=system, state=state
81
71
  )
82
72
 
83
- async def device(self) -> Device:
73
+ async def device(self, reset_cache: bool = False) -> Device:
84
74
  """Get the device information."""
85
75
  raise NotImplementedError
86
76
 
@@ -90,14 +80,18 @@ class HomeWizardEnergy:
90
80
 
91
81
  async def system(
92
82
  self,
93
- update: SystemUpdate | None = None,
83
+ cloud_enabled: bool | None = None,
84
+ status_led_brightness_pct: int | None = None,
85
+ api_v1_enabled: bool | None = None,
94
86
  ) -> System:
95
87
  """Get/set the system."""
96
88
  raise NotImplementedError
97
89
 
98
90
  async def state(
99
91
  self,
100
- update: StateUpdate | None = None,
92
+ power_on: bool | None = None,
93
+ switch_lock: bool | None = None,
94
+ brightness: int | None = None,
101
95
  ) -> State:
102
96
  """Get/set the state."""
103
97
  raise UnsupportedError("State is not supported")
@@ -43,10 +43,35 @@ class CombinedModels:
43
43
  """All values."""
44
44
 
45
45
  device: Device
46
- measurement: Measurement | None
46
+ measurement: Measurement
47
47
  state: State | None
48
48
  system: System | None
49
49
 
50
+ def __init__(
51
+ self,
52
+ device: Device,
53
+ measurement: Measurement,
54
+ state: State | None,
55
+ system: System | None,
56
+ ):
57
+ self.device = device
58
+ self.measurement = measurement
59
+ self.state = state
60
+ self.system = system
61
+
62
+ # Move things around for backwards compatibility
63
+ ## measurement.wifi_ssid -> system.wifi_ssid
64
+ if self.measurement is not None and self.measurement.wifi_ssid is not None:
65
+ if self.system is None:
66
+ self.system = System()
67
+ self.system.wifi_ssid = self.measurement.wifi_ssid
68
+
69
+ ## state.brightness -> system.status_led_brightness_pct
70
+ if self.state is not None and self.state.brightness is not None:
71
+ if self.system is None:
72
+ self.system = System()
73
+ self.system.status_led_brightness_pct = (self.state.brightness / 255) * 100
74
+
50
75
 
51
76
  def get_verification_hostname(model: str, serial_number: str) -> str:
52
77
  """Helper method to convert device model and serial to identifier
@@ -86,7 +111,7 @@ class Device(BaseModel):
86
111
  """Return if the device supports state."""
87
112
  return self.product_type == Model.ENERGY_SOCKET
88
113
 
89
- def support_identify(self) -> bool:
114
+ def supports_identify(self) -> bool:
90
115
  """Return if the device supports identify."""
91
116
  return self.product_type not in (
92
117
  Model.ENERGY_METER_1_PHASE,
@@ -320,7 +345,7 @@ class Measurement(BaseModel):
320
345
  return rv
321
346
 
322
347
  @staticmethod
323
- def to_datetime(timestamp: str | int | None) -> datetime | None:
348
+ def to_datetime(timestamp: str | int) -> datetime:
324
349
  """Convert DSRM gas-timestamp to datetime object.
325
350
 
326
351
  Args:
@@ -329,10 +354,6 @@ class Measurement(BaseModel):
329
354
  Returns:
330
355
  A datetime object.
331
356
  """
332
-
333
- if timestamp is None:
334
- return None
335
-
336
357
  if isinstance(timestamp, int):
337
358
  # V1 API uses int for timestamp
338
359
  return datetime.strptime(str(timestamp), "%y%m%d%H%M%S")
@@ -424,7 +445,7 @@ class Measurement(BaseModel):
424
445
  return d
425
446
 
426
447
 
427
- @dataclass
448
+ @dataclass(kw_only=True)
428
449
  class ExternalDevice(BaseModel):
429
450
  """Represents externally connected device."""
430
451
 
@@ -438,7 +459,7 @@ class ExternalDevice(BaseModel):
438
459
  INLET_HEAT_METER = "inlet_heat_meter"
439
460
 
440
461
  unique_id: datetime = field(
441
- default=None, metadata={"deserialize": lambda x: Measurement.hex_to_readable(x)}
462
+ metadata={"deserialize": lambda x: Measurement.hex_to_readable(x)}
442
463
  )
443
464
 
444
465
  type: DeviceType | None = field(
@@ -449,10 +470,10 @@ class ExternalDevice(BaseModel):
449
470
  else None
450
471
  },
451
472
  )
452
- value: float = field(default=0.0)
453
- unit: str = field(default="")
473
+ value: float = field()
474
+ unit: str = field()
454
475
  timestamp: datetime = field(
455
- default=None, metadata={"deserialize": lambda x: Measurement.to_datetime(x)}
476
+ metadata={"deserialize": lambda x: Measurement.to_datetime(x)}
456
477
  )
457
478
 
458
479
 
@@ -15,15 +15,7 @@ from aiohttp.hdrs import METH_GET, METH_PUT
15
15
  from ..const import LOGGER
16
16
  from ..errors import DisabledError, NotFoundError, RequestError, UnsupportedError
17
17
  from ..homewizard_energy import HomeWizardEnergy
18
- from ..models import (
19
- CombinedModels,
20
- Device,
21
- Measurement,
22
- State,
23
- StateUpdate,
24
- System,
25
- SystemUpdate,
26
- )
18
+ from ..models import Device, Measurement, State, StateUpdate, System, SystemUpdate
27
19
 
28
20
  T = TypeVar("T")
29
21
 
@@ -46,33 +38,17 @@ def optional_method(
46
38
  class HomeWizardEnergyV1(HomeWizardEnergy):
47
39
  """Communicate with a HomeWizard Energy device."""
48
40
 
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
41
+ async def device(self, reset_cache: bool = False) -> Device:
42
+ """Return the device object."""
68
43
 
69
- return models
44
+ if self._device is not None and not reset_cache:
45
+ return self._device
70
46
 
71
- async def device(self) -> Device:
72
- """Return the device object."""
73
47
  _, response = await self._request("api")
74
48
  device = Device.from_json(response)
75
49
 
50
+ # Cache device object
51
+ self._device = device
76
52
  return device
77
53
 
78
54
  async def measurement(self) -> Measurement:
@@ -81,52 +57,61 @@ class HomeWizardEnergyV1(HomeWizardEnergy):
81
57
  return Measurement.from_json(response)
82
58
 
83
59
  @optional_method
84
- async def state(self, update: StateUpdate | None = None) -> State:
85
- """Return or update the state object."""
60
+ async def system(
61
+ self,
62
+ cloud_enabled: bool | None = None,
63
+ status_led_brightness_pct: int | None = None,
64
+ api_v1_enabled: bool | None = None,
65
+ ) -> System:
66
+ """Return the system object."""
86
67
 
87
- if self._device is not None and self._device.supports_state is False:
88
- raise UnsupportedError("State is not supported")
68
+ # Legacy: raise on unsupported field
69
+ if api_v1_enabled is not None:
70
+ raise UnsupportedError("Setting api_v1_enabled is not supported in v1")
89
71
 
90
- if update is not None:
91
- data = update.to_dict()
92
- status, response = await self._request(
93
- "api/v1/state", method=METH_PUT, data=data
72
+ # Legacy: route 'status_led_brightness_pct' to state
73
+ if status_led_brightness_pct is not None:
74
+ state = await self.state(brightness=status_led_brightness_pct * 2.55)
75
+ return System(status_led_brightness_pct=state.brightness / 2.55)
76
+
77
+ if cloud_enabled is not None:
78
+ # Executing the update
79
+ data = SystemUpdate(cloud_enabled=cloud_enabled).to_dict()
80
+ _, response = await self._request(
81
+ "api/v1/system", method=METH_PUT, data=data
94
82
  )
95
83
 
96
84
  else:
97
- status, response = await self._request("api/v1/state")
85
+ _, response = await self._request("api/v1/system")
98
86
 
99
- if status != HTTPStatus.OK:
100
- raise RequestError("Failed to get/set state")
101
-
102
- state = State.from_json(response)
103
- return state
87
+ system = System.from_json(response)
88
+ return system
104
89
 
105
90
  @optional_method
106
- async def system(self, update: SystemUpdate | None = None) -> System:
107
- """Return the system object."""
108
- if update is not None:
109
- if (
110
- update.status_led_brightness_pct is not None
111
- or update.api_v1_enabled is not None
112
- ):
113
- raise UnsupportedError(
114
- "Setting status_led_brightness_pct and api_v1_enabled is not supported in v1"
115
- )
91
+ async def state(
92
+ self,
93
+ power_on: bool | None = None,
94
+ switch_lock: bool | None = None,
95
+ brightness: int | None = None,
96
+ ) -> State:
97
+ """Return or update the state object."""
116
98
 
117
- data = update.to_dict()
118
- status, response = await self._request(
119
- "api/v1/system", method=METH_PUT, data=data
99
+ if self._device is not None and self._device.supports_state() is False:
100
+ raise UnsupportedError("State is not supported")
101
+
102
+ if power_on is not None or switch_lock is not None or brightness is not None:
103
+ data = StateUpdate(
104
+ power_on=power_on, switch_lock=switch_lock, brightness=brightness
105
+ ).to_dict()
106
+ _, response = await self._request(
107
+ "api/v1/state", method=METH_PUT, data=data
120
108
  )
121
109
 
122
110
  else:
123
- status, response = await self._request("api/v1/system")
111
+ _, response = await self._request("api/v1/state")
124
112
 
125
- if status != HTTPStatus.OK:
126
- raise RequestError("Failed to get/set system")
127
-
128
- system = System.from_json(response)
129
- return system
113
+ state = State.from_json(response)
114
+ return state
130
115
 
131
116
  @optional_method
132
117
  async def identify(
@@ -134,7 +119,7 @@ class HomeWizardEnergyV1(HomeWizardEnergy):
134
119
  ) -> bool:
135
120
  """Send identify request."""
136
121
 
137
- if self._device is not None and self._device.supports_state is False:
122
+ if self._device is not None and self._device.supports_identify() is False:
138
123
  raise UnsupportedError("State is not supported")
139
124
 
140
125
  await self._request("api/v1/identify", method=METH_PUT)
@@ -179,9 +164,6 @@ class HomeWizardEnergyV1(HomeWizardEnergy):
179
164
  raise DisabledError(
180
165
  "API disabled. API must be enabled in HomeWizard Energy app"
181
166
  )
182
- case HTTPStatus.NO_CONTENT:
183
- # No content, just return
184
- return (HTTPStatus.NO_CONTENT, None)
185
167
  case HTTPStatus.NOT_FOUND:
186
168
  raise NotFoundError("Resource not found")
187
169
  case HTTPStatus.OK:
@@ -72,11 +72,16 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
72
72
  self._token = token
73
73
 
74
74
  @authorized_method
75
- async def device(self) -> Device:
75
+ async def device(self, reset_cache: bool = False) -> Device:
76
76
  """Return the device object."""
77
+ if self._device is not None and not reset_cache:
78
+ return self._device
79
+
77
80
  _, response = await self._request("/api")
78
81
  device = Device.from_json(response)
79
82
 
83
+ # Cache device object
84
+ self._device = device
80
85
  return device
81
86
 
82
87
  @authorized_method
@@ -90,12 +95,22 @@ class HomeWizardEnergyV2(HomeWizardEnergy):
90
95
  @authorized_method
91
96
  async def system(
92
97
  self,
93
- update: SystemUpdate | None = None,
98
+ cloud_enabled: bool | None = None,
99
+ status_led_brightness_pct: int | None = None,
100
+ api_v1_enabled: bool | None = None,
94
101
  ) -> System:
95
102
  """Return the system object."""
96
103
 
97
- if update is not None:
98
- data = update.to_dict()
104
+ if (
105
+ cloud_enabled is not None
106
+ or status_led_brightness_pct is not None
107
+ or api_v1_enabled is not None
108
+ ):
109
+ data = SystemUpdate(
110
+ cloud_enabled=cloud_enabled,
111
+ status_led_brightness_pct=status_led_brightness_pct,
112
+ api_v1_enabled=api_v1_enabled,
113
+ ).to_dict()
99
114
  status, response = await self._request(
100
115
  "/api/system", method=METH_PUT, data=data
101
116
  )
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-homewizard-energy"
3
- version = "8.0.0-alpha-2"
3
+ version = "8.0.0-alpha-4"
4
4
  description = "Asynchronous Python client for the HomeWizard Energy"
5
5
  authors = ["DCSBL"]
6
6
  maintainers = ["DCSBL"]