1 #!/usr/bin/env python 2 # 3 # Copyright 2017 - The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 import time 17 import enum 18 import logging 19 from acts.controllers.relay_lib.generic_relay_device import GenericRelayDevice 20 from acts.controllers.relay_lib.errors import RelayConfigError 21 from acts.controllers.relay_lib.helpers import validate_key 22 23 PAIRING_MODE_WAIT_TIME = 6 24 POWER_TOGGLE_WAIT_TIME = 1 25 MISSING_RELAY_MSG = 'Relay config for Sony XB20 "%s" missing relay "%s".' 26 27 log = logging 28 29 class Buttons(enum.Enum): 30 POWER = 'Power' 31 32 class SonyXB20Speaker(GenericRelayDevice): 33 """Sony XB20 Bluetooth Speaker model 34 35 Wraps the button presses, as well as the special features like pairing. 36 """ 37 38 def __init__(self, config, relay_rig): 39 GenericRelayDevice.__init__(self, config, relay_rig) 40 41 self.mac_address = validate_key('mac_address', config, str, 'sony_xb20') 42 43 for button in Buttons: 44 self.ensure_config_contains_relay(button.value) 45 46 def ensure_config_contains_relay(self, relay_name): 47 """Throws an error if the relay does not exist.""" 48 if relay_name not in self.relays: 49 raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name)) 50 51 def _hold_button(self, button, seconds): 52 self.hold_down(button.value) 53 time.sleep(seconds) 54 self.release(button.value) 55 56 def power_on(self): 57 self._hold_button(Buttons.POWER, POWER_TOGGLE_WAIT_TIME) 58 59 def power_off(self): 60 self._hold_button(Buttons.POWER, POWER_TOGGLE_WAIT_TIME) 61 62 def enter_pairing_mode(self): 63 self._hold_button(Buttons.POWER, PAIRING_MODE_WAIT_TIME) 64 65 def setup(self): 66 """Sets all relays to their default state (off).""" 67 GenericRelayDevice.setup(self) 68 69 def clean_up(self): 70 """Sets all relays to their default state (off).""" 71 GenericRelayDevice.clean_up(self) 72