-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
executable file
·45 lines (32 loc) · 1.39 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
"""Get Xiaomi Mi Smart Scale 2 weight and publishing to mqtt.
Tested only on raspberry pi 3b and mi scale 2
with <3 by @qbbr
"""
import argparse
import logging
import os
from dotenv import dotenv_values
from logger import log, basicConfig
from mqttpublisher import MqttPublisher
from scanner import start
def main():
config = dotenv_values(os.path.dirname(__file__) + "/.env")
parser = argparse.ArgumentParser(description="Get Xiaomi Mi Smart Scale 2 weight and publishing to mqtt.",
epilog="with <3 by @qbbr")
parser.add_argument("--loglevel", dest="logLevel", choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
help="set the logging level")
args = parser.parse_args()
if args.logLevel:
basicConfig(level=getattr(logging, args.logLevel))
def callback(weight, unit):
log.info("received data = %s %s", weight, unit)
if weight < float(config.get("MIN_WEIGHT")) or weight > float(config.get("MAX_WEIGHT")):
log.warning("weight is not between %s and %s, skip publishing", config.get("MIN_WEIGHT"),
config.get("MAX_WEIGHT"))
return
publisher = MqttPublisher(config)
publisher.publish(weight)
start(config.get("MAC_ADDRESS"), float(config.get("TIMEOUT")), callback)
if __name__ == "__main__":
main()