mqtt discovery support

This commit is contained in:
lolouk44 2020-07-01 20:22:34 +01:00
parent b700f506cf
commit c5b64862b7
2 changed files with 29 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "Xiaomi Mi Scale", "name": "Xiaomi Mi Scale",
"version": "0.1.3", "version": "0.1.5b",
"slug": "xiaomi_mi_scale", "slug": "xiaomi_mi_scale",
"description": "Read weight measurements from Xiamomi scale via BLE", "description": "Read weight measurements from Xiamomi scale via BLE",
"url": "https://github.com/lolouk44/xiaomi_mi_scale_ha_add_on", "url": "https://github.com/lolouk44/xiaomi_mi_scale_ha_add_on",
@ -20,6 +20,8 @@
"MQTT_PASSWORD": "", "MQTT_PASSWORD": "",
"MQTT_PORT": 1883, "MQTT_PORT": 1883,
"TIME_INTERVAL": 30, "TIME_INTERVAL": 30,
"MQTT_DISCOVERY": true,
"MQTT_DISCOVERY_PREFIX": "homeassistant",
"USER1_GT": 70, "USER1_GT": 70,
"USER1_SEX": "male", "USER1_SEX": "male",
@ -48,6 +50,8 @@
"MQTT_PASSWORD": "str", "MQTT_PASSWORD": "str",
"MQTT_PORT": "int", "MQTT_PORT": "int",
"TIME_INTERVAL": "int", "TIME_INTERVAL": "int",
"MQTT_DISCOVERY": "bool",
"MQTT_DISCOVERY_PREFIX": "str",
"USER1_GT": "int", "USER1_GT": "int",
"USER1_SEX": "str", "USER1_SEX": "str",

View File

@ -29,20 +29,15 @@ try:
sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Loading Config From Add-On Options...\n") sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Loading Config From Add-On Options...\n")
data = json.load(json_file) data = json.load(json_file)
MISCALE_MAC = data["MISCALE_MAC"] MISCALE_MAC = data["MISCALE_MAC"]
if(data["MQTT_USERNAME"] == ""): MQTT_USERNAME = None if(data["MQTT_USERNAME"] == "") else data["MQTT_USERNAME"]
MQTT_USERNAME = None MQTT_PASSWORD = None if(data["MQTT_PASSWORD"] == "") else data["MQTT_PASSWORD"]
else:
MQTT_USERNAME = data["MQTT_USERNAME"]
if(data["MQTT_PASSWORD"] == ""):
MQTT_PASSWORD = None
else:
MQTT_PASSWORD = data["MQTT_PASSWORD"]
MQTT_HOST = data["MQTT_HOST"] MQTT_HOST = data["MQTT_HOST"]
MQTT_PORT = int(data["MQTT_PORT"]) MQTT_PORT = int(data["MQTT_PORT"])
MQTT_PREFIX = data["MQTT_PREFIX"] MQTT_PREFIX = data["MQTT_PREFIX"]
TIME_INTERVAL = int(data["TIME_INTERVAL"]) TIME_INTERVAL = int(data["TIME_INTERVAL"])
MQTT_DISCOVERY = data["MQTT_DISCOVERY"]
MQTT_DISCOVERY_PREFIX = data["MQTT_DISCOVERY_PREFIX"]
HCI_DEV = data["HCI_DEV"][-1] HCI_DEV = data["HCI_DEV"][-1]
OLD_MEASURE = ''
# User Variables... # User Variables...
USER1_GT = int(data["USER1_GT"]) USER1_GT = int(data["USER1_GT"])
@ -74,8 +69,9 @@ except FileNotFoundError:
MQTT_PORT = int(os.getenv('MQTT_PORT', 1883)) MQTT_PORT = int(os.getenv('MQTT_PORT', 1883))
MQTT_PREFIX = os.getenv('MQTT_PREFIX', 'miscale') MQTT_PREFIX = os.getenv('MQTT_PREFIX', 'miscale')
TIME_INTERVAL = int(os.getenv('TIME_INTERVAL', 30)) TIME_INTERVAL = int(os.getenv('TIME_INTERVAL', 30))
MQTT_DISCOVERY = os.getenv('MQTT_DISCOVERY',True)
MQTT_DISCOVERY_PREFIX = os.getenv('MQTT_DISCOVERY_PREFIX','homeassistant')
HCI_DEV = os.getenv('HCI_DEV', 'hci0')[-1] HCI_DEV = os.getenv('HCI_DEV', 'hci0')[-1]
OLD_MEASURE = ''
# User Variables... # User Variables...
USER1_GT = int(os.getenv('USER1_GT', '70')) # If the weight is greater than this number, we'll assume that we're weighing User #1 USER1_GT = int(os.getenv('USER1_GT', '70')) # If the weight is greater than this number, we'll assume that we're weighing User #1
@ -96,6 +92,22 @@ except FileNotFoundError:
USER3_DOB = os.getenv('USER3_DOB', '1988-01-01') # DOB (in yyyy-mm-dd format) USER3_DOB = os.getenv('USER3_DOB', '1988-01-01') # DOB (in yyyy-mm-dd format)
sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Config Loaded...\n") sys.stdout.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Config Loaded...\n")
OLD_MEASURE = ''
def discovery():
for MQTTUser in (USER1_NAME,USER2_NAME,USER3_NAME):
message = '{"name": "' + MQTTUser + ' Weight",'
message+= '"state_topic": "miScale/' + MQTTUser + '/weight","value_template": "{{ value_json.Weight }}","unit_of_measurement": "kg",'
message+= '"json_attributes_topic": "miScale/' + MQTTUser + '/weight","icon": "mdi:scale-bathroom"}'
publish.single(
MQTT_DISCOVERY_PREFIX + '/sensor/' + MQTT_PREFIX + '/' + MQTTUser + '/config',
message,
retain=False,
hostname=MQTT_HOST,
port=MQTT_PORT,
auth={'username':MQTT_USERNAME, 'password':MQTT_PASSWORD}
)
class ScanProcessor(): class ScanProcessor():
def GetAge(self, d1): def GetAge(self, d1):
@ -197,6 +209,8 @@ class ScanProcessor():
raise raise
def main(): def main():
if MQTT_DISCOVERY:
discovery()
BluetoothFailCounter = 0 BluetoothFailCounter = 0
while True: while True:
try: try: