Tapo P100 Smart Plug Controller

5.1.7 · active · verified Thu Apr 16

PlugP100 is an asynchronous Python library designed to control TP-Link Tapo P100, P105, P110, L900, L920, and similar smart devices locally. It provides a simple API for common operations like turning devices on/off, checking status, and accessing energy monitoring data where available. The current stable version is 5.1.7, with active development on version 6.x indicating a potential upcoming major release with significant changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a Tapo P100-series smart plug, retrieve its status, and toggle its power state. It uses environment variables for secure credential management and handles common exceptions. The library is asynchronous, so the code must be run within an `asyncio` event loop.

import asyncio
import os
from plugp100.api.plug_device import PlugP100
from plugp100.common.exceptions import TapoError

async def main():
    # It's recommended to use environment variables for sensitive data
    ip_address = os.environ.get("TAPO_IP", "192.168.1.100") # Replace with your device IP
    username = os.environ.get("TAPO_USERNAME", "your_tapo_email@example.com") # Your Tapo account email
    password = os.environ.get("TAPO_PASSWORD", "YourTapoPassword") # Your Tapo account password

    if not all([ip_address, username, password]):
        print("Please set TAPO_IP, TAPO_USERNAME, and TAPO_PASSWORD environment variables.")
        return

    # Initialize the device client
    plug = PlugP100(ip_address, username, password)

    try:
        # Authenticate with the device
        await plug.login()
        print(f"Successfully logged in to Tapo device at {ip_address}")

        # Get device information
        device_info = await plug.get_device_info()
        print(f"Device Model: {device_info.model}")
        print(f"Device Name: {device_info.device_name}")
        print(f"Device is currently {'On' if device_info.device_on else 'Off'}")

        # Toggle the plug's power state
        if device_info.device_on:
            print("Turning off the device...")
            await plug.turn_off()
        else:
            print("Turning on the device...")
            await plug.turn_on()

        # Get updated device info to confirm state change
        device_info_updated = await plug.get_device_info()
        print(f"Device is now {'On' if device_info_updated.device_on else 'Off'}")

        # Example for P110/P105 with energy monitoring (uncomment if applicable)
        # if hasattr(plug, 'get_current_energy_usage'):
        #     energy_usage = await plug.get_current_energy_usage()
        #     print(f"Current Power: {energy_usage.current_power} mW")

    except TapoError as e:
        print(f"Tapo device error: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")
    finally:
        # Ensure the session is properly closed
        if plug.is_logged_in: # Check if plug is still logged in before attempting logout
            await plug.logout()
            print("Logged out from Tapo device.")

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

view raw JSON →