#!/usr/bin/env python3

from glowmarkt import *
import datetime
import sys
import argparse

try:

    parser = argparse.ArgumentParser(
        description=__doc__
    )
    parser.add_argument('--username', '-u',
                        required=True,
                        help="Bright account username")
    parser.add_argument('--password', '-p',
                        required=True,
                        help="Bright account password")
    parser.add_argument('--classifier', '-c',
                        default="electricity.consumption",
                        help="Resource classifier to use (default: electricity.consumption)")
    parser.add_argument('--add-standing-charge', '-S',
                        action="store_const",
                        const=True,
                        help="Add tariff standing charge to value.")

    # Parse arguments
    args = parser.parse_args(sys.argv[1:])

    cli = BrightClient(args.username, args.password)
    ents = cli.get_virtual_entities()

    rows = []

    for ent in ents:

        for res in ent.get_resources():

            if res.classifier != args.classifier:
                continue

            # Work out period from start of day to now.
            now = datetime.datetime.now().astimezone()
            t_to = res.round(now, "PT30M")
            t_from = t_to.replace(second=0, microsecond=0, minute=0, hour=0)

            rdgs = res.get_readings(t_from, t_to, "PT30M")

            tot = 0
            for r in rdgs:
                tot += r[1].value

            if args.add_standing_charge:
                tar = res.get_tariff()
                tot += tar.current_rates.standing_charge.value

            print(tot)
            
except Exception as e:
    print(e)

