[Python] How to Control TP-Link Tapo Plugs from Another Network Using PlugP100 or Python-Kasa...

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am using the PlugP100 / Python-Kasa library to control my TP-Link smart plugs. Currently, I can send commands to the plugs when I am on the same network and know their IP addresses. However, I have plugs located on different networks, and I cannot directly access those networks, so the commands won't work in such cases.

    When I am not on the same network, device discovery still works, but I cannot retrieve the IP addresses of those devices.

    How can I control or interact with my plugs from a different network? Are there alternative solutions for remote control using this library or other methods?

    is there a MQTT way to do it?

    python-kasa:

    async def main():
    dev = await Discover.discover_single("192.168.x.x",username=EMAIL,password=PASSWORD)
    await dev.turn_on()
    await dev.update()


    plugp100:

    connectedDevices = [] # List to store connected devices
    async def example_discovery(credentials: AuthCredential):
    discovered = await TapoDiscovery.scan(timeout=5)
    device_objects = [] # List to store created Device objects
    for discovered_device in discovered:
    try:
    # Initialize the Tapo device
    device = await discovered_device.get_tapo_device(credentials)
    # Update the device state
    await device.update()
    # Extract raw_state and create a Device object
    raw_state = device.raw_state
    device_objects.append(raw_state)
    # print(new_device)
    await device.client.close()

    except Exception as e:
    logging.error(f"Failed to update {discovered_device.ip} {discovered_device.device_type}", exc_info=e)
    return device_objects # Return the list of created Device objects

    async def connectAll(rawDeviceList):
    for device in rawDeviceList:
    device_configuration = DeviceConnectConfiguration(
    host=device.ip,
    credentials=AuthCredential(EMAIL, PASSWORD)
    )
    connectedDevice = await connect(device_configuration)
    await connectedDevice.update()
    connectedDevices.append(connectedDevice)
    return connectedDevices

    async def toggle_device_state(ip, state, connected_devices):
    for device in connected_devices:
    if ip == device.raw_state.get('ip', 'Unknown'):
    if state == "on":
    await device.turn_on()
    elif state == "off":
    await device.turn_off()
    else:
    print("Invalid state. Please enter 'on' or 'off'.")

    Continue reading...

Compartilhe esta Página