linky.py 1.5 KB
Newer Older
Marmotte's avatar
Marmotte committed
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
#! /usr/bin/env python3

import pytz

from datetime import datetime, timedelta
from influxdb import InfluxDBClient
from pylinky import LinkyClient

local_tz = pytz.timezone('Europe/Paris')
heures_creuses = [
    (12, 30),
    (13, 0),
    (13, 30),
    (14, 0),
    (14, 30),
    (15, 0),
    (2, 0),
    (2, 30),
    (3, 0),
    (3, 30),
    (4, 0),
    (4, 30),
    (5, 0),
    (5, 30),
    (6, 0),
    (6, 30),
]
start = datetime.now() - timedelta(days=3)
end = datetime.now()
datetime_format = '%Y-%m-%dT%H:%M:%SZ'
username = 'username'
password = 'password'

linky_client = LinkyClient(username=username, password=password)
linky_client.login()

linky_raw_data = linky_client.get_data_per_period(start=start, end=end)
linky_formatted_data = linky_client.format_data(
    linky_raw_data, time_format=datetime_format)

influxdb_client = InfluxDBClient(database='house')
for data in linky_formatted_data:
    data_time = pytz.utc.localize(datetime.strptime(
        data['time'], datetime_format,
    ))
    local_data_time = local_tz.normalize(data_time)
    local_time_tuple = (local_data_time.hour, local_data_time.minute)

    influxdb_client.write_points([{
        'measurement': 'enedis',
        'tags': {
            'address': 'address',
            'price': (
                'Heures creuses'
                if local_time_tuple in heures_creuses
                else 'Heures pleines'
            )
        },
        'time': data_time,
        'fields': {
            'conso': float(data['conso']),
        },
    }])