Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / src / app / zap-templates / templates / chip / python-chip-ChipCluster.zapt
1 '''
2 {{> header}}
3 '''
4
5 import ctypes
6 from .ChipStack import *
7 from .exceptions import *
8
9 '''
10 TODO(#4511): This file only sends cluster commands, should add more functions.
11 '''
12
13 __all__ = ["ChipCluster"]
14
15 class ChipCluster:
16     def __init__(self, chipstack):
17         self._ChipStack = chipstack
18
19     def ListClusters(self):
20         return {
21 {{#chip_server_clusters}}
22             "{{asCamelCased name false}}": {
23 {{#chip_server_cluster_commands}}
24                 "{{asCamelCased name false}}": {
25 {{#chip_server_cluster_command_arguments}}
26                     "{{asCamelCased label}}": "{{asPythonType chipType}}",
27 {{/chip_server_cluster_command_arguments}}
28                 },
29 {{/chip_server_cluster_commands}}
30             },
31 {{/chip_server_clusters}}
32         }
33
34     def SendCommand(self, device: ctypes.c_void_p, cluster: str, command: str, endpoint: int, groupid: int, args):
35         func = getattr(self, "Cluster{}_Command{}".format(cluster, command), None)
36         if not func:
37             raise UnknownCommand(cluster, command)
38         func(device, endpoint, groupid, **args)
39
40 {{#chip_server_clusters}}
41 {{#chip_server_cluster_commands}}
42     def Cluster{{asCamelCased clusterName false}}_Command{{asCamelCased name false}}(self, device: ctypes.c_void_p, ZCLendpoint: int, ZCLgroupid: int{{#chip_server_cluster_command_arguments}}, {{asCamelCased label}}: {{asPythonType chipType}}{{/chip_server_cluster_command_arguments}}):
43 {{#chip_server_cluster_command_arguments}}
44 {{#if (isCharString type)}}
45         {{asCamelCased label}} = {{asCamelCased label}}.encode("utf-8") + b'\x00'
46 {{/if}}
47 {{/chip_server_cluster_command_arguments}}
48         self._ChipStack.Call(
49             lambda: self._chipLib.chip_ime_AppendCommand_{{asCamelCased clusterName false}}_{{asCamelCased name false}}(
50                 device, ZCLendpoint, ZCLgroupid{{#chip_server_cluster_command_arguments}}, {{asCamelCased label}}{{#if (isString type)}}, len({{asCamelCased label}}){{/if}}{{/chip_server_cluster_command_arguments}}
51             )
52         )
53
54 {{/chip_server_cluster_commands}}
55 {{/chip_server_clusters}}
56     def InitLib(self, chipLib):
57         self._chipLib = chipLib
58 {{#chip_server_clusters}}
59         # Cluster {{asCamelCased name false}}
60 {{#chip_server_cluster_commands}}
61         # Cluster {{asCamelCased clusterName false}} Command {{asCamelCased name false}}
62         self._chipLib.chip_ime_AppendCommand_{{asCamelCased clusterName false}}_{{asCamelCased name false}}.argtypes = [ctypes.c_void_p, ctypes.c_uint8, ctypes.c_uint16{{#chip_server_cluster_command_arguments}}{{#if (isString type)}}, ctypes.c_char_p, ctypes.c_uint32{{else}}, ctypes.{{asPythonCType chipType}}{{/if}}{{/chip_server_cluster_command_arguments}}]
63         self._chipLib.chip_ime_AppendCommand_{{asCamelCased clusterName false}}_{{asCamelCased name false}}.restype = ctypes.c_uint32
64 {{/chip_server_cluster_commands}}
65 {{/chip_server_clusters}}