-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
andy31415
merged 1 commit into
project-chip:master
from
yunhanw-google:feature/add_python_event_codegen
Nov 17, 2021
Merged
Add IM event python codegen #11802
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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,)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?