From 331a90ca327898d58c02715639f9f2f30e22639e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20Gro=C3=9Fklo=C3=9F?= Date: Thu, 17 Jul 2025 13:26:32 +0200 Subject: [PATCH] remove last sync & fix wakeup time --- main.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index e205759..dc2bd43 100644 --- a/main.py +++ b/main.py @@ -53,14 +53,6 @@ class GadgetbridgeMQTTPublisher: "state_class": "total", "query": self.query_monthly_steps, }, - { - "name": "Last Sync", - "unique_id": "last_sync", - "state_topic": f"gadgetbridge/{self.device_name}/last_sync", - "icon": "mdi:sync", - "device_class": "timestamp", - "query": self.query_last_sync, - }, { "name": "Battery Level", "unique_id": "battery_level", @@ -244,13 +236,6 @@ class GadgetbridgeMQTTPublisher: ) return cursor.fetchone()[0] or 0 - def query_last_sync(self, cursor) -> Any: - cursor.execute("SELECT MAX(TIMESTAMP) FROM HEART_PULSE_SAMPLE") - last_sync_ts = cursor.fetchone()[0] - return ( - datetime.fromtimestamp(last_sync_ts).isoformat() if last_sync_ts else None - ) - def query_battery_level(self, cursor) -> Any: cursor.execute( "SELECT LEVEL FROM BATTERY_LEVEL ORDER BY TIMESTAMP DESC LIMIT 1" @@ -305,7 +290,13 @@ class GadgetbridgeMQTTPublisher: "SELECT WAKEUP_TIME FROM XIAOMI_SLEEP_TIME_SAMPLE ORDER BY TIMESTAMP DESC LIMIT 1" ) row = cursor.fetchone() - return datetime.fromtimestamp(row[0]).isoformat() if row and row[0] else None + if row and row[0]: + ts = row[0] + # If timestamp is in milliseconds, convert to seconds + if ts > 1e12: + ts = ts // 1000 + return datetime.fromtimestamp(ts).isoformat() + return None def query_is_awake(self, cursor) -> Any: cursor.execute(