add weight from MI_SCALE_WEIGHT_SAMPLE
This commit is contained in:
parent
958d33f366
commit
bcf905b33b
34
main.py
34
main.py
@ -128,6 +128,17 @@ class GadgetbridgeMQTTPublisher:
|
||||
"device_class": "battery",
|
||||
}
|
||||
|
||||
# Weight sensor
|
||||
weight_config = {
|
||||
"name": f"{self.device_name.replace('_', ' ').title()} Weight",
|
||||
"unique_id": f"{self.device_name}_weight",
|
||||
"state_topic": f"gadgetbridge/{self.device_name}/weight",
|
||||
"unit_of_measurement": "kg",
|
||||
"icon": "mdi:scale-bathroom",
|
||||
"device": device_info,
|
||||
"state_class": "measurement",
|
||||
}
|
||||
|
||||
await self.publish_home_assistant_discovery(
|
||||
"sensor", "daily_steps", steps_config
|
||||
)
|
||||
@ -143,6 +154,7 @@ class GadgetbridgeMQTTPublisher:
|
||||
await self.publish_home_assistant_discovery(
|
||||
"sensor", "battery_level", battery_config
|
||||
)
|
||||
await self.publish_home_assistant_discovery("sensor", "weight", weight_config)
|
||||
|
||||
def get_steps_data(self) -> Dict:
|
||||
"""Extract steps data from Gadgetbridge database"""
|
||||
@ -243,6 +255,7 @@ class GadgetbridgeMQTTPublisher:
|
||||
"monthly": f"gadgetbridge/{self.device_name}/steps/monthly",
|
||||
"last_sync": f"gadgetbridge/{self.device_name}/last_sync",
|
||||
"battery": f"gadgetbridge/{self.device_name}/battery",
|
||||
"weight": f"gadgetbridge/{self.device_name}/weight",
|
||||
}
|
||||
try:
|
||||
await self.mqtt_client.publish(
|
||||
@ -264,6 +277,12 @@ class GadgetbridgeMQTTPublisher:
|
||||
await self.mqtt_client.publish(
|
||||
topics["battery"], str(battery_level), qos=1
|
||||
)
|
||||
# Publish latest weight
|
||||
latest_weight = self.get_latest_weight()
|
||||
if latest_weight is not None:
|
||||
await self.mqtt_client.publish(
|
||||
topics["weight"], str(latest_weight), qos=1
|
||||
)
|
||||
self.logger.info(
|
||||
f"Published steps data: Daily={data['daily_steps']}, Weekly={data['weekly_steps']}, Monthly={data['monthly_steps']}, Battery={battery_level}"
|
||||
)
|
||||
@ -315,6 +334,21 @@ class GadgetbridgeMQTTPublisher:
|
||||
self.logger.error(f"Error querying battery level: {e}")
|
||||
return None
|
||||
|
||||
def get_latest_weight(self) -> Optional[float]:
|
||||
"""Get the latest weight in kg from MI_SCALE_WEIGHT_SAMPLE table"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT WEIGHT_KG FROM MI_SCALE_WEIGHT_SAMPLE 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 latest weight: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def get_all_device_names(db_path):
|
||||
"""Returns a list of all unique device names from the database."""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user