-
Notifications
You must be signed in to change notification settings - Fork 14
Getting Started
piekstra edited this page Feb 7, 2026
·
1 revision
pip install tplink-cloud-apigit clone https://github.com/piekstra/tplink-cloud-api.git
cd tplink-cloud-api
pip install .import asyncio
from tplinkcloud import TPLinkDeviceManager
async def main():
# Login and fetch devices
device_manager = await TPLinkDeviceManager(
username='your_kasa_email@example.com',
password='your_password'
)
# List all devices
devices = await device_manager.get_devices()
for device in devices:
print(f"{device.model_type.name}: {device.get_alias()}")
# Control a device
device = await device_manager.find_device("My Smart Plug")
if device:
await device.power_on() # Turn on
await device.power_off() # Turn off
await device.toggle() # Toggle current state
asyncio.run(main())# Exact match
device = await device_manager.find_device("Living Room Light")
# Partial match (case-insensitive)
devices = await device_manager.find_devices("light")
for device in devices:
print(device.get_alias())import json
device = await device_manager.find_device("My Smart Plug")
power_usage = await device.get_power_usage_realtime()
print(json.dumps(power_usage, indent=2, default=lambda x: x.__dict__))from tplinkcloud import TPLinkDeviceScheduleRuleBuilder
device = await device_manager.find_device("My Smart Plug")
# Get existing rules
schedule = await device.get_schedule_rules()
# Add a new rule (turn on at sunset on weekends)
new_rule = TPLinkDeviceScheduleRuleBuilder() \
.with_action(turn_on=True) \
.with_name('Weekend Sunset') \
.with_enable_status(True) \
.with_sunset_start() \
.with_repeat_on_days([0, 0, 0, 0, 0, 1, 1]) \
.build()
await device.add_schedule_rule(new_rule.to_json())If you already have a token (e.g., from a previous session):
device_manager = TPLinkDeviceManager()
device_manager.set_auth_token('your_token_here')
devices = await device_manager.get_devices()device_manager = TPLinkDeviceManager(
username='email@example.com',
password='password',
prefetch=True, # Fetch devices on init (default: True)
cache_devices=True, # Cache device list (default: True)
tplink_cloud_api_host=None, # Custom API host (default: TP-Link cloud)
verbose=False, # Print debug output (default: False)
term_id=None # Terminal UUID (default: auto-generated)
)- Python 3.10+
- A TP-Link / Kasa account (create one in the Kasa app)
- Devices registered to your account via the Kasa app
This error occurs when using the V1 API with accounts that have 2FA enabled. Upgrade to v5.0+ which supports the V2 API.
The V2 API uses TP-Link's private CA. The library bundles the CA chain automatically. If you encounter SSL errors, ensure you're on the latest version.
Tokens expire after some time. v5.0+ supports automatic token refresh. For v4.x, create a new TPLinkDeviceManager instance to re-login.
Ensure the device is:
- Registered to your Kasa account
- Connected to WiFi
- Using the exact alias from the Kasa app (case-sensitive for
find_device)