python-izone 2.1.0__py3-none-any.whl → 2.1.1__py3-none-any.whl

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.
pizone/controller.py CHANGED
@@ -109,6 +109,16 @@ class Controller:
109
109
  REQUEST_TIMEOUT = 10
110
110
  """Time to wait for a response from the device, in seconds."""
111
111
 
112
+ V2_ERROR_RETRY_INTERVAL = 0.5
113
+ """Gap between V2 command retries after ``{ERROR}``, in seconds."""
114
+
115
+ V2_ERROR_RETRY_WINDOW = 8.0
116
+ """How long to keep retrying a V2 command that returns ``{ERROR}``.
117
+
118
+ Some bridges reject commands for a few seconds while applying an earlier
119
+ write.
120
+ """
121
+
112
122
  ZONE_CONFIRM_DELAY = 0.25
113
123
  ZONE_CONFIRM_INTERVAL = 2.0
114
124
  ZONE_CONFIRM_WINDOW = 30.0
@@ -195,6 +205,7 @@ class Controller:
195
205
  self._on_address_changed: Callable[[ControllerEndpoint], None] | None = None
196
206
 
197
207
  self._sending_lock = Lock()
208
+ self._v2_write_gens: dict[tuple[str, int | None], int] = {}
198
209
  self._refresh_depth: int = 0
199
210
  self._refresh_fail_ex: ConnectionError | None = None
200
211
  self._zone_mode_confirmations: dict[int, _ZoneModeConfirmation] = {}
@@ -445,8 +456,11 @@ class Controller:
445
456
  raise InvalidValueError("Free air system is not enabled")
446
457
  await self._set_system_state("FreeAir", "FreeAir", "on")
447
458
  else:
448
- if self.free_air:
449
- await self._set_system_state("FreeAir", "FreeAir", "off")
459
+ # A newer FreeAir write replaced ours; let it decide the mode.
460
+ if self.free_air and not await self._set_system_state(
461
+ "FreeAir", "FreeAir", "off"
462
+ ):
463
+ return
450
464
  await self._set_system_state("SysMode", "SystemMODE", value.value)
451
465
 
452
466
  @property
@@ -1043,9 +1057,11 @@ class Controller:
1043
1057
  command: str,
1044
1058
  value: DictValue,
1045
1059
  send: DictValue | None = None,
1046
- ) -> None:
1060
+ ) -> bool:
1047
1061
  """Send a system command and update the local cache.
1048
1062
 
1063
+ Returns False if a newer write to the same field replaced it.
1064
+
1049
1065
  Raises:
1050
1066
  ConnectionError: If the HTTP request fails.
1051
1067
  DeviceReconfigurationError: If the system was reconfigured.
@@ -1055,48 +1071,98 @@ class Controller:
1055
1071
  if send is None:
1056
1072
  send = value
1057
1073
 
1058
- async with self._sending_lock, self._refresh_scope():
1059
- if self._read_api == "v2":
1060
- v2_payload = v2_mod.system_command_to_v2(state, value, send)
1061
- if state == "FreeAir" and self._isave_v2_unsupported:
1062
- v2_payload = None
1063
- if v2_payload is not None:
1064
- try:
1065
- await self._http_command_v2(v2_payload)
1066
- except ControllerCommandError:
1067
- if state != "FreeAir":
1068
- raise
1069
- self._isave_v2_unsupported = True
1070
- await self._http_post(command, {command: send})
1071
- else:
1072
- await self._http_post(command, {command: send})
1073
- self._system_settings[state] = value
1074
- await self._fetch_system_v2_unlocked()
1075
- else:
1074
+ if self._read_api != "v2":
1075
+ async with self._sending_lock, self._refresh_scope():
1076
1076
  await self._http_post(command, {command: send})
1077
1077
  self._system_settings[state] = value
1078
1078
  await self._fetch_system()
1079
+ return True
1080
+
1081
+ async def apply() -> None:
1082
+ self._system_settings[state] = value
1083
+ await self._fetch_system_v2_unlocked()
1084
+
1085
+ v2_payload = v2_mod.system_command_to_v2(state, value, send)
1086
+ if state == "FreeAir" and self._isave_v2_unsupported:
1087
+ v2_payload = None
1088
+ if v2_payload is not None:
1089
+ try:
1090
+ return await self._command_v2_retrying(v2_payload, apply)
1091
+ except ControllerCommandError:
1092
+ if state != "FreeAir":
1093
+ raise
1094
+ self._isave_v2_unsupported = True
1095
+ async with self._sending_lock, self._refresh_scope():
1096
+ await self._http_post(command, {command: send})
1097
+ await apply()
1098
+ return True
1079
1099
 
1080
1100
  async def _execute_zone_command(
1081
1101
  self, command: str, data: dict[str, Any], zone_index: int
1082
- ) -> None:
1083
- """POST a zone command and confirm with a zone refresh."""
1102
+ ) -> bool:
1103
+ """POST a zone command and confirm with a zone refresh.
1104
+
1105
+ Returns False if a newer write to the same field replaced it.
1106
+ """
1084
1107
  self._raise_if_reconfigured()
1085
- async with self._sending_lock, self._refresh_scope():
1086
- if self._read_api == "v2":
1108
+ if self._read_api != "v2":
1109
+ group = (zone_index // 4) * 4
1110
+ async with self._sending_lock, self._refresh_scope():
1111
+ await self._http_post(command, data)
1112
+ await self._fetch_zone_group(group)
1113
+ return True
1114
+
1115
+ payload = v2_mod.zone_command_to_v2(command, data, zone_index)
1116
+
1117
+ async def apply() -> None:
1118
+ if "ZoneMode" in payload:
1119
+ mode = v2_mod.zone_mode_from_wire(payload["ZoneMode"]["Mode"])
1120
+ self._track_zone_mode(zone_index, mode.value)
1121
+ else:
1122
+ await self._fetch_zone_v2_unlocked(zone_index)
1123
+
1124
+ return await self._command_v2_retrying(payload, apply)
1125
+
1126
+ async def _command_v2_retrying(
1127
+ self, payload: dict[str, Any], apply: Callable[[], Awaitable[None]]
1128
+ ) -> bool:
1129
+ """Send *payload* and run *apply*, retrying ``{ERROR}`` without the lock.
1130
+
1131
+ A newer write to the same field replaces one waiting to retry; the
1132
+ replaced call skips *apply* and returns False.
1133
+
1134
+ Raises:
1135
+ ConnectionError: If the HTTP request fails or the controller is closed.
1136
+ ControllerCommandError: If the bridge still rejects the command once
1137
+ ``V2_ERROR_RETRY_WINDOW`` has passed.
1138
+ DeviceReconfigurationError: If the system was reconfigured.
1139
+
1140
+ """
1141
+ key = v2_mod.command_key(payload)
1142
+ gen: int | None = None
1143
+ loop = asyncio.get_running_loop()
1144
+ deadline = loop.time() + self.V2_ERROR_RETRY_WINDOW
1145
+ while True:
1146
+ async with self._sending_lock, self._refresh_scope():
1147
+ if gen is None:
1148
+ gen = self._v2_write_gens.get(key, 0) + 1
1149
+ self._v2_write_gens[key] = gen
1150
+ elif self._v2_write_gens[key] != gen:
1151
+ _LOG.debug("Dropping %s, replaced by a newer write", payload)
1152
+ return False
1153
+ self._raise_if_reconfigured()
1087
1154
  if self._closed:
1088
1155
  raise ConnectionError("Controller is closed")
1089
- payload = v2_mod.zone_command_to_v2(command, data, zone_index)
1090
- await self._http_command_v2(payload)
1091
- if "ZoneMode" in payload:
1092
- mode = v2_mod.zone_mode_from_wire(payload["ZoneMode"]["Mode"])
1093
- self._track_zone_mode(zone_index, mode.value)
1156
+ try:
1157
+ await self._http_command_v2(payload)
1158
+ except ControllerCommandError:
1159
+ if loop.time() >= deadline:
1160
+ raise
1161
+ _LOG.debug("Bridge rejected %s, retrying", payload)
1094
1162
  else:
1095
- await self._fetch_zone_v2_unlocked(zone_index)
1096
- else:
1097
- group = (zone_index // 4) * 4
1098
- await self._http_post(command, data)
1099
- await self._fetch_zone_group(group)
1163
+ await apply()
1164
+ return True
1165
+ await asyncio.sleep(self.V2_ERROR_RETRY_INTERVAL)
1100
1166
 
1101
1167
  async def _fetch_zone_v2_unlocked(self, index: int) -> None:
1102
1168
  """Refresh one zone via Type=2. Caller must hold ``_sending_lock``."""
pizone/v2.py CHANGED
@@ -449,6 +449,12 @@ def system_command_to_v2(state: str, value: Any, send: Any) -> dict[str, Any] |
449
449
  return None
450
450
 
451
451
 
452
+ def command_key(payload: dict[str, Any]) -> tuple[str, int | None]:
453
+ """Return the field an iZoneCommandV2 payload writes: (command, zone index)."""
454
+ ((name, body),) = payload.items()
455
+ return name, body.get("Index") if isinstance(body, dict) else None
456
+
457
+
452
458
  def zone_command_to_v2(
453
459
  command: str, data: dict[str, Any], zone_index: int
454
460
  ) -> dict[str, Any]:
@@ -560,7 +566,10 @@ async def command_v2(
560
566
  use_content_type: bool | None,
561
567
  timeout: float = 10.0,
562
568
  ) -> tuple[str, bool | None]:
563
- """POST iZoneCommandV2; return (raw body text, content-type mode used)."""
569
+ """POST iZoneCommandV2; return (raw body text, content-type mode used).
570
+
571
+ Sends once; the controller retries ``{ERROR}`` with its lock released.
572
+ """
564
573
  if use_content_type is not None:
565
574
  body = await _post_once(
566
575
  session,
@@ -570,16 +579,6 @@ async def command_v2(
570
579
  use_content_type=use_content_type,
571
580
  timeout=timeout,
572
581
  )
573
- if body.strip().startswith("{ERROR"):
574
- await asyncio.sleep(V2_ERROR_BACKOFF)
575
- body = await _post_once(
576
- session,
577
- host,
578
- path="iZoneCommandV2",
579
- payload=payload,
580
- use_content_type=use_content_type,
581
- timeout=timeout,
582
- )
583
582
  return body, use_content_type
584
583
 
585
584
  for mode in (False, True):
pizone/version.py CHANGED
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '2.1.0'
22
- __version_tuple__ = version_tuple = (2, 1, 0)
21
+ __version__ = version = '2.1.1'
22
+ __version_tuple__ = version_tuple = (2, 1, 1)
23
23
 
24
24
  __commit_id__ = commit_id = None
pizone/zone.py CHANGED
@@ -205,8 +205,8 @@ class Zone:
205
205
  if value < 0 or value > 100:
206
206
  raise InvalidValueError(f"MinAir '{value}' is out of range")
207
207
 
208
- await self._send_command("AirMinCommand", value)
209
- self._zone_data["MinAir"] = value
208
+ if await self._send_command("AirMinCommand", value):
209
+ self._zone_data["MinAir"] = value
210
210
 
211
211
  async def set_airflow_max(self, value: int) -> None:
212
212
  """Change the zone maximum airflow in 5% increments.
@@ -221,8 +221,8 @@ class Zone:
221
221
  if value < 0 or value > 100:
222
222
  raise InvalidValueError(f"MaxAir '{value}' is out of range")
223
223
 
224
- await self._send_command("AirMaxCommand", value)
225
- self._zone_data["MaxAir"] = value
224
+ if await self._send_command("AirMaxCommand", value):
225
+ self._zone_data["MaxAir"] = value
226
226
 
227
227
  async def set_temp_setpoint(self, value: float) -> None:
228
228
  """Change the zone temperature setpoint in degrees C.
@@ -243,9 +243,9 @@ class Zone:
243
243
  if value < self._controller.temp_min or value > self._controller.temp_max:
244
244
  raise InvalidValueError(f"SetPoint '{value}' is out of range")
245
245
 
246
- await self._send_command("ZoneCommand", value)
247
- self._zone_data["Mode"] = "auto"
248
- self._zone_data["SetPoint"] = value
246
+ if await self._send_command("ZoneCommand", value):
247
+ self._zone_data["Mode"] = "auto"
248
+ self._zone_data["SetPoint"] = value
249
249
 
250
250
  async def set_mode(self, value: Zone.Mode) -> None:
251
251
  """Set the current zone mode.
@@ -261,14 +261,14 @@ class Zone:
261
261
  # V2 can select AUTO without overwriting a newer wall-panel setpoint.
262
262
  # V1 still needs the cached setpoint to select temperature control.
263
263
  if self._controller._read_api == "v2": # noqa: SLF001
264
- await self._send_command("ZoneCommand", value.value)
264
+ sent = await self._send_command("ZoneCommand", value.value)
265
265
  else:
266
- await self._send_command(
266
+ sent = await self._send_command(
267
267
  "ZoneCommand", self._get_zone_state("SetPoint")
268
268
  )
269
- self._zone_data["Mode"] = "auto"
270
- else:
271
- await self._send_command("ZoneCommand", value.value)
269
+ if sent:
270
+ self._zone_data["Mode"] = "auto"
271
+ elif await self._send_command("ZoneCommand", value.value):
272
272
  self._zone_data["Mode"] = value.value
273
273
 
274
274
  def _update_zone(self, zone_data: Zone.ZoneData) -> None:
@@ -316,14 +316,16 @@ class Zone:
316
316
  except KeyError, ValueError:
317
317
  return None
318
318
 
319
- async def _send_command(self, command: str, data: str | float) -> None:
319
+ async def _send_command(self, command: str, data: str | float) -> bool:
320
320
  """Send a zone command via the parent controller.
321
321
 
322
+ Returns False if a newer write to the same field replaced it.
323
+
322
324
  Raises:
323
325
  ConnectionError: If the device cannot be reached or the response is invalid.
324
326
 
325
327
  """
326
328
  send_data = {command: {"ZoneNo": str(self._index + 1), "Command": str(data)}}
327
- await self._controller._execute_zone_command( # noqa: SLF001
329
+ return await self._controller._execute_zone_command( # noqa: SLF001
328
330
  command, send_data, self._index
329
331
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: python-izone
3
- Version: 2.1.0
3
+ Version: 2.1.1
4
4
  Summary: A python interface to the iZone airconditioner controller
5
5
  Project-URL: Homepage, https://github.com/Swamp-Ig/pizone
6
6
  Author-email: Penny Wood <pypl@ninjateaparty.com>
@@ -1,15 +1,15 @@
1
1
  pizone/__init__.py,sha256=Q3in9_rEc4gMUJJqOkrvcJJQoCAl2pQIcAnTB_sxQBI,3010
2
2
  pizone/const.py,sha256=0skTh6zBe61AJ_57fqdBrxz3aVBM3T4Ak_cPSkOx2Bo,153
3
- pizone/controller.py,sha256=umLIQicUYuhqm2iJ2ECj-aEefRlasyH6lv_40ogMfac,50274
3
+ pizone/controller.py,sha256=tbzYIWLgNGbdbIz9V2p01S5lY6B_VN7KJ4uddqB6G9k,52631
4
4
  pizone/discovery.py,sha256=UX5IAZTAXEk51nHsCLzFe4W80MZjlm31W8HYfszGuwo,43447
5
5
  pizone/exceptions.py,sha256=IJMsGOocv-sqBDsZP4MuOb_a3b1kRhGLig3YykncgI0,4527
6
6
  pizone/power.py,sha256=2ltoFotv3phsRJHXxP9Yu7M4PmvUVi8Q_0KJUVvuGkc,10533
7
7
  pizone/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  pizone/types.py,sha256=W7pAoBPw1cDt0V_5MbasgUiz7Iwq0zfdxpH4gHTmzsQ,2860
9
- pizone/v2.py,sha256=F-jsKfOVFAtRrjzHeGQcuG48_DJ3C-0sRKE_2urTwEw,19276
10
- pizone/version.py,sha256=v9WVy_5yL3epBJmMyIAo86Fa1LzYly1fPB2fENrVANg,520
11
- pizone/zone.py,sha256=TaUnHKZUSupP1NHy8SJglXmkv6yG42e-MUu20u00Pv0,10909
12
- python_izone-2.1.0.dist-info/METADATA,sha256=SypgaparUHrPPq_CDRy2yYgfP3ND0V0ilvPTVr9P5Ko,4282
13
- python_izone-2.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
14
- python_izone-2.1.0.dist-info/licenses/licence.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
15
- python_izone-2.1.0.dist-info/RECORD,,
9
+ pizone/v2.py,sha256=EzVCIzEAHaZcZVjvhreTR9_rNr7rrwRt_t9Bpd-pkZw,19270
10
+ pizone/version.py,sha256=rn5oInXmmhzUEn1IjVIbkXfet5tCFtndQd4xmEGVmko,520
11
+ pizone/zone.py,sha256=a-077W9SivsC3O-XYSrxWJex3pubaJIIw-AKMVSGrOA,11042
12
+ python_izone-2.1.1.dist-info/METADATA,sha256=IvSP9hlkCS_gosdxwNsNjXF0Y0jR-sXacwg4XP0wdXY,4282
13
+ python_izone-2.1.1.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
14
+ python_izone-2.1.1.dist-info/licenses/licence.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
15
+ python_izone-2.1.1.dist-info/RECORD,,