Created
April 17, 2023 21:33
-
-
Save pdcastro/961f7ef775a4aaac5a9bf5a7c5b0f482 to your computer and use it in GitHub Desktop.
Sample Python script to collect temperature and humidity data through the Tado cloud API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from dataclasses import dataclass | |
from datetime import datetime, timezone | |
from PyTado.interface import Tado | |
TADO_USERNAME = "xxxx" | |
TADO_PASSWORD = "xxxx" | |
@dataclass | |
class PollHandle: | |
handle: asyncio.Handle | |
def poll_tado() -> PollHandle: | |
tado = Tado(TADO_USERNAME, TADO_PASSWORD) | |
zones: list[dict] = tado.getZones() | |
zone: dict = next(filter(lambda x: x.get("type") == "HEATING", zones)) | |
zone_id: int = zone["id"] | |
iso = datetime.fromisoformat | |
loop = asyncio.get_running_loop() | |
t0 = loop.time() | |
interval_sec = 60 | |
count = 0 | |
last_ts = "" | |
def callback(): | |
nonlocal count, last_ts, poll_handle | |
now_utc = datetime.now(timezone.utc).strftime("%H:%M:%S") | |
data = tado.getState(zone_id)["sensorDataPoints"] | |
humidity = data["humidity"]["percentage"] | |
celsius = data["insideTemperature"]["celsius"] | |
ts = data["insideTemperature"]["timestamp"] | |
delta = (iso(ts) - iso(last_ts)).total_seconds() / 60 if last_ts else 0.0 | |
print(f"{count:04} {now_utc=} {ts=} {delta=:.2F}minutes {celsius=} {humidity=}%") | |
last_ts = ts | |
count += 1 | |
poll_handle.handle = loop.call_at(t0 + count * interval_sec, callback) | |
poll_handle = PollHandle(loop.call_soon(callback)) | |
return poll_handle | |
async def main(): | |
poll_handle = poll_tado() | |
while not poll_handle.handle.cancelled(): | |
try: | |
await asyncio.sleep(10) | |
except asyncio.CancelledError: # CTRL-C | |
break | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample script output: