-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsync_octopus_tado.py
79 lines (63 loc) · 2.48 KB
/
sync_octopus_tado.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import argparse
import requests
from requests.auth import HTTPBasicAuth
from PyTado.interface import Tado
def get_meter_reading_total_consumption(api_key, mprn, gas_serial_number):
"""
Retrieves total gas consumption from the Octopus Energy API for the given gas meter point and serial number.
"""
url = f"https://api.octopus.energy/v1/gas-meter-points/{mprn}/meters/{gas_serial_number}/consumption/?group_by=quarter"
total_consumption = 0.0
while url:
response = requests.get(
url, auth=HTTPBasicAuth(api_key, "")
)
if response.status_code == 200:
meter_readings = response.json()
total_consumption += sum(
interval["consumption"] for interval in meter_readings["results"]
)
url = meter_readings.get("next", "")
else:
print(
f"Failed to retrieve data. Status code: {response.status_code}, Message: {response.text}"
)
break
print(f"Total consumption is {total_consumption}")
return total_consumption
def send_reading_to_tado(username, password, reading):
"""
Sends the total consumption reading to Tado using its Energy IQ feature.
"""
tado = Tado(username, password)
result = tado.set_eiq_meter_readings(reading=int(reading))
print(result)
def parse_args():
"""
Parses command-line arguments for Tado and Octopus API credentials and meter details.
"""
parser = argparse.ArgumentParser(
description="Tado and Octopus API Interaction Script"
)
# Tado API arguments
parser.add_argument("--tado-email", required=True, help="Tado account email")
parser.add_argument("--tado-password", required=True, help="Tado account password")
# Octopus API arguments
parser.add_argument(
"--mprn",
required=True,
help="MPRN (Meter Point Reference Number) for the gas meter",
)
parser.add_argument(
"--gas-serial-number", required=True, help="Gas meter serial number"
)
parser.add_argument("--octopus-api-key", required=True, help="Octopus API key")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
# Get total consumption from Octopus Energy API
consumption = get_meter_reading_total_consumption(
args.octopus_api_key, args.mprn, args.gas_serial_number
)
# Send the total consumption to Tado
send_reading_to_tado(args.tado_email, args.tado_password, consumption)