From 5d6668fda42ed9e8ed051e00fb0b64504aa23971 Mon Sep 17 00:00:00 2001 From: jwansek Date: Sat, 7 Oct 2023 00:11:46 +0100 Subject: Made the database hhost be in the config file --- .dockerignore | 1 + database.py | 7 +++++-- devices.py | 15 +++++++++++---- docker-compose.yml | 9 --------- requirements.txt | 2 ++ 5 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f827b66 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +db.env diff --git a/database.py b/database.py index 23e06f0..846da4d 100644 --- a/database.py +++ b/database.py @@ -4,7 +4,7 @@ import os @dataclass class PowerDatabase: - host: str = "db" + host: str = None user: str = "root" passwd: str = None db: str = "power" @@ -13,6 +13,9 @@ class PowerDatabase: def __enter__(self): if self.passwd is None: self.passwd = os.environ["MYSQL_ROOT_PASSWORD"] + + if self.host is None: + self.host = os.environ["MYSQL_HOST"] try: self.__connection = self.__get_connection() @@ -112,7 +115,7 @@ if __name__ == "__main__": dotenv.load_dotenv(dotenv_path = "db.env") host = "srv.athome" else: - host = "db" + host = None with PowerDatabase(host = host) as db: print(db.get_tasmota_devices()) diff --git a/devices.py b/devices.py index 27e6545..650ee87 100644 --- a/devices.py +++ b/devices.py @@ -11,7 +11,7 @@ if not os.path.exists(os.path.join("/app", ".docker")): dotenv.load_dotenv(dotenv_path = "db.env") HOST = "srv.athome" else: - HOST = "db" + HOST = None async def get_energy_for(host, username = None, password = None): device = await tasmotadevicecontroller.TasmotaDevice().connect(host, username, password) @@ -39,15 +39,22 @@ def poll_watt_all(): asyncio.set_event_loop(loop) with database.PowerDatabase(host = HOST) as db: for host, username, password in db.get_tasmota_devices(): - asyncio.run(poll_watt_for(db, host, username, password)) + try: + asyncio.run(poll_watt_for(db, host, username, password)) + except: + print("Retrying %s..." % host) + asyncio.run(poll_watt_for(db, host, username, password)) def poll_kwh_all(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) with database.PowerDatabase(host = HOST) as db: for host, username, password in db.get_tasmota_devices(): - asyncio.run(poll_yesterday_kwh_for(db, host, username, password)) - + try: + asyncio.run(poll_yesterday_kwh_for(db, host, username, password)) + except: + print("Retrying %s..." % host) + asyncio.run(poll_yesterday_kwh_for(db, host, username, password)) if __name__ == "__main__": if sys.argv[1] == "daily": diff --git a/docker-compose.yml b/docker-compose.yml index a752a0f..a387601 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,12 +8,3 @@ services: image: jwansek/power env_file: - ./db.env - external_links: - - mariadb:db - networks: - - db-network - -networks: - db-network: - external: - name: mariadb \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index ec38b64..d15029f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ tasmotadevicecontroller==0.0.8 aiohttp==3.8.3 pymysql +python-dotenv + -- cgit v1.2.3