example_cluster.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #
  2. # Copyright (c) 2023 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the 'License');
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an 'AS IS' BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from matter_yamltests.pseudo_clusters.pseudo_cluster import PseudoCluster
  16. class example_cluster(PseudoCluster):
  17. name = 'ExampleCluster'
  18. async def CommandSuccess(self, request):
  19. return {}
  20. async def CommandError(self, request):
  21. return {'error': 'UNSUPPORTED_COMMAND'}
  22. async def CommandWithReturnValues(self, request):
  23. rv = {
  24. 'BooleanTrue': True,
  25. 'BooleanFalse': False,
  26. 'PositiveNumber': 123456789,
  27. 'NegativeNumber': -123456789,
  28. 'ListOfBoolean': [True, False, True],
  29. 'Struct': {
  30. 'boolean': True,
  31. 'number': 1,
  32. 'struct': {
  33. 'boolean': False,
  34. 'number': 2,
  35. },
  36. 'list': [
  37. {
  38. 'boolean': True,
  39. 'number': 1,
  40. },
  41. {
  42. 'boolean': False,
  43. 'number': 3,
  44. },
  45. ]
  46. }
  47. }
  48. return {'value': rv}
  49. async def CommandWithInputValues(self, request):
  50. try:
  51. arg1 = int(self._get_argument_value(request, 'Argument1'))
  52. arg2 = int(self._get_argument_value(request, 'Argument2'))
  53. except NameError:
  54. return {'error': 'INVALID_COMMAND'}
  55. rv = {'ReturnValue': arg1 + arg2}
  56. return {'value': rv}
  57. def _get_argument_value(self, request, name):
  58. for argument in request.arguments['values']:
  59. if argument['name'] == name:
  60. return argument['value']
  61. raise NameError