diff options
author | Dennis Eriksen <d@ennis.no> | 2023-11-09 13:57:26 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-11-09 13:57:26 +0100 |
commit | 4ad7f2e5111ca9e27dcbe5781c87f249bc7377ad (patch) | |
tree | d605620535229e741f7e2da473086fe185f78e5f | |
parent | small fixes (diff) | |
download | energyscripts-4ad7f2e5111ca9e27dcbe5781c87f249bc7377ad.tar.gz |
added new function to get env
which handles missing env better...
-rw-r--r-- | scripts/common/__init__.py | 17 | ||||
-rw-r--r-- | scripts/common/postgres.py | 11 | ||||
-rw-r--r-- | scripts/common/queue.py | 7 | ||||
-rw-r--r-- | scripts/elvia.py | 3 | ||||
-rw-r--r-- | scripts/elvia_gridtariff.py | 5 | ||||
-rw-r--r-- | scripts/entsoe.py | 3 | ||||
-rw-r--r-- | scripts/esphomeapi.py | 3 | ||||
-rw-r--r-- | scripts/mqtt_listener.py | 13 | ||||
-rw-r--r-- | scripts/mqtt_watch.py | 14 | ||||
-rw-r--r-- | scripts/nb.py | 5 | ||||
-rw-r--r-- | scripts/neohub.py | 5 | ||||
-rw-r--r-- | scripts/queue_runner.py | 10 |
12 files changed, 52 insertions, 44 deletions
diff --git a/scripts/common/__init__.py b/scripts/common/__init__.py index e349610..e3ea442 100644 --- a/scripts/common/__init__.py +++ b/scripts/common/__init__.py @@ -6,7 +6,22 @@ import os import sys from datetime import datetime -QUEUE = bool(os.environ.get("el_QUEUE", False)) + +# Get env var +def env(var_name, default=None): + value = os.getenv(var_name) + if value is not None: + return value + + if default is not None: + return default + + print(f"Error: Could not find environment variable '{var_name}'") + print("That variable is required for running this program.") + sys.exit(1) + + +QUEUE = bool(env("el_QUEUE", False)) name = os.path.splitext(os.path.split(sys.argv[0])[-1])[0] diff --git a/scripts/common/postgres.py b/scripts/common/postgres.py index 5f6ea31..5692535 100644 --- a/scripts/common/postgres.py +++ b/scripts/common/postgres.py @@ -1,15 +1,16 @@ #!/usr/bin/env python3 """ common functions and stuff """ -import os import sys import psycopg -pg_db = os.environ["el_pg_db"] -pg_host = os.environ["el_pg_host"] -pg_user = os.environ.get("el_pg_user", "") -pg_pass = os.environ.get("el_pg_pass", "") +from . import env + +pg_db = env("el_pg_db") +pg_host = env("el_pg_host") +pg_user = env("el_pg_user", "") +pg_pass = env("el_pg_pass", "") def dbi(sql, values, **kwargs): diff --git a/scripts/common/queue.py b/scripts/common/queue.py index de49802..f67acbb 100644 --- a/scripts/common/queue.py +++ b/scripts/common/queue.py @@ -2,13 +2,14 @@ """ common functions and stuff """ import logging -import os import pickle from litequeue import SQLQueue -QUEUE_DB = os.environ.get("el_QUEUE_db", "litequeue.db") -QUEUE_DIR = os.environ.get("el_QUEUE_dir", "queue") +from . import env + +QUEUE_DB = env("el_QUEUE_db", "litequeue.db") +QUEUE_DIR = env("el_QUEUE_dir", "queue") QUEUE_DB = QUEUE_DIR + "/" + QUEUE_DB diff --git a/scripts/elvia.py b/scripts/elvia.py index fea0f61..fffb6bf 100644 --- a/scripts/elvia.py +++ b/scripts/elvia.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 """ Get energy consumption from Elvia """ -import os import sys from datetime import datetime, timedelta @@ -9,7 +8,7 @@ import common import requests from tzlocal import get_localzone -apiKey = os.environ["el_elvia_token"] +apiKey = common.env("el_elvia_token") apiUrl = "https://elvia.azure-api.net/customer/metervalues/api/v1/metervalues" startTime = datetime.now(get_localzone()) - timedelta(days=2) diff --git a/scripts/elvia_gridtariff.py b/scripts/elvia_gridtariff.py index 98164bb..4475c48 100644 --- a/scripts/elvia_gridtariff.py +++ b/scripts/elvia_gridtariff.py @@ -1,7 +1,6 @@ #!/usr/bin/python3 """ get grid tariffs""" -import os import sys from datetime import datetime, timedelta @@ -11,9 +10,9 @@ from tzlocal import get_localzone # API documentation: https://elvia.portal.azure-api.net/docs/services/gridtariffapi/operations/post-digin-api-v-tariffquery-meteringpointsgridtariffs? -apiKey = os.environ["el_elvia_grid_api_key"] +apiKey = common.env("el_elvia_grid_api_key") apiUrl = "https://elvia.azure-api.net/grid-tariff/digin/api/1/tariffquery/meteringpointsgridtariffs" -meteringPointId = os.environ["el_meteringPointId"] +meteringPointId = common.env("el_meteringPointId") startTime = datetime.now(get_localzone()) - timedelta(days=2) startTime = startTime.strftime("%Y-%m-%d") diff --git a/scripts/entsoe.py b/scripts/entsoe.py index fc34274..2c825c2 100644 --- a/scripts/entsoe.py +++ b/scripts/entsoe.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 """ Get energyprices from Entsoe """ -import os import sys from datetime import datetime, timedelta @@ -22,7 +21,7 @@ from tzlocal import get_localzone # > respond to your request. # 1: https://transparency.entsoe.eu/content/static_content/Static%20content/web%20api/Guide.html#_authentication_and_authorisation # 2: https://transparency.entsoe.eu/content/static_content/download?path=/Static%20content/API-Token-Management.pdf -apiKey = os.environ["el_entsoe_token"] +apiKey = common.env("el_entsoe_token") # https://transparency.entsoe.eu/content/static_content/Static%20content/web%20api/Guide.html apiUrl = "https://web-api.tp.entsoe.eu/api?securityToken=" + apiKey diff --git a/scripts/esphomeapi.py b/scripts/esphomeapi.py index 390fbd5..40594f1 100644 --- a/scripts/esphomeapi.py +++ b/scripts/esphomeapi.py @@ -4,7 +4,6 @@ import argparse import asyncio import logging -import os import sys import common @@ -14,7 +13,7 @@ import zeroconf from aioesphomeapi import APIClient, APIConnectionError, ReconnectLogic, SensorState sleepsec = 60 -noise_psk = os.environ["el_esphome_api_psk"] +noise_psk = common.env("el_esphome_api_psk") async def main(args): diff --git a/scripts/mqtt_listener.py b/scripts/mqtt_listener.py index 3ee688d..1ff3176 100644 --- a/scripts/mqtt_listener.py +++ b/scripts/mqtt_listener.py @@ -2,18 +2,17 @@ """ Listen for mqtt-events, and trigger for some """ import json -import os from datetime import datetime import common import paho.mqtt.client as mqtt -mqtt_server = os.environ["el_mqtt_server"] -mqtt_port = int(os.environ["el_mqtt_port"]) -keepalive = int(os.environ["el_mqtt_keepalive"]) -mqtt_topic = os.environ["el_mqtt_topic"] -mqtt_user = os.environ["el_mqtt_user"] -mqtt_pass = os.environ["el_mqtt_pass"] +mqtt_server = common.env("el_mqtt_server") +mqtt_port = int(common.env("el_mqtt_port")) +keepalive = int(common.env("el_mqtt_keepalive")) +mqtt_topic = common.env("el_mqtt_topic") +mqtt_user = common.env("el_mqtt_user") +mqtt_pass = common.env("el_mqtt_pass") tempsensors = [ "Bad Temp", diff --git a/scripts/mqtt_watch.py b/scripts/mqtt_watch.py index e2c4e21..22105c6 100644 --- a/scripts/mqtt_watch.py +++ b/scripts/mqtt_watch.py @@ -1,17 +1,17 @@ #!/usr/bin/env python3 """ Listen for mqtt-events, and trigger for some """ -import os from datetime import datetime +import common import paho.mqtt.client as mqtt -mqtt_server = os.environ["el_mqtt_server"] -mqtt_port = int(os.environ["el_mqtt_port"]) -keepalive = int(os.environ["el_mqtt_keepalive"]) -mqtt_topic = os.environ["el_mqtt_topic"] -mqtt_user = os.environ["el_mqtt_user"] -mqtt_pass = os.environ["el_mqtt_pass"] +mqtt_server = common.env("el_mqtt_server") +mqtt_port = int(common.env("el_mqtt_port")) +keepalive = int(common.env("el_mqtt_keepalive")) +mqtt_topic = common.env("el_mqtt_topic") +mqtt_user = common.env("el_mqtt_user") +mqtt_pass = common.env("el_mqtt_pass") # The callback for when the client receives a CONNACK response from the server. diff --git a/scripts/nb.py b/scripts/nb.py index d00b0d2..6afb74e 100644 --- a/scripts/nb.py +++ b/scripts/nb.py @@ -2,7 +2,6 @@ """ Get exchange rates from nb """ import csv -import os import sys import tempfile from datetime import datetime, timedelta @@ -14,8 +13,8 @@ from tzlocal import get_localzone # I'm not sure I understand Norges Banks json-model. It seems a lot easier to just get the CSV, and convert it to JSON. apiUrl = "https://data.norges-bank.no/api/data/EXR/B.EUR.NOK.SP?format=csv&locale=en" -pg_db = os.environ["el_pg_db"] -pg_host = os.environ["el_pg_host"] +pg_db = common.env("el_pg_db") +pg_host = common.env("el_pg_host") pg_table = "nbex" startTime = datetime.now(get_localzone()) - timedelta(days=10) diff --git a/scripts/neohub.py b/scripts/neohub.py index bf43859..f4c4f2b 100644 --- a/scripts/neohub.py +++ b/scripts/neohub.py @@ -2,15 +2,14 @@ """ Get stuff from neohub! This is mostly the usage-example from https://gitlab.com/neohubapi/neohubapi/ """ import asyncio -import os import time from datetime import datetime import common import neohubapi.neohub as neohub -neohub_ip = os.environ["el_neohub_ip"] -neohub_port = os.environ["el_neohub_port"] +neohub_ip = common.env("el_neohub_ip") +neohub_port = common.env("el_neohub_port") SLEEP = 120 # Sleep between runs diff --git a/scripts/queue_runner.py b/scripts/queue_runner.py index fd88600..0fdb80d 100644 --- a/scripts/queue_runner.py +++ b/scripts/queue_runner.py @@ -1,20 +1,18 @@ #!/usr/bin/env python3 """ move items from queue to database """ -import os import pickle import sqlite3 import time +import common from common.postgres import dbi from litequeue import SQLQueue -QUEUE_DB = os.environ.get("el_QUEUE_db", "litequeue.db") -QUEUE_DIR = os.environ.get("el_QUEUE_dir", "queue") +QUEUE_DB = common.env("el_QUEUE_db", "litequeue.db") +QUEUE_DIR = common.env("el_QUEUE_dir", "queue") QUEUE_DB = QUEUE_DIR + "/" + QUEUE_DB -QUEUE_SLEEP = int( - os.environ.get("el_QUEUE_sleep", 15) -) # Default sleep 15 seconds when queue empty +QUEUE_SLEEP = int(common.env("el_QUEUE_sleep", 15)) # Default sleep 15 seconds when queue empty # Unlock all con = sqlite3.connect(QUEUE_DB) |