# Interact with child devices

Note

The library is fully async and methods that perform IO need to be run inside an async coroutine. Code examples assume you are following them inside `asyncio REPL`:

        $ python -m asyncio

Or the code is running inside an async function:

    import asyncio
    from kasa import Discover

    async def main():
        dev = await Discover.discover_single("127.0.0.1",username="un@example.com",password="pw")
        await dev.turn_on()
        await dev.update()

    if __name__ == "__main__":
        asyncio.run(main())

All of your code needs to run inside the same event loop so only call `asyncio.run` once.

The main entry point for the API is `discover()` and `discover_single()` which return Device objects. Most newer devices require your TP-Link cloud username and password, but this can be omitted for older devices.

Interact with child devices.

    >>> from kasa import Discover
    >>>
    >>> dev = await Discover.discover_single(
    >>>     "127.0.0.1",
    >>>     username="user@example.com",
    >>>     password="great_password"
    >>> )
    >>> await dev.update()
    >>> print(dev.alias)
    Bedroom Power Strip

All methods act on the whole strip:

    >>> for plug in dev.children:
    >>>    print(f"{plug.alias}: {plug.is_on}")
    Plug 1: True
    Plug 2: False
    Plug 3: False
    >>> dev.is_on
    True
    >>> await dev.turn_off()
    >>> await dev.update()

Accessing individual plugs can be done using the children property:

    >>> len(dev.children)
    3
    >>> for plug in dev.children:
    >>>    print(f"{plug.alias}: {plug.is_on}")
    Plug 1: False
    Plug 2: False
    Plug 3: False
    >>> await dev.children[1].turn_on()
    >>> await dev.update()
    >>> dev.is_on
    True

## Pairing and unpairing

Module for childsetup interface.

The childsetup module allows pairing and unpairing of supported child device types to hubs.

    >>> from kasa import Discover, Module, LightState
    >>>
    >>> dev = await Discover.discover_single(
    >>>     "127.0.0.6",
    >>>     username="user@example.com",
    >>>     password="great_password"
    >>> )
    >>> await dev.update()
    >>> print(dev.alias)
    Tapo Hub

    >>> childsetup = dev.modules[Module.ChildSetup]
    >>> childsetup.supported_categories
    ['camera', 'subg.trv', 'subg.trigger', 'subg.plugswitch']

Put child devices in pairing mode. The hub will pair with all supported devices in pairing mode:

    >>> added = await childsetup.pair()
    >>> added
    [{'device_id': 'SCRUBBED_CHILD_DEVICE_ID_5', 'category': 'subg.trigger.button', 'device_model': 'S200B', 'name': 'I01BU0tFRF9OQU1FIw===='}]

    >>> for child in dev.children:
    >>>     print(f"{child.device_id} - {child.model}")
    SCRUBBED_CHILD_DEVICE_ID_1 - T310
    SCRUBBED_CHILD_DEVICE_ID_2 - T315
    SCRUBBED_CHILD_DEVICE_ID_3 - T110
    SCRUBBED_CHILD_DEVICE_ID_4 - S200B
    SCRUBBED_CHILD_DEVICE_ID_5 - S200B

Unpair with the child device_id:

    >>> await childsetup.unpair("SCRUBBED_CHILD_DEVICE_ID_4")
    >>> for child in dev.children:
    >>>     print(f"{child.device_id} - {child.model}")
    SCRUBBED_CHILD_DEVICE_ID_1 - T310
    SCRUBBED_CHILD_DEVICE_ID_2 - T315
    SCRUBBED_CHILD_DEVICE_ID_3 - T110
    SCRUBBED_CHILD_DEVICE_ID_5 - S200B