Example #1
0
 def setup(self):
     """Sets all relays to their default state (off)."""
     GenericRelayDevice.setup(self)
     # If the Fugu remote does have a power relay attached, turn it on.
     power = 'Power'
     if power in self.relays:
         self.relays[power].set_nc()
Example #2
0
    def __init__(self, config, relay_rig):
        GenericRelayDevice.__init__(self, config, relay_rig)

        self.mac_address = validate_key('mac_address', config, str, 'ak_xb10')

        for button in Buttons:
            self.ensure_config_contains_relay(button.value)
Example #3
0
    def __init__(self, config, relay_rig):
        GenericRelayDevice.__init__(self, config, relay_rig)

        self.mac_address = validate_key('mac_address', config, str,
                                        'SingleButtonDongle')

        self.ensure_config_contains_relay(Buttons.ACTION.value)
    def test_setup_multiple_relays(self):
        self.board.set(self.r0.position, RelayState.NC)
        self.board.set(self.r1.position, RelayState.NC)

        grd = GenericRelayDevice(self.device_config, self.rig)
        grd.setup()

        self.assertEqual(self.r0.get_status(), RelayState.NO)
        self.assertEqual(self.r1.get_status(), RelayState.NO)
    def test_setup_single_relay(self):
        self.r0.set(RelayState.NC)
        self.r1.set(RelayState.NC)

        modified_config = copy.deepcopy(self.device_config)
        del modified_config['relays']['r1']

        grd = GenericRelayDevice(modified_config, self.rig)
        grd.setup()

        self.assertEqual(self.r0.get_status(), RelayState.NO)
        self.assertEqual(self.r1.get_status(), RelayState.NC)
Example #6
0
    def create_relay_device(self, config):
        """Builds a RelayDevice from the given config.

        When given no 'type' key in the config, the function will default to
        returning a GenericRelayDevice with the relays found in the 'relays'
        array.

        Args:
            config: An object containing 'name', 'relays', and (optionally)
            type.

        Returns:
            A RelayDevice with the given type found in the config. If no type is
            found, it will default to GenericRelayDevice.

        Raises:
            RelayConfigError if the type given does not match any from the
            _device_constructors dictionary.

        """
        if 'type' in config:
            if config['type'] not in RelayRig._device_constructors:
                raise RelayConfigError(
                    'Device with type {} not found. Has it been added '
                    'to the _device_constructors dict?'.format(config['type']))
            else:
                device = self._device_constructors[config['type']](config,
                                                                   self)

        else:
            device = GenericRelayDevice(config, self)

        return device
 def change_state(self, begin_state, call, end_state, previous_state=None):
     self.board.set(self.r0.position, begin_state)
     grd = GenericRelayDevice(self.device_config, self.rig)
     call(grd)
     self.assertEqual(self.r0.get_status(), end_state)
     if previous_state:
         self.assertEqual(
             self.board.relay_previous_states[self.r0.position],
             previous_state)
    def __init__(self, config, relay_rig):
        GenericRelayDevice.__init__(self, config, relay_rig)

        self.mac_address = validate_key('mac_address', config, str,
                                        self.__class__.__name__)
Example #9
0
 def clean_up(self):
     """Sets all relays to their default state (off)."""
     GenericRelayDevice.clean_up(self)
Example #10
0
 def setup(self):
     """Sets all relays to their default state (off)."""
     GenericRelayDevice.setup(self)
Example #11
0
class RelayRig:
    """A group of relay boards and their connected devices.

    This class is also responsible for handling the creation of the relay switch
    boards, as well as the devices and relays associated with them.

    The boards dict can contain different types of relay boards. They share a
    common interface through inheriting from RelayBoard. This layer can be
    ignored by the user.

    The relay devices are stored in a dict of (device_name: device). These
    device references should be used by the user when they want to directly
    interface with the relay switches. See RelayDevice or GeneralRelayDevice for
    implementation.

    """
    DUPLICATE_ID_ERR_MSG = 'The {} "{}" is not unique. Duplicated in:\n {}'

    # A dict of lambdas that instantiate relay board upon invocation.
    # The key is the class type name, the value is the lambda.
    _board_constructors = {
        'SainSmartBoard': lambda x: SainSmartBoard(x),
    }

    # Similar to the dict above, except for devices.
    _device_constructors = {
        'GenericRelayDevice': lambda x, rig: GenericRelayDevice(x, rig),
        'FuguRemote': lambda x, rig: FuguRemote(x, rig),
        'SonyXB2Speaker': lambda x, rig: SonyXB2Speaker(x, rig),
    }

    def __init__(self, config):
        self.relays = dict()
        self.boards = dict()
        self.devices = dict()

        validate_key('boards', config, list, 'relay config file')

        for elem in config['boards']:
            board = self.create_relay_board(elem)
            if board.name in self.boards:
                raise RelayConfigError(
                    self.DUPLICATE_ID_ERR_MSG.format('name', elem['name'],
                                                     elem))
            self.boards[board.name] = board

        # Note: 'boards' is a necessary value, 'devices' is not.
        if 'devices' in config:
            for elem in config['devices']:
                relay_device = self.create_relay_device(elem)
                if relay_device.name in self.devices:
                    raise RelayConfigError(
                        self.DUPLICATE_ID_ERR_MSG.format(
                            'name', elem['name'], elem))
                self.devices[relay_device.name] = relay_device
        else:
            device_config = dict()
            device_config['name'] = 'GenericRelayDevice'
            device_config['relays'] = dict()
            for relay_id in self.relays:
                device_config['relays'][relay_id] = relay_id
            self.devices['device'] = self.create_relay_device(device_config)

    def create_relay_board(self, config):
        """Builds a RelayBoard from the given config.

        Args:
            config: An object containing 'type', 'name', 'relays', and
            (optionally) 'properties'. See the example json file.

        Returns:
            A RelayBoard with the given type found in the config.

        Raises:
            RelayConfigError if config['type'] doesn't exist or is not a string.

        """
        validate_key('type', config, str, '"boards" element')
        try:
            ret = self._board_constructors[config['type']](config)
        except LookupError:
            raise RelayConfigError(
                'RelayBoard with type {} not found. Has it been added '
                'to the _board_constructors dict?'.format(config['type']))
        for _, relay in ret.relays.items():
            self.relays[relay.relay_id] = relay
        return ret

    def create_relay_device(self, config):
        """Builds a RelayDevice from the given config.

        When given no 'type' key in the config, the function will default to
        returning a GenericRelayDevice with the relays found in the 'relays'
        array.

        Args:
            config: An object containing 'name', 'relays', and (optionally)
            type.

        Returns:
            A RelayDevice with the given type found in the config. If no type is
            found, it will default to GenericRelayDevice.

        Raises:
            RelayConfigError if the type given does not match any from the
            _device_constructors dictionary.

        """
        if 'type' in config:
            if config['type'] not in RelayRig._device_constructors:
                raise RelayConfigError(
                    'Device with type {} not found. Has it been added '
                    'to the _device_constructors dict?'.format(config['type']))
            else:
                device = self._device_constructors[config['type']](config,
                                                                   self)

        else:
            device = GenericRelayDevice(config, self)

        return device
Example #12
0
 def setup(self):
     GenericRelayDevice.setup(self)