| # 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 OnOff 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() |
| |
| # OnOff Response States: |
| LIGHT_ON = 'on' |
| LIGHT_OFF = 'off' |
| |
| |
| class OnOffHandler: |
| """Mixin for Matter OnOff cluster handler. |
| |
| The mixin assumes self.dut and self.endpoint are set. |
| """ |
| |
| _GET_ONOFF = 'getOnOff' |
| _SET_ON = 'setOn' |
| _SET_OFF = 'setOff' |
| |
| SUPPORTED_METHODS = {_GET_ONOFF, _SET_ON, _SET_OFF} |
| |
| def _get_on_off(self, params: Dict[str, Any]) -> str: |
| """Queries the OnOff attribute of the device. |
| |
| Returns: |
| 'on' if the device is in on state, 'off' if it's in off state. |
| |
| Raises: |
| DeviceError: getting light state fails. |
| """ |
| del params # not used |
| try: |
| return LIGHT_ON if self.endpoint.on_off.onoff else LIGHT_OFF |
| except errors.DeviceError as exc: |
| logger.exception(f'Getting OnOff attribute of {self.dut.name} failed.') |
| raise exc |
| |
| def _set_on(self, params: Dict[str, Any]) -> None: |
| """Turns on the device. |
| |
| Raises: |
| DeviceError: turning light on fails. |
| """ |
| del params # not used |
| try: |
| self.endpoint.on_off.on() |
| except errors.DeviceError as exc: |
| logger.exception(f'Turning {self.dut.name} on failed.') |
| raise exc |
| |
| def _set_off(self, params: Dict[str, Any]) -> None: |
| """Turns off of the device. |
| |
| Raises: |
| DeviceError: turning light off fails. |
| """ |
| del params # not used |
| try: |
| self.endpoint.on_off.off() |
| except errors.DeviceError as exc: |
| logger.exception(f'Turning {self.dut.name} off failed.') |
| raise exc |