Add --site parameter

This commit is contained in:
Jan Myszkier
2024-03-13 09:16:16 +01:00
committed by Michał Leśniewski
parent f0a7cd9818
commit 7e5e2ede74
3 changed files with 53 additions and 12 deletions

View File

@@ -23,20 +23,39 @@ $ pip3 install git@github.com:mlesniew/elicznik.git
With the package installed readings can be retrieved by simply running the `elicznik` command:
```
usage: elicznik [-h] [--format {table,csv}] [--api {chart,csv}] username password [start_date] [end_date]
usage: elicznik [-h] [--format {table,csv}] [--api {chart,csv}] [--site SITE] username password [start_date] [end_date]
positional arguments:
username tauron-dystrybucja.pl user name
password tauron-dystrybucja.pl password
start_date Start date of date range to be retrieved, in ISO8601 format. If the end date is omitted, it's the only date for which
measurements are retrieved.
end_date End date of date range to be retrieved, inclusive, in ISO8601 format. Can be omitted to only retrieve a single day's
measurements.
start_date Start date of date range to be retrieved, in ISO8601 format. If the end date is omitted, it's the only date for which measurements are retrieved.
end_date End date of date range to be retrieved, inclusive, in ISO8601 format. Can be omitted to only retrieve a single day's measurements.
options:
-h, --help show this help message and exit
--format {table,csv} Specify the output format
--api {chart,csv} Specify which Tauron API to use to get the measurements.
--api {chart,csv} Specify which Tauron API to use to get the measurements
--site SITE site identifier, must match '[0-9]+_[0-9]+_[0-9]+'
(venv) ~/src/elicznik (site) $ elicznik --help | cat
usage: elicznik [-h] [--format {table,csv}] [--api {chart,csv}] [--site SITE]
username password [start_date] [end_date]
positional arguments:
username tauron-dystrybucja.pl user name
password tauron-dystrybucja.pl password
start_date Start date of date range to be retrieved, in ISO8601
format. If the end date is omitted, it's the only date
for which measurements are retrieved.
end_date End date of date range to be retrieved, inclusive, in
ISO8601 format. Can be omitted to only retrieve a
single day's measurements.
options:
-h, --help show this help message and exit
--format {table,csv} Specify the output format
--api {chart,csv} Specify which Tauron API to use to get the
measurements
--site SITE site identifier, must match '[0-9]+_[0-9]+_[0-9]+'
```
@@ -79,7 +98,7 @@ timestamp consumed produced net consumption net production
import datetime
import elicznik
with elicznik.ELicznik("freddy@example.com", "secretpassword") as m:
with elicznik.ELicznik("freddy@example.com", "secretpassword", "optional_site_identifier") as m:
# date range
print("July 2021")
@@ -127,7 +146,6 @@ with elicznik.ELicznikChart("freddy@example.com", "secretpassword") as m:
## TODO & bugs
* Add support for accounts with multiple meters
* Convert the dates to UTC and handle switches from and to DST properly
* Make the dependency on tabulate optional

View File

@@ -2,11 +2,20 @@ import argparse
import csv
import datetime
import sys
import re
import tabulate
from .elicznik import ELicznikChart, ELicznikCSV
SITE_ID_PATTERN = "[0-9]+_[0-9]+_[0-9]+"
def parse_site(site):
match = re.match(SITE_ID_PATTERN, site)
if not match:
raise ValueError
return match.string
def main():
parser = argparse.ArgumentParser()
@@ -20,7 +29,13 @@ def main():
"--api",
choices=["chart", "csv"],
default="csv",
help="Specify which Tauron API to use to get the measurements. ",
help="Specify which Tauron API to use to get the measurements",
)
parser.add_argument(
"--site",
type=parse_site,
default=None,
help=f"site identifier, must match '{SITE_ID_PATTERN}'",
)
parser.add_argument("username", help="tauron-dystrybucja.pl user name")
parser.add_argument("password", help="tauron-dystrybucja.pl password")
@@ -47,7 +62,7 @@ def main():
elicznik_class = ELicznikCSV if args.api == "csv" else ELicznikChart
with elicznik_class(args.username, args.password) as elicznik:
with elicznik_class(args.username, args.password, args.site) as elicznik:
result = elicznik.get_readings(args.start_date, args.end_date)
if args.format == "table":

View File

@@ -9,9 +9,10 @@ from .session import Session
class ELicznikBase:
LOGIN_URL = "https://logowanie.tauron-dystrybucja.pl/login"
def __init__(self, username, password):
def __init__(self, username, password, site=None):
self.username = username
self.password = password
self.site = site
def login(self):
self.session = Session()
@@ -24,6 +25,13 @@ class ELicznikBase:
"service": "https://elicznik.tauron-dystrybucja.pl",
},
)
if self.site is not None:
self.session.post(
"https://elicznik.tauron-dystrybucja.pl/ustaw_punkt",
data={
"site[client]": self.site
},
)
def __enter__(self):
self.login()
@@ -153,4 +161,4 @@ class ELicznikCSV(ELicznikBase):
)
ELicznik = ELicznikCSV
ELicznik = ELicznikCSV