Home | History | Annotate | Download | only in relay_lib
      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.relay import SynchronizeRelays
     21 from acts.controllers.relay_lib.errors import RelayConfigError
     22 from acts.controllers.relay_lib.helpers import validate_key
     23 
     24 PAIRING_MODE_WAIT_TIME = 5
     25 POWER_ON_WAIT_TIME = 2
     26 POWER_OFF_WAIT_TIME = 6
     27 MISSING_RELAY_MSG = 'Relay config for Sony XB2 "%s" missing relay "%s".'
     28 
     29 log = logging
     30 
     31 
     32 class Buttons(enum.Enum):
     33     POWER = 'Power'
     34     PAIR = 'Pair'
     35 
     36 
     37 class SonyXB2Speaker(GenericRelayDevice):
     38     """Sony XB2 Bluetooth Speaker model
     39 
     40     Wraps the button presses, as well as the special features like pairing.
     41     """
     42 
     43     def __init__(self, config, relay_rig):
     44         GenericRelayDevice.__init__(self, config, relay_rig)
     45 
     46         self.mac_address = validate_key('mac_address', config, str, 'sony_xb2')
     47 
     48         for button in Buttons:
     49             self.ensure_config_contains_relay(button.value)
     50 
     51     def ensure_config_contains_relay(self, relay_name):
     52         """Throws an error if the relay does not exist."""
     53         if relay_name not in self.relays:
     54             raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name))
     55 
     56     def _hold_button(self, button, seconds):
     57         self.hold_down(button.value)
     58         time.sleep(seconds)
     59         self.release(button.value)
     60 
     61     def power_on(self):
     62         self._hold_button(Buttons.POWER, POWER_ON_WAIT_TIME)
     63 
     64     def power_off(self):
     65         self._hold_button(Buttons.POWER, POWER_OFF_WAIT_TIME)
     66 
     67     def enter_pairing_mode(self):
     68         self._hold_button(Buttons.PAIR, PAIRING_MODE_WAIT_TIME)
     69 
     70     def setup(self):
     71         """Sets all relays to their default state (off)."""
     72         GenericRelayDevice.setup(self)
     73 
     74     def clean_up(self):
     75         """Sets all relays to their default state (off)."""
     76         GenericRelayDevice.clean_up(self)
     77