Skip to content

Commit b02ee8e

Browse files
committed
Add raw attribute callback
Add new subscription callback which uses raw AttributePath as paths of changed attributes. This allows to subscribe to custom clusters, where no Cluster/Attribute types are part of the Python library. Also allow to get the raw Python values (in tagged dict format) directly from the subscription transaction.
1 parent f378cb6 commit b02ee8e

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/controller/python/chip/clusters/Attribute.py

+38-10
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ def __init__(self, transaction: AsyncReadTransaction, subscriptionId, devCtrl):
429429
self._onResubscriptionAttemptedCb: Callable[[SubscriptionTransaction,
430430
int, int], None] = DefaultResubscriptionAttemptedCallback
431431
self._onAttributeChangeCb: Callable[[TypedAttributePath, SubscriptionTransaction], None] = DefaultAttributeChangeCallback
432+
self._onRawAttributeChangeCb: Optional[Callable[[AttributePath, SubscriptionTransaction]]] = None
432433
self._onEventChangeCb: Callable[[EventReadResult, SubscriptionTransaction], None] = DefaultEventChangeCallback
433434
self._onErrorCb: Callable[[int, SubscriptionTransaction], None] = DefaultErrorCallback
434435
self._readTransaction = transaction
@@ -454,6 +455,18 @@ def GetAttribute(self, path: TypedAttributePath) -> Any:
454455
else:
455456
return data[path.Path.EndpointId][path.ClusterType][path.AttributeType]
456457

458+
def GetTLVAttributes(self) -> Dict[int, Dict[int, Dict[int, Any]]]:
459+
'''Returns the attributes value cache in raw/tag dict value tracking
460+
the latest state on the publisher.
461+
'''
462+
return self._readTransaction._cache.attributeTLVCache
463+
464+
465+
def GetTLVAttribute(self, path: AttributePath) -> bytes:
466+
'''Returns a specific attribute given a AttributePath.
467+
'''
468+
return self._readTransaction._cache.attributeTLVCache[path.EndpointId][path.ClusterId][path.AttributeId]
469+
457470
def GetEvents(self):
458471
return self._readTransaction.GetAllEventValues()
459472

@@ -534,8 +547,14 @@ def SetAttributeUpdateCallback(self, callback: Callable[[TypedAttributePath, Sub
534547
Sets the callback function for the attribute value change event,
535548
accepts a Callable accepts an attribute path and the cached data.
536549
'''
537-
if callback is not None:
538-
self._onAttributeChangeCb = callback
550+
self._onAttributeChangeCb = callback
551+
552+
def SetRawAttributeUpdateCallback(self, callback: Callable[[AttributePath, SubscriptionTransaction], None]):
553+
'''
554+
Sets the callback function for raw attribute value change event,
555+
accepts a Callable which accepts an attribute path and the cached data.
556+
'''
557+
self._onRawAttributeChangeCb = callback
539558

540559
def SetEventUpdateCallback(self, callback: Callable[[EventReadResult, SubscriptionTransaction], None]):
541560
if callback is not None:
@@ -553,6 +572,10 @@ def SetErrorCallback(self, callback: Callable[[int, SubscriptionTransaction], No
553572
def OnAttributeChangeCb(self) -> Callable[[TypedAttributePath, SubscriptionTransaction], None]:
554573
return self._onAttributeChangeCb
555574

575+
@property
576+
def OnRawAttributeChangeCb(self) -> Callable[[TypedAttributePath, SubscriptionTransaction], None]:
577+
return self._onRawAttributeChangeCb
578+
556579
@property
557580
def OnEventChangeCb(self) -> Callable[[EventReadResult, SubscriptionTransaction], None]:
558581
return self._onEventChangeCb
@@ -767,14 +790,19 @@ def _handleReportBegin(self):
767790
def _handleReportEnd(self):
768791
if (self._subscription_handler is not None):
769792
for change in self._changedPathSet:
770-
try:
771-
attribute_path = TypedAttributePath(Path=change)
772-
except (KeyError, ValueError) as err:
773-
# path could not be resolved into a TypedAttributePath
774-
LOGGER.exception(err)
775-
continue
776-
self._subscription_handler.OnAttributeChangeCb(
777-
attribute_path, self._subscription_handler)
793+
if self._subscription_handler.OnAttributeChangeCb:
794+
try:
795+
attribute_path = TypedAttributePath(Path=change)
796+
except (KeyError, ValueError) as err:
797+
# path could not be resolved into a TypedAttributePath
798+
LOGGER.exception(err)
799+
continue
800+
self._subscription_handler.OnAttributeChangeCb(
801+
attribute_path, self._subscription_handler)
802+
803+
if self._subscription_handler.OnRawAttributeChangeCb:
804+
self._subscription_handler.OnRawAttributeChangeCb(
805+
change, self._subscription_handler)
778806

779807
# Clear it out once we've notified of all changes in this transaction.
780808
self._changedPathSet = set()

0 commit comments

Comments
 (0)