Example #1
0
def send_message(device_address, message):
    """Sends a single message to a connected device.

    :param device_address: The hex-address of the device.
    :param message: A message from sirius.protocol.messages.

    :returns: 2-tuple of (success, command id used - even if the sending failed)
    """
    command_id = _get_next_command_id()

    # Search for bridge.
    for bridge_state in bridge_by_address.values():
        if device_address in bridge_state.connected_devices:
            break
    else:
        return (False, command_id)

    # Send data through the websocket.
    command = encoders.encode_bridge_command(
        bridge_state.address,
        message,
        command_id,
        '0',
    )
    bridge_state.websocket.send(json.dumps(command))

    # Remember the command we just sent and increment the command id.
    # TODO - implement timeout logic.
    bridge_state.pending_commands[command_id] = message

    return (True, command_id)
Example #2
0
def send_message(device_address, message):
    """Sends a single message to a connected device.

    :param device_address: The hex-address of the device.
    :param message: A message from sirius.protocol.messages.

    :returns: 2-tuple of (success, command id used - even if the sending failed)
    """
    command_id = _get_next_command_id()

    # Search for bridge.
    for bridge_state in bridge_by_address.values():
        if device_address in bridge_state.connected_devices:
            break
    else:
        return (False, command_id)

    # Send data through the websocket.
    command = encoders.encode_bridge_command(
        bridge_state.address,
        message,
        command_id,
        '0',
    )
    bridge_state.websocket.send(json.dumps(command))

    # Remember the command we just sent and increment the command id.
    # TODO - implement timeout logic.
    bridge_state.pending_commands[command_id] = message

    return (True, command_id)
Example #3
0
    def test_add_device_encryption_key(self):
        claim_code = '6xwh-441j-8115-zyrh'
        expected_encryption_key = 'F7D9bmztHV32+WJScGZR0g=='

        command = messages.AddDeviceEncryptionKey(
            'some-bridge-address',
            'some-device-address',
            claim_code
        )

        json = encoders.encode_bridge_command('some-bridge-address', command, 1, '0')

        expected_json = {
            'type': 'BridgeCommand',
            'bridge_address': 'some-bridge-address',
            'command_id': 1,
            'timestamp': '0',
            'json_payload': {
                'name': 'add_device_encryption_key',
                'params': {
                    'device_address': 'some-device-address',
                    'encryption_key': expected_encryption_key,
                }
            }
        }

        self.assertDictEqual(expected_json, json)
Example #4
0
    def test_set_delivery_no_face(self):
        command = messages.SetDeliveryNoFace('some-device-address', self.bw_2x2())

        json = encoders.encode_bridge_command('some-bridge-address', command, 1, '0')

        expected_json = {
            'type': 'DeviceCommand',
            'bridge_address': 'some-bridge-address',
            'device_address': 'some-device-address',
            'command_id': 1,
            'timestamp': '0',
            'binary_payload': 'AQASAAEAAAAAAAAAJwAAACMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECAQ=='
        }

        self.assertDictEqual(expected_json, json)
Example #5
0
    def test_set_personality_with_message(self):
        command = messages.SetPersonalityWithMessage('some-device-address', self.bw_2x2(), self.bw_2x2(), self.bw_2x2(), self.bw_2x2(), self.bw_2x2())

        json = encoders.encode_bridge_command('some-bridge-address', command, 1, '0')

        expected_json = {
            'type': 'DeviceCommand',
            'bridge_address': 'some-bridge-address',
            'device_address': 'some-device-address',
            'command_id': 1,
            'timestamp': '0',
            'binary_payload': 'AQABAQEAAAAAAAAAwwAAACMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECASMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECASMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECASMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECASMAAAAAABUAAAAdcwPoHWHQHS8PHUSAGyoAAAAAADABAwAAAAECAQ=='
        }

        self.assertDictEqual(expected_json, json)