forked from empierre/domoticz_linky-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinky_month.py
executable file
·176 lines (134 loc) · 5.95 KB
/
linky_month.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Generates energy consumption JSON files from Enedis (ERDF) consumption data
collected via their website (API).
"""
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import datetime
import logging
import sys
import json
import linky
from dateutil.relativedelta import relativedelta
USERNAME = os.environ['LINKY_USERNAME']
PASSWORD = os.environ['LINKY_PASSWORD']
BASEDIR = os.environ['BASE_DIR']
def generate_y_axis(res):
"""Generate y axis (consumption values)."""
y_values = []
# Extract data points from the source dictionary into a list
for ordre, datapoint in enumerate(res['graphe']['data']):
value = datapoint['valeur']
# Remove any invalid values
# (they're error codes on the API side, but useless here)
if value < 0:
value = 0
y_values.insert(ordre, value)
return y_values
def generate_x_axis(res, time_delta_unit, time_format, inc):
"""Generate x axis (time values)."""
x_values = []
# Extract start date and parse it
start_date_queried_str = res['graphe']['periode']['dateDebut']
start_date_queried = datetime.datetime.strptime(start_date_queried_str, "%d/%m/%Y").date()
# Calculate final start date using the "offset" attribute returned by the API
kwargs = {}
kwargs[time_delta_unit] = res['graphe']['decalage'] * inc
start_date = start_date_queried - relativedelta(**kwargs)
# Generate X axis time labels for every data point
for ordre, _ in enumerate(res['graphe']['data']):
kwargs = {}
kwargs[time_delta_unit] = ordre * inc
x_values.insert(ordre, (start_date + relativedelta(**kwargs)).strftime(time_format))
return x_values
def dtostr(date):
"""Date formatting"""
return date.strftime("%d/%m/%Y")
def export_hours_values(res):
"""Export the JSON file for half-hours power measure (for the last pas day)."""
hours_x_values = generate_x_axis(res,
'hours', "%H:%M", 0.5)
hours_y_values = generate_y_axis(res)
hours_values = []
for i in range(0, len(hours_x_values)):
hours_values.append({"time": hours_x_values[i], "conso": hours_y_values[i]})
with open(BASEDIR+"/export_hours_values.json", 'w+') as outfile:
json.dump(hours_values, outfile)
def export_days_values(res):
"""Export the JSON file for daily consumption (for the past rolling 30 days)."""
days_x_values = generate_x_axis(res,
'days', "%d %b %Y", 1)
days_y_values = generate_y_axis(res)
days_values = []
for i in range(0, len(days_x_values)):
days_values.append({"time": days_x_values[i], "conso": days_y_values[i]})
with open(BASEDIR+"/export_days_values.json", 'w+') as outfile:
json.dump(days_values, outfile)
def export_months_values(res):
"""
Export the JSON file for monthly consumption.
For the current year, starting 12 months from today.
"""
months_x_values = generate_x_axis(res,
'months', "%b", 1)
months_y_values = generate_y_axis(res)
months_values = []
for i in range(0, len(months_x_values)):
months_values.append({"time": months_x_values[i], "conso": months_y_values[i]})
with open(BASEDIR+"/export_months_values.json", 'w+') as outfile:
json.dump(months_values, outfile)
def export_years_values(res):
"""Export the JSON file for yearly consumption."""
years_x_values = generate_x_axis(res,
'years', "%Y", 1)
years_y_values = generate_y_axis(res)
years_values = []
for i in range(0, len(years_x_values)):
years_values.append({"time": years_x_values[i], "conso": years_y_values[i]})
with open(BASEDIR+"/export_years_values.json", 'w+') as outfile:
json.dump(years_values, outfile)
# Main script
def main():
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
try:
logging.info("logging in as %s...", USERNAME)
token = linky.login(USERNAME, PASSWORD)
logging.info("logged in successfully!")
logging.info("retreiving data...")
today = datetime.date.today()
logging.info("arg "+sys.argv[1])
mmonth = int(sys.argv[1])
mmonthnext = int(sys.argv[1])+1
logging.info(dtostr(today - relativedelta(days=1, months=mmonth)))
logging.info(dtostr(today - relativedelta(days=1, months=mmonthnext)))
# 12 months ago - today
res_month = linky.get_data_per_month(token, dtostr(today - relativedelta(months=11)),
dtostr(today))
# One month ago - yesterday
res_day = linky.get_data_per_day(token, dtostr(today - relativedelta(days=1, months=mmonthnext)),
dtostr(today - relativedelta(days=1, months=mmonth)))
logging.info("got data!")
############################################
# Export of the JSON files, with exception handling as Enedis website is not robust and return empty data often
try:
export_days_values(res_day)
except Exception:
logging.info("days values non exported")
############################################
except linky.LinkyLoginException as exc:
logging.error(exc)
sys.exit(1)
if __name__ == "__main__":
main()