Ejemplo n.º 1
0
    async def async_send_command(
        self,
        commands: Iterable[str],
        device: str,
        num_repeats: int,
        delay_secs: float,
        hold_secs: float,
    ):
        """Send a list of commands to one device."""
        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

        _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 commands:
                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,
            )
Ejemplo n.º 2
0
async def test_async_send_command_custom_delay(
    mock_hc, harmony_client, hass, mock_write_config
):
    """Ensure calls to send remote commands properly propagate to devices with custom delays."""
    entry = MockConfigEntry(
        domain=DOMAIN,
        data={
            CONF_HOST: "192.0.2.0",
            CONF_NAME: HUB_NAME,
            ATTR_DELAY_SECS: DEFAULT_DELAY_SECS + 2,
        },
    )

    entry.add_to_hass(hass)
    await hass.config_entries.async_setup(entry.entry_id)
    await hass.async_block_till_done()

    send_commands_mock = harmony_client.send_commands

    # Tell the TV to play by id
    await _send_commands_and_wait(
        hass,
        {
            ATTR_ENTITY_ID: ENTITY_REMOTE,
            ATTR_COMMAND: PLAY_COMMAND,
            ATTR_DEVICE: TV_DEVICE_ID,
        },
    )

    send_commands_mock.assert_awaited_once_with(
        [
            SendCommandDevice(
                device=str(TV_DEVICE_ID),
                command=PLAY_COMMAND,
                delay=float(DEFAULT_HOLD_SECS),
            ),
            DEFAULT_DELAY_SECS + 2,
        ]
    )
    send_commands_mock.reset_mock()
Ejemplo n.º 3
0
async def test_async_send_command(mock_hc, harmony_client, opp,
                                  mock_write_config):
    """Ensure calls to send remote commands properly propagate to devices."""
    entry = MockConfigEntry(domain=DOMAIN,
                            data={
                                CONF_HOST: "192.0.2.0",
                                CONF_NAME: HUB_NAME
                            })

    entry.add_to_opp(opp)
    await opp.config_entries.async_setup(entry.entry_id)
    await opp.async_block_till_done()

    send_commands_mock = harmony_client.send_commands

    # No device provided
    await _send_commands_and_wait(opp, {
        ATTR_ENTITY_ID: ENTITY_REMOTE,
        ATTR_COMMAND: PLAY_COMMAND
    })
    send_commands_mock.assert_not_awaited()

    # Tell the TV to play by id
    await _send_commands_and_wait(
        opp,
        {
            ATTR_ENTITY_ID: ENTITY_REMOTE,
            ATTR_COMMAND: PLAY_COMMAND,
            ATTR_DEVICE: TV_DEVICE_ID,
        },
    )

    send_commands_mock.assert_awaited_once_with([
        SendCommandDevice(
            device=str(TV_DEVICE_ID),
            command=PLAY_COMMAND,
            delay=float(DEFAULT_HOLD_SECS),
        ),
        DEFAULT_DELAY_SECS,
    ])
    send_commands_mock.reset_mock()

    # Tell the TV to play by name
    await _send_commands_and_wait(
        opp,
        {
            ATTR_ENTITY_ID: ENTITY_REMOTE,
            ATTR_COMMAND: PLAY_COMMAND,
            ATTR_DEVICE: TV_DEVICE_NAME,
        },
    )

    send_commands_mock.assert_awaited_once_with([
        SendCommandDevice(
            device=TV_DEVICE_ID,
            command=PLAY_COMMAND,
            delay=float(DEFAULT_HOLD_SECS),
        ),
        DEFAULT_DELAY_SECS,
    ])
    send_commands_mock.reset_mock()

    # Tell the TV to play and stop by name
    await _send_commands_and_wait(
        opp,
        {
            ATTR_ENTITY_ID: ENTITY_REMOTE,
            ATTR_COMMAND: [PLAY_COMMAND, STOP_COMMAND],
            ATTR_DEVICE: TV_DEVICE_NAME,
        },
    )

    send_commands_mock.assert_awaited_once_with([
        SendCommandDevice(
            device=TV_DEVICE_ID,
            command=PLAY_COMMAND,
            delay=float(DEFAULT_HOLD_SECS),
        ),
        DEFAULT_DELAY_SECS,
        SendCommandDevice(
            device=TV_DEVICE_ID,
            command=STOP_COMMAND,
            delay=float(DEFAULT_HOLD_SECS),
        ),
        DEFAULT_DELAY_SECS,
    ])
    send_commands_mock.reset_mock()

    # Tell the TV to play by name multiple times
    await _send_commands_and_wait(
        opp,
        {
            ATTR_ENTITY_ID: ENTITY_REMOTE,
            ATTR_COMMAND: PLAY_COMMAND,
            ATTR_DEVICE: TV_DEVICE_NAME,
            ATTR_NUM_REPEATS: 2,
        },
    )

    send_commands_mock.assert_awaited_once_with([
        SendCommandDevice(
            device=TV_DEVICE_ID,
            command=PLAY_COMMAND,
            delay=float(DEFAULT_HOLD_SECS),
        ),
        DEFAULT_DELAY_SECS,
        SendCommandDevice(
            device=TV_DEVICE_ID,
            command=PLAY_COMMAND,
            delay=float(DEFAULT_HOLD_SECS),
        ),
        DEFAULT_DELAY_SECS,
    ])
    send_commands_mock.reset_mock()

    # Send commands to an unknown device
    await _send_commands_and_wait(
        opp,
        {
            ATTR_ENTITY_ID: ENTITY_REMOTE,
            ATTR_COMMAND: PLAY_COMMAND,
            ATTR_DEVICE: "no-such-device",
        },
    )
    send_commands_mock.assert_not_awaited()
    send_commands_mock.reset_mock()