From dcd66bf9fced0f540259208660680a5339393ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Gro=C3=9Fklo=C3=9F?= Date: Thu, 17 Jul 2025 12:43:34 +0200 Subject: [PATCH] change to git deploy & add battery sensor --- compose.yaml | 9 +++++++-- main.py | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/compose.yaml b/compose.yaml index a1bc3e4..1b437f9 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,8 +6,7 @@ services: network_mode: host working_dir: /app volumes: - - /mnt/Data/Apps/GadgetbridgeMqtt/App:/app # Create Dataset with App Preset in TrueNAS Scale - - /mnt/Data/Apps/GadgetbridgeMqtt/Logs:/app/logs # Create Dataset with App Preset in TrueNAS Scale + - /mnt/Data/Apps/GadgetbridgeMqtt/Logs:/app/logs # Optional? - /mnt/Data/Apps/*****/Gadgetbridge/Gadgetbridge.db:/data/Gadgetbridge.db:ro environment: - TZ=Europe/Berlin # Get from e.g. https://webbrowsertools.com/timezone/ -> Timezone info Table -> Timezone @@ -19,8 +18,14 @@ services: - DEVICE_NAME="unknown" - PYTHONUNBUFFERED=1 - PUBLISH_INTERVAL_SECONDS=300 + - DAY_END_TIME=5 # 5 AM command: > sh -c " + apt-get update && + apt-get install -y git && + git clone https://git.olli.info/Oliver/GadgetbridgeMqtt.git /tmp/repo && + cp /tmp/repo/main.py /app/ && + cp /tmp/repo/healthcheck.py /app/ && pip install --no-cache-dir aiomqtt && python main.py " diff --git a/main.py b/main.py index d8422ad..628e352 100644 --- a/main.py +++ b/main.py @@ -117,6 +117,17 @@ class GadgetbridgeMQTTPublisher: "device_class": "timestamp", } + # Battery level sensor + battery_config = { + "name": f"{self.device_name.replace('_', ' ').title()} Battery Level", + "unique_id": f"{self.device_name}_battery_level", + "state_topic": f"gadgetbridge/{self.device_name}/battery", + "unit_of_measurement": "%", + "icon": "mdi:battery", + "device": device_info, + "device_class": "battery", + } + await self.publish_home_assistant_discovery( "sensor", "daily_steps", steps_config ) @@ -129,6 +140,9 @@ class GadgetbridgeMQTTPublisher: await self.publish_home_assistant_discovery( "sensor", "last_sync", last_sync_config ) + await self.publish_home_assistant_discovery( + "sensor", "battery_level", battery_config + ) def get_steps_data(self) -> Dict: """Extract steps data from Gadgetbridge database""" @@ -228,6 +242,7 @@ class GadgetbridgeMQTTPublisher: "weekly": f"gadgetbridge/{self.device_name}/steps/weekly", "monthly": f"gadgetbridge/{self.device_name}/steps/monthly", "last_sync": f"gadgetbridge/{self.device_name}/last_sync", + "battery": f"gadgetbridge/{self.device_name}/battery", } try: await self.mqtt_client.publish( @@ -243,11 +258,17 @@ class GadgetbridgeMQTTPublisher: await self.mqtt_client.publish( topics["last_sync"], data["last_sync"], qos=1 ) + # Publish battery level + battery_level = self.get_battery_level() + if battery_level is not None: + await self.mqtt_client.publish( + topics["battery"], str(battery_level), qos=1 + ) self.logger.info( - f"Published steps data: Daily={data['daily_steps']}, Weekly={data['weekly_steps']}, Monthly={data['monthly_steps']}" + f"Published steps data: Daily={data['daily_steps']}, Weekly={data['weekly_steps']}, Monthly={data['monthly_steps']}, Battery={battery_level}" ) except Exception as e: - self.logger.error(f"Failed to publish steps data: {e}") + self.logger.error(f"Failed to publish steps/battery data: {e}") async def run(self): """Main execution method (async)""" @@ -272,6 +293,21 @@ class GadgetbridgeMQTTPublisher: except Exception as e: self.logger.error(f"Failed to connect to MQTT broker: {e}") + def get_battery_level(self) -> Optional[int]: + """Get the latest battery level from BATTERY_LEVEL table""" + try: + conn = sqlite3.connect(self.db_path) + cursor = conn.cursor() + cursor.execute( + "SELECT LEVEL FROM BATTERY_LEVEL ORDER BY TIMESTAMP DESC LIMIT 1" + ) + row = cursor.fetchone() + conn.close() + return row[0] if row else None + except Exception as e: + self.logger.error(f"Error querying battery level: {e}") + return None + def get_all_device_names(db_path): """Returns a list of all unique device names from the database."""