-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_readings.py
192 lines (150 loc) · 5.5 KB
/
submit_readings.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import datetime
import argparse
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.exceptions import TransportQueryError
from AccountInfo import AccountInfo
transport = AIOHTTPTransport(url="https://api.eonnext-kraken.energy/v1/graphql/")
client = Client(transport=transport, fetch_schema_from_transport=True)
parser = argparse.ArgumentParser(description="Submit electricity and gas meter readings to Sainsbury's Energy")
parser.add_argument('--email', action="store", type=str, required=True, help="Your Sainsbury's Energy account email")
parser.add_argument('--password', action="store", type=str, required=True,
help="Your Sainsbury's Energy account password")
parser.add_argument('--gas', action="store", type=int, help="An integer value to submit as the gas meter reading")
parser.add_argument('--elec', action="store", type=int,
help="An integer value to submit as the electricity meter reading")
args = parser.parse_args()
def get_refresh_token(password: str, email: str):
token_mutation = gql(
f"""
mutation getToken1 {{
obtainKrakenToken(input: {{
email: "{email}",
password: "{password}"
}}){{
token
}}
}}
"""
)
result = client.execute(token_mutation)
token = result["obtainKrakenToken"]["token"]
print(f"Token obtained: {token[:10]}...")
return token
def get_account_number():
query = gql(
f"""
query viewer{{
viewer {{
accounts {{
number
}}
}}
}}
"""
)
result = client.execute(query)
result_number = result["viewer"]["accounts"][0]["number"]
print(f"Account number obtained: {result_number}")
return result_number
def get_meter_info(ac_no: str):
query = gql(
f"""
query getAccount{{
account(accountNumber: "{ac_no}"){{
id, status, properties {{
id, electricityMeterPoints {{
id, mpan, meters {{
serialNumber, registers {{
identifier
}}
}}
}}, gasMeterPoints {{
id, mprn, meters {{
serialNumber, registers {{
expectedReadingRanges
}}
}}
}}
}}
}}
}}
"""
)
result = client.execute(query)
result_obj = AccountInfo(result)
print(f"Meter info obtained: {result_obj}")
return result_obj
def submit_gas_reading(account_info: AccountInfo, reading: int, reading_date: datetime.date = datetime.date.today()):
print(
f"Submitting gas reading of {reading} for {reading_date} for MPRN {account_info.mprn} with serial {account_info.gas_meter_serial}")
gas_reading_mutation = gql(
f"""
mutation gasReading {{
createGasMeterReading(
mprn: "{account_info.mprn}",
serialNumber: "{account_info.gas_meter_serial}",
readAt: "{reading_date}",
reading: {reading}
) {{
readingErrors{{
field
}}
}}
}}
"""
)
result = client.execute(gas_reading_mutation)
if result['createGasMeterReading'] is None:
return True
else:
return False
def submit_electricity_reading(account_info: AccountInfo, reading: int,
reading_date: datetime.date = datetime.date.today()):
print(
f"Submitting electricity reading of {reading} with register {account_info.electricity_register} for {reading_date} for MPAN {account_info.mpan} with serial {account_info.electricity_meter_serial}")
electricity_reading_mutation = gql(
f"""
mutation electricityReading {{
createElectricityMeterReading(
mpan: "{account_info.mpan}",
serialNumber: "{account_info.electricity_meter_serial}",
readAt: "{reading_date}",
readings: {{
reading: {reading},
register: "{account_info.electricity_register}"
}}
) {{
readingErrors{{
field
}}
}}
}}
"""
)
result = client.execute(electricity_reading_mutation)
if result['createElectricityMeterReading'] is None:
return True
else:
return False
if __name__ == '__main__':
try:
jwt = f"JWT {get_refresh_token(email=args.email, password=args.password)}"
except TransportQueryError:
raise ConnectionRefusedError("Unable to get token, please check creds")
transport.headers = {"Authorization": jwt}
account_number = get_account_number()
fetched_account_info = get_meter_info(account_number)
gas_success = False
electricity_success = False
if args.gas is not None:
if args.gas < fetched_account_info.expected_gas_reading_range[0] or args.gas > fetched_account_info.expected_gas_reading_range[1]:
raise ValueError("Gas reading is outside of expected range")
gas_success = submit_gas_reading(fetched_account_info, args.gas)
if args.elec is not None:
electricity_success = submit_electricity_reading(fetched_account_info, args.elec)
print(
f"""Electricity ⚡️: {"Success 😎" if electricity_success else "Failed ☹️"}
Gas 🔥: {"Success 😎" if gas_success else "Failed ☹️"}
"""
)