Discovering devices

API documentation

class kasa.Discover[source]

Discover TPLink Smart Home devices.

The main entry point for this library is Discover.discover(), which returns a dictionary of the found devices. The key is the IP address of the device and the value contains ready-to-use, SmartDevice-derived device object.

discover_single() can be used to initialize a single device given its IP address. If the type of the device and its IP address is already known, you can initialize the corresponding device class directly without this.

The protocol uses UDP broadcast datagrams on port 9999 for discovery.

Examples:

Discovery returns a list of discovered devices:

>>> import asyncio
>>> found_devices = asyncio.run(Discover.discover())
>>> [dev.alias for dev in found_devices]
['TP-LINK_Power Strip_CF69']

Discovery can also be targeted to a specific broadcast address instead of the 255.255.255.255:

>>> asyncio.run(Discover.discover(target="192.168.8.255"))

It is also possible to pass a coroutine to be executed for each found device:

>>> async def print_alias(dev):
>>>    print(f"Discovered {dev.alias}")
>>> devices = asyncio.run(Discover.discover(on_discovered=print_alias))
DISCOVERY_PORT = 9999
DISCOVERY_QUERY = {'system': {'get_sysinfo': None}}
async static discover(*, target='255.255.255.255', on_discovered=None, timeout=5, discovery_packets=3, interface=None) Dict[str, kasa.smartdevice.SmartDevice][source]

Discover supported devices.

Sends discovery message to 255.255.255.255:9999 in order to detect available supported devices in the local network, and waits for given timeout for answers from devices. If you have multiple interfaces, you can use target parameter to specify the network for discovery.

If given, on_discovered coroutine will get awaited with a SmartDevice-derived object as parameter.

The results of the discovery are returned as a dict of SmartDevice-derived objects keyed with IP addresses. The devices are already initialized and all but emeter-related properties can be accessed directly.

Parameters
  • target – The target address where to send the broadcast discovery queries if multi-homing (e.g. 192.168.xxx.255).

  • on_discovered – coroutine to execute on discovery

  • timeout – How long to wait for responses, defaults to 5

  • discovery_packets – Number of discovery packets to broadcast

  • interface – Bind to specific interface

Returns

dictionary with discovered devices

async static discover_single(host: str) kasa.smartdevice.SmartDevice[source]

Discover a single device by the given IP address.

Parameters

host – Hostname of device to query

Return type

SmartDevice

Returns

Object for querying/controlling found device.