Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IM event python codegen #11802

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ enum class Fields {
struct Type
{
public:
static constexpr PriorityLevel priorityLevel = PriorityLevel::{{asUpperCamelCase priority}};
static constexpr EventId eventId = {{asMEI manufacturerCode code}};
static constexpr PriorityLevel GetPriorityLevel() { return kPriorityLevel; }
static constexpr EventId GetEventId() { return kEventId; }
static constexpr ClusterId GetClusterId() { return Clusters::{{asUpperCamelCase parent.name}}::Id; }

{{#zcl_event_fields}}
Expand Down
56 changes: 56 additions & 0 deletions src/controller/python/chip/clusters/ClusterObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,59 @@ def _cluster_object(cls) -> ClusterObject:
)
],
bases=(ClusterObject,))


class ClusterEventDescriptor:
'''
The ClusterEventDescriptor is used for holding an event's metadata like its cluster id, event id and its type.

Users should not initialize an object based on this class. Instead, users should pass the subclass objects to tell some methods what they want.

The implementation of this functions is quite tricky, it will create a cluster object on-the-fly, and use it for actual encode / decode routine to save lines of code.
'''
@classmethod
def ToTLV(cls, tag: Union[int, None], value):
writer = tlv.TLVWriter()
wrapped_value = cls._cluster_object(Value=value)
cls.event_type.PutFieldToTLV(tag,
asdict(wrapped_value)['Value'], writer, '')
return writer.encoding

@classmethod
def FromTLV(cls, tlvBuffer: bytes):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need it's own FromTLV methods? Why cant it just the versions int he base class?

obj_class = cls._cluster_object
return obj_class.FromDict(obj_class.descriptor.TagDictToLabelDict('', {0: tlv.TLVReader(tlvBuffer).get().get('Any', {})})).Value

@classmethod
def FromTagDictOrRawValue(cls, val: Any):
obj_class = cls._cluster_object
return obj_class.FromDict(obj_class.descriptor.TagDictToLabelDict('', {0: val})).Value

@ChipUtility.classproperty
def cluster_id(self) -> int:
raise NotImplementedError()

@ChipUtility.classproperty
def event_id(self) -> int:
raise NotImplementedError()

@ChipUtility.classproperty
def event_type(cls) -> ClusterObjectFieldDescriptor:
raise NotImplementedError()

@ChipUtility.classproperty
def _cluster_object(cls) -> ClusterObject:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? It was needed for attributes since each attribute was of type ClusterObjectFieldDescriptor, but in this case, you have a perfectly valid ClusterObjectDescriptor available here...

return make_dataclass('InternalClass',
[
('Value', List[cls.event_type.Type]
if cls.event_type.IsArray else cls.event_type.Type, field(default=None)),
('descriptor', ClassVar[ClusterObjectDescriptor],
field(
default=ClusterObjectDescriptor(
Fields=[ClusterObjectFieldDescriptor(
Label='Value', Tag=0, Type=cls.event_type.Type, IsArray=cls.event_type.IsArray)]
)
)
)
],
bases=(ClusterObject,))
Loading