aiocomfoconnect 0.4.5__tar.gz → 0.4.7__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aiocomfoconnect
3
- Version: 0.4.5
3
+ Version: 0.4.7
4
4
  Summary: aiocomfoconnect is an asyncio Python 3 library for communicating with a Zehnder ComfoAir Q350/450/600 ventilation system
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -59,6 +59,7 @@ from aiocomfoconnect.properties import (
59
59
  Property,
60
60
  PropertyConstraints,
61
61
  )
62
+ from aiocomfoconnect.protobuf import zehnder_pb2
62
63
  from aiocomfoconnect.sensors import Sensor
63
64
  from aiocomfoconnect.util import bytearray_to_bits, encode_pdo_value, version_decode
64
65
 
@@ -177,6 +178,8 @@ class ComfoConnect(Bridge):
177
178
  self._sensors: Dict[int, Sensor] = {}
178
179
  self._sensors_values: Dict[int, Any] = {}
179
180
  self._sensor_holds: Dict[int, asyncio.Handle] = {}
181
+ # The subscriptions that register_sensor is waiting for, done when the bridge answered.
182
+ self._sensor_subscriptions: Dict[int, asyncio.Future] = {}
180
183
 
181
184
  self._reconnect_task: Optional[asyncio.Task] = None
182
185
  self._connect_lock = asyncio.Lock()
@@ -259,6 +262,9 @@ class ComfoConnect(Bridge):
259
262
  for sensor in list(self._sensors.values()):
260
263
  try:
261
264
  await self.cmd_rpdo_request(sensor.id, sensor.type)
265
+ except ComfoConnectNotAllowed:
266
+ # A resumed session can still have the sensor, see register_sensor.
267
+ _LOGGER.debug("The bridge refused the subscription to sensor %s (%s), keeping it", sensor.id, sensor.name)
262
268
  except (ComfoConnectError, AioComfoConnectTimeout) as exc:
263
269
  _LOGGER.warning("Failed to subscribe to sensor %s (%s): %r", sensor.id, sensor.name, exc)
264
270
 
@@ -364,10 +370,16 @@ class ComfoConnect(Bridge):
364
370
  The bridge can drop our session without closing the connection, e.g. when another app takes over a session with our
365
371
  uuid. It then answers every request with NOT_ALLOWED (or OTHER_SESSION). Closing the connection lets the reconnect
366
372
  loop start a new session. The request still raises its error.
373
+
374
+ A NOT_ALLOWED on a CnRpdoRequest doesn't mean that: some bridges refuse only that subscription when the sensor is
375
+ already subscribed on the session, and keep the session. When the session is gone, the heartbeat and the next
376
+ other request get NOT_ALLOWED too.
367
377
  """
368
378
  try:
369
379
  return await super()._send(request, request_type, params, reply, timeout)
370
- except (ComfoConnectNotAllowed, ComfoConnectOtherSession):
380
+ except (ComfoConnectNotAllowed, ComfoConnectOtherSession) as exc:
381
+ if isinstance(exc, ComfoConnectNotAllowed) and request_type == zehnder_pb2.GatewayOperation.CnRpdoRequestType: # pylint: disable=no-member
382
+ raise
371
383
  if self._connection_state == ConnectionState.CONNECTED and self._writer is not None:
372
384
  _LOGGER.warning("The bridge dropped our session, reconnecting")
373
385
  self._writer.close()
@@ -392,8 +404,24 @@ class ComfoConnect(Bridge):
392
404
  self._set_connection_state(ConnectionState.DISCONNECTED)
393
405
 
394
406
  async def register_sensor(self, sensor: Sensor):
395
- """Register a sensor on the bridge."""
396
- previous = self._sensors.get(sensor.id)
407
+ """Register a sensor on the bridge.
408
+
409
+ A sensor that is already registered isn't subscribed again, since some bridges refuse a second subscription to the
410
+ same sensor. Its latest value goes to the sensor callback right away instead, so every caller gets a value. While
411
+ the first subscription is still waiting for the bridge, we wait for it, and subscribe ourselves when it failed.
412
+ """
413
+ while sensor.id in self._sensor_subscriptions:
414
+ # Shielded, so cancelling us doesn't cancel the wait of the other registration.
415
+ await asyncio.shield(self._sensor_subscriptions[sensor.id])
416
+
417
+ if sensor.id in self._sensors:
418
+ self._sensors[sensor.id] = sensor
419
+ value = self._sensors_values.get(sensor.id)
420
+ # A held sensor emits its value when the hold ends.
421
+ if value is not None and sensor.id not in self._sensor_holds and self._sensor_callback_fn is not None:
422
+ self._sensor_callback_fn(sensor, _sensor_value(sensor, value))
423
+ return
424
+
397
425
  self._sensors[sensor.id] = sensor
398
426
  self._sensors_values[sensor.id] = None
399
427
 
@@ -401,19 +429,24 @@ class ComfoConnect(Bridge):
401
429
  # they need their own hold. The hold that was started when we connected has probably expired already.
402
430
  self._hold_sensor(sensor.id)
403
431
 
432
+ subscription = self._sensor_subscriptions[sensor.id] = asyncio.get_running_loop().create_future()
404
433
  try:
405
434
  await self.cmd_rpdo_request(sensor.id, sensor.type)
435
+ except ComfoConnectNotAllowed:
436
+ # The session already has this sensor, e.g. a resumed session that kept the subscriptions of the previous one,
437
+ # and the bridge keeps sending its values. When the session is gone instead, the next session subscribes again.
438
+ _LOGGER.debug("The bridge refused the subscription to sensor %s (%s), keeping it", sensor.id, sensor.name)
406
439
  except BaseException:
407
440
  # Only keep the sensor when the bridge accepted the subscription, so we don't retry it on every reconnect.
408
441
  handle = self._sensor_holds.pop(sensor.id, None)
409
442
  if handle is not None:
410
443
  handle.cancel()
411
- if previous is None:
412
- self._sensors.pop(sensor.id, None)
413
- self._sensors_values.pop(sensor.id, None)
414
- else:
415
- self._sensors[sensor.id] = previous
444
+ self._sensors.pop(sensor.id, None)
445
+ self._sensors_values.pop(sensor.id, None)
416
446
  raise
447
+ finally:
448
+ del self._sensor_subscriptions[sensor.id]
449
+ subscription.set_result(None)
417
450
 
418
451
  async def deregister_sensor(self, sensor: Sensor):
419
452
  """Deregister a sensor on the bridge."""
@@ -537,10 +570,13 @@ class ComfoConnect(Bridge):
537
570
  async def set_property(self, prop: Property, value: int | float | str, node_id: Optional[int] = None) -> Any:
538
571
  """Set a property on the node it lives on (or node_id), encoded for its type.
539
572
 
540
- A property with options takes an option, a scaled property takes the scaled value.
573
+ A property with options takes an option, a scaled property takes the scaled value. A numeric value is rounded to
574
+ the whole number the unit stores, so a float like 90.0 is fine for a property without a scale too.
541
575
  """
542
576
  if prop.scale is not None:
543
577
  value = round(value / prop.scale)
578
+ elif prop.options is None and prop.property_type in NUMERIC_PROPERTY_TYPES:
579
+ value = round(value)
544
580
  if prop.options is not None:
545
581
  values = {option: raw for raw, option in prop.options.items()}
546
582
  if value not in values:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "aiocomfoconnect"
3
- version = "0.4.5"
3
+ version = "0.4.7"
4
4
  description = "aiocomfoconnect is an asyncio Python 3 library for communicating with a Zehnder ComfoAir Q350/450/600 ventilation system"
5
5
  authors = ["Michaël Arnauts <michael.arnauts@gmail.com>"]
6
6
  license = "MIT"
File without changes