-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
tapo_p110.py
62 lines (43 loc) · 1.73 KB
/
tapo_p110.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""P110 & P115 Example"""
import asyncio
import os
from datetime import datetime
from tapo import ApiClient
from tapo.requests import EnergyDataInterval
async def main():
tapo_username = os.getenv("TAPO_USERNAME")
tapo_password = os.getenv("TAPO_PASSWORD")
ip_address = os.getenv("IP_ADDRESS")
client = ApiClient(tapo_username, tapo_password)
device = await client.p110(ip_address)
print("Turning device on...")
await device.on()
print("Waiting 2 seconds...")
await asyncio.sleep(2)
print("Turning device off...")
await device.off()
device_info = await device.get_device_info()
print(f"Device info: {device_info.to_dict()}")
device_usage = await device.get_device_usage()
print(f"Device usage: {device_usage.to_dict()}")
current_power = await device.get_current_power()
print(f"Current power: {current_power.to_dict()}")
energy_usage = await device.get_energy_usage()
print(f"Energy usage: {energy_usage.to_dict()}")
today = datetime.today()
energy_data_hourly = await device.get_energy_data(EnergyDataInterval.Hourly, today)
print(f"Energy data (hourly): {energy_data_hourly.to_dict()}")
energy_data_daily = await device.get_energy_data(
EnergyDataInterval.Daily,
datetime(today.year, get_quarter_start_month(today), 1),
)
print(f"Energy data (daily): {energy_data_daily.to_dict()}")
energy_data_monthly = await device.get_energy_data(
EnergyDataInterval.Monthly,
datetime(today.year, 1, 1),
)
print(f"Energy data (monthly): {energy_data_monthly.to_dict()}")
def get_quarter_start_month(today: datetime) -> int:
return 3 * ((today.month - 1) // 3) + 1
if __name__ == "__main__":
asyncio.run(main())