introduced config.yaml

This commit is contained in:
2026-08-30 12:25:57 +02:00
parent bb80bb953c
commit 20fa7cb67d
10 changed files with 191 additions and 123 deletions
+13 -27
View File
@@ -4,7 +4,6 @@
from __future__ import annotations
import argparse
import os
import secrets
import sys
import urllib.parse
@@ -14,6 +13,7 @@ from pathlib import Path
# Allow direct execution from a source checkout without installing the package.
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from netatmo_service.config import load_config
from netatmo_service.netatmo import NetatmoClient, NetatmoError
AUTHORIZE_URL = "https://api.netatmo.com/oauth2/authorize"
@@ -21,16 +21,7 @@ AUTHORIZE_URL = "https://api.netatmo.com/oauth2/authorize"
def arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--client-id", default=os.getenv("NETATMO_CLIENT_ID"))
parser.add_argument("--client-secret", default=os.getenv("NETATMO_CLIENT_SECRET"))
parser.add_argument(
"--redirect-uri", default=os.getenv("NETATMO_REDIRECT_URI", "http://localhost:8080")
)
parser.add_argument(
"--token-file",
type=Path,
default=Path(os.getenv("NETATMO_TOKEN_FILE", "./rrd/netatmo_tokens.json")),
)
parser.add_argument("--config", type=Path, default=Path("config.yaml"))
parser.add_argument("--no-browser", action="store_true")
return parser.parse_args()
@@ -51,15 +42,20 @@ def authorization_code(value: str, expected_state: str) -> str:
def main() -> int:
args = arguments()
if not args.client_id or not args.client_secret:
print("Set NETATMO_CLIENT_ID and NETATMO_CLIENT_SECRET first.", file=sys.stderr)
try:
config = load_config(args.config)
except RuntimeError as exc:
print(exc, file=sys.stderr)
return 2
if not config["NETATMO_CLIENT_ID"] or not config["NETATMO_CLIENT_SECRET"]:
print("Set netatmo.client_id and netatmo.client_secret in config.yaml first.", file=sys.stderr)
return 2
state = secrets.token_urlsafe(24)
url = f"{AUTHORIZE_URL}?" + urllib.parse.urlencode(
{
"client_id": args.client_id,
"redirect_uri": args.redirect_uri,
"client_id": config["NETATMO_CLIENT_ID"],
"redirect_uri": config["NETATMO_REDIRECT_URI"],
"scope": "read_station",
"state": state,
"response_type": "code",
@@ -73,23 +69,13 @@ def main() -> int:
try:
code = authorization_code(input("> "), state)
config = {
"NETATMO_CLIENT_ID": args.client_id,
"NETATMO_CLIENT_SECRET": args.client_secret,
"NETATMO_REFRESH_TOKEN": "",
"NETATMO_ACCESS_TOKEN": "",
"NETATMO_TOKEN_FILE": args.token_file.expanduser().resolve(),
"NETATMO_DEVICE_ID": "",
"NETATMO_TOKEN_URL": "https://api.netatmo.com/oauth2/token",
"NETATMO_STATIONS_URL": "https://api.netatmo.com/api/getstationsdata",
}
client = NetatmoClient(config)
client.exchange_code(code, args.redirect_uri)
client.exchange_code(code, config["NETATMO_REDIRECT_URI"])
except (EOFError, ValueError, NetatmoError) as exc:
print(f"Token setup failed: {exc}", file=sys.stderr)
return 1
print(f"Tokens saved to {args.token_file} with owner-only permissions.")
print(f"Tokens saved to {config['NETATMO_TOKEN_FILE']} with owner-only permissions.")
return 0