Пример #1
0
    async def async_send_command(self, command, **kwargs):
        """Send a list of commands to one device."""
        _LOGGER.debug("%s: Send Command", self.name)
        device = kwargs.get(ATTR_DEVICE)
        if device is None:
            _LOGGER.error("%s: Missing required argument: device", self.name)
            return

        device_id = None
        if device.isdigit():
            _LOGGER.debug("%s: Device %s is numeric", self.name, device)
            if self._client.get_device_name(int(device)):
                device_id = device

        if device_id is None:
            _LOGGER.debug("%s: Find device ID %s based on device name",
                          self.name, device)
            device_id = self._client.get_device_id(str(device).strip())

        if device_id is None:
            _LOGGER.error("%s: Device %s is invalid", self.name, device)
            return

        num_repeats = kwargs[ATTR_NUM_REPEATS]
        delay_secs = kwargs.get(ATTR_DELAY_SECS, self._delay_secs)
        hold_secs = kwargs[ATTR_HOLD_SECS]
        _LOGGER.debug(
            "Sending commands to device %s holding for %s seconds "
            "with a delay of %s seconds",
            device,
            hold_secs,
            delay_secs,
        )

        # Creating list of commands to send.
        snd_cmnd_list = []
        for _ in range(num_repeats):
            for single_command in command:
                send_command = SendCommandDevice(device=device_id,
                                                 command=single_command,
                                                 delay=hold_secs)
                snd_cmnd_list.append(send_command)
                if delay_secs > 0:
                    snd_cmnd_list.append(float(delay_secs))

        _LOGGER.debug("%s: Sending commands", self.name)
        try:
            result_list = await self._client.send_commands(snd_cmnd_list)
        except aioexc.TimeOut:
            _LOGGER.error("%s: Sending commands timed-out", self.name)
            return

        for result in result_list:
            _LOGGER.error(
                "Sending command %s to device %s failed with code %s: %s",
                result.command.command,
                result.command.device,
                result.code,
                result.msg,
            )
Пример #2
0
    async def async_send_command(self, command, **kwargs):
        """Send a list of commands to one device."""
        from aioharmony.harmonyapi import SendCommandDevice
        _LOGGER.debug("%s: Send Command", self.name)
        device = kwargs.get(ATTR_DEVICE)
        if device is None:
            _LOGGER.error("%s: Missing required argument: device", self.name)
            return

        device_id = None
        if device.isdigit():
            _LOGGER.debug("%s: Device %s is numeric", self.name, device)
            if self._client.get_device_name(int(device)):
                device_id = device

        if device_id is None:
            _LOGGER.debug("%s: Find device ID %s based on device name",
                          self.name, device)
            device_id = self._client.get_device_id(str(device).strip())

        if device_id is None:
            _LOGGER.error("%s: Device %s is invalid", self.name, device)
            return

        num_repeats = kwargs.get(ATTR_NUM_REPEATS)
        delay_secs = kwargs.get(ATTR_DELAY_SECS, self._delay_secs)

        # Creating list of commands to send.
        snd_cmnd_list = []
        for _ in range(num_repeats):
            for single_command in command:
                send_command = SendCommandDevice(device=device,
                                                 command=single_command,
                                                 delay=0)
                snd_cmnd_list.append(send_command)
                if delay_secs > 0:
                    snd_cmnd_list.append(float(delay_secs))

        _LOGGER.debug("%s: Sending commands", self.name)
        result_list = await self._client.send_commands(snd_cmnd_list)
        for result in result_list:
            _LOGGER.warning(
                "Sending command %s to device %s failed with code "
                "%s: %s", result.command.command, result.command.device,
                result.command.code, result.command.msg)
Пример #3
0
async def send_command(client, args):
    """Connects to the Harmony and send a simple command.

    Args:
        args (argparse): Argparse object containing required variables from
        command line

    """
    device_id = None
    if args.device_id.isdigit():
        if client.get_device_name(int(args.device_id)):
            device_id = args.device_id

    if device_id is None:
        device_id = client.get_device_id(str(args.device_id).strip())

    if device_id is None:
        print("Device {} is invalid.".format(args.device_id))
        return

    snd_cmmnd = SendCommandDevice(
        device=device_id,
        command=args.command,
        delay=args.hold_secs)

    snd_cmmnd_list = []
    for _ in range(args.repeat_num):
        snd_cmmnd_list.append(snd_cmmnd)
        if args.delay_secs > 0:
            snd_cmmnd_list.append(args.delay_secs)

    result_list = await client.send_commands(snd_cmmnd_list)

    if result_list:
        for result in result_list:
            print("Sending of command {} to device {} failed with code {}: "
                  "{}".format(
                      result.command.command,
                      result.command.device,
                      result.code,
                      result.msg))
    else:
        print('Command Sent')
Пример #4
0
async def send_command(request):
    if request.body_exists:
        json_data = await request.json()
        device = json_data.get("id")
        command = json_data.get("command")
        client = await get_client()
        if client is not None:
            send_command_args = SendCommandDevice(
                device=device,
                command=command,
                delay=0,
            )
            res = await client.send_commands(send_command_args)
            await client.close()
            if res:
                return web.json_response(
                    {
                        "status": "Bad Request",
                        "message": res[0].msg
                    },
                    status=400)
            else:
                return web.json_response({
                    "status": "ok",
                    "message": "Command sent successfully"
                })
        else:
            return web.json_response(
                {
                    "status": "ERROR",
                    "message": "HUB not connect"
                }, status=404)
    else:
        return web.json_response(
            {
                "status":
                "Bad Request",
                "message":
                "It is necessary to transfer the device id and command name in JSON format. Example: {'id': '38213988', 'command': 'Stereo'}",
            },
            status=400,
        )
Пример #5
0
        async def command(self, device_id, command, delay=0):
            device_id = str(device_id)

            if device_id not in self.actor.devices:
                return False, 'Unknown device id (%s)' % (device_id)

            try:
                cmd = SendCommandDevice(device_id, command, delay)

                await self.actor.client.send_commands(cmd)
                # self.actor.client.power_off()

            except aioexc.TimeOut:
                return False, 'Command send timed out' % (command, device_id)

            except:
                logger.exception('!!!!!!!' * 100)

            return True, 'Command %s sent to %s (%s)' % (
                command, self.actor.devices[device_id], device_id)
Пример #6
0
async def handleInput(props):
    commandName = props['commandName']

    if commandName == 'close':
        await CLIENT.close()

    elif commandName == 'get-config':
        return CLIENT.json_config

    elif commandName == 'send-command':
        command = SendCommandDevice(device=props['deviceId'],
                                    command=props['command'],
                                    delay=props['delay'])

        commandList = []
        for _ in range(props['repeats']):
            commandList.append(command)
            if props['delay'] > 0:
                commandList.append(props['delay'])

        resultList = await CLIENT.send_commands(commandList)

        if resultList:
            errorList = []

            for result in resultList:
                errorList.append(result.msg)

            return {'errors': errorList}

    elif commandName == 'start-activity':
        response = await CLIENT.start_activity(props['id'])

        if not response[0]:
            return {'errors': [response[1]]}

    return {}