| # Copyright 2021 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| """Module for Level Control cluster command handling.""" |
| from typing import Any, Dict |
| |
| from gazoo_device import errors |
| |
| from local_agent import logger as logger_module |
| |
| |
| logger = logger_module.get_logger() |
| |
| |
| class LevelControlHandler: |
| """Mixin for Matter Level Control cluster handler. |
| |
| The mixin assumes self.dut and self.endpoint are set. |
| """ |
| |
| _GET_LEVEL = 'getLevel' |
| _SET_LEVEL = 'setLevel' |
| |
| SUPPORTED_METHODS = {_GET_LEVEL, _SET_LEVEL} |
| |
| def _get_level(self, params: Dict[str, Any]) -> str: |
| """Queries the CurrentLevel attribute on the device. |
| |
| Returns: |
| The current level. |
| |
| Raises: |
| DeviceError: getting CurrentLevel attribute fails. |
| """ |
| del params # not used |
| try: |
| return self.endpoint.level.current_level |
| except errors.DeviceError as exc: |
| logger.exception( |
| f'Getting CurrentLevel attribute of {self.dut.name} failed.') |
| raise exc |
| |
| def _set_level(self, params: Dict[str, Any]) -> None: |
| """Set the CurrentLevel attribute on the device. |
| |
| Raises: |
| DeviceError: setting CurrentLevel attribute fails. |
| """ |
| self.validate_key_in_params( |
| params=params, param_key='level', expected_type=int) |
| try: |
| self.endpoint.level.move_to_level(level=params['level']) |
| except errors.DeviceError as exc: |
| logger.exception(f'Turning {self.dut.name} on failed.') |
| raise exc |