Skip to content

Commit

Permalink
Simplify UpdateCachedData for better readability
Browse files Browse the repository at this point in the history
Extract cache type implementation in functions. Also make the code a bit
more Pythonic.

Note: This no longer initializes endpointCache in the Cluster-View. This
shouldn't matter in practice as the dictionary wasn't used.
  • Loading branch information
agners committed Dec 18, 2023
1 parent 4cffa70 commit feb9623
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions src/controller/python/chip/clusters/Attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,30 @@ def UpdateCachedData(self, changedPathSet: set[AttributePath]):
instead of a cluster object value, a ValueDecodeFailure shall be present.
'''

tlvCache = self.attributeTLVCache
attributeCache = self.attributeCache
def handle_cluster_view(endpointId, clusterId, clusterType):
try:
decodedData = clusterType.FromDict(
data=clusterType.descriptor.TagDictToLabelDict([], self.attributeTLVCache[endpointId][clusterId]))
decodedData.SetDataVersion(self.versionList.get(endpointId, {}).get(clusterId))
return decodedData
except Exception as ex:
return ValueDecodeFailure(self.attributeTLVCache[endpointId][clusterId], ex)

def handle_attribute_view(endpointId, clusterId, attributeId, attributeType):
value = self.attributeTLVCache[endpointId][clusterId][attributeId]
if isinstance(value, ValueDecodeFailure):
return value
try:
return attributeType.FromTagDictOrRawValue(value)
except Exception as ex:
return ValueDecodeFailure(value, ex)

for attributePath in changedPathSet:
endpointId = attributePath.EndpointId
endpointId, clusterId, attributeId = attributePath.EndpointId, attributePath.ClusterId, attributePath.AttributeId

if endpointId not in attributeCache:
attributeCache[endpointId] = {}

endpointCache = attributeCache[endpointId]

clusterId = attributePath.ClusterId
if endpointId not in self.attributeCache:
self.attributeCache[endpointId] = {}
endpointCache = self.attributeCache[endpointId]

if clusterId not in _ClusterIndex:
#
Expand All @@ -430,30 +442,13 @@ def UpdateCachedData(self, changedPathSet: set[AttributePath]):

clusterType = _ClusterIndex[clusterId]

if clusterType not in endpointCache:
endpointCache[clusterType] = {}

clusterCache = endpointCache[clusterType]
clusterDataVersion = self.versionList.get(
endpointId, {}).get(clusterId, None)

if self.returnClusterObject:
try:
# Since the TLV data is already organized by attribute tags, we can trivially convert to a cluster object representation.
endpointCache[clusterType] = clusterType.FromDict(
data=clusterType.descriptor.TagDictToLabelDict([], tlvCache[endpointId][clusterId]))
endpointCache[clusterType].SetDataVersion(
clusterDataVersion)
except Exception as ex:
decodedValue = ValueDecodeFailure(
tlvCache[endpointId][clusterId], ex)
endpointCache[clusterType] = decodedValue
endpointCache[clusterType] = handle_cluster_view(endpointId, clusterId, clusterType)
else:
clusterCache[DataVersion] = clusterDataVersion

attributeId = attributePath.AttributeId

value = tlvCache[endpointId][clusterId][attributeId]
if clusterType not in endpointCache:
endpointCache[clusterType] = {}
clusterCache = endpointCache[clusterType]
clusterCache[DataVersion] = self.versionList.get(endpointId, {}).get(clusterId)

if (clusterId, attributeId) not in _AttributeIndex:
#
Expand All @@ -463,20 +458,7 @@ def UpdateCachedData(self, changedPathSet: set[AttributePath]):
continue

attributeType = _AttributeIndex[(clusterId, attributeId)][0]

if attributeType not in clusterCache:
clusterCache[attributeType] = {}

if isinstance(value, ValueDecodeFailure):
clusterCache[attributeType] = value
else:
try:
decodedValue = attributeType.FromTagDictOrRawValue(
tlvCache[endpointId][clusterId][attributeId])
except Exception as ex:
decodedValue = ValueDecodeFailure(value, ex)

clusterCache[attributeType] = decodedValue
clusterCache[attributeType] = handle_attribute_view(endpointId, clusterId, attributeId, attributeType)


class SubscriptionTransaction:
Expand Down

0 comments on commit feb9623

Please sign in to comment.