Bladeren bron

Allow location info in config, check MAC

Thomas Dietrich 8 jaren geleden
bovenliggende
commit
8569dd498a
2 gewijzigde bestanden met toevoegingen van 15 en 5 verwijderingen
  1. 5 3
      config.ini
  2. 10 2
      miflora-mqtt-daemon.py

+ 5 - 3
config.ini

@@ -51,11 +51,13 @@
 [Sensors]
 
 # Add your Mi Flora sensors here. Each sensor consists of a name and a Ethernet MAC address.
-# One sensor per line. Scan for sensors from the command line with:
+# One sensor per line. An additional location information can be added to the name, delimited by an '@'.
+# Scan for sensors from the command line with:
 #    $ sudo hcitool lescan
 #
 # Examples:
 #
-#Schefflera     = C4:7C:8D:11:22:33
-#JapaneseBonsai = C4:7C:8D:44:55:66
+#Schefflera      = C4:7C:8D:11:22:33
+#JapaneseBonsai  = C4:7C:8D:44:55:66
+#Petunia@Balcony = C4:7C:8D:77:88:99
 

+ 10 - 2
miflora-mqtt-daemon.py

@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 
+import re
 import sys
 import json
 import os.path
@@ -87,6 +88,13 @@ sd_notifier.notify('READY=1')
 # Initialize Mi Flora sensors
 flores = dict()
 for [name, mac] in config['Sensors'].items():
+    location = ''
+    if '@' in name:
+        name, location = name.split("@")
+    if not re.match("C4:7C:8D:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}", mac):
+        print('Error. The MAC address "{}" seems to be in the wrong format. Please check your configuration.'.format(mac), file=sys.stderr)
+        sd_notifier.notify('STATUS=The MAC address "{}" seems to be in the wrong format. Please check your configuration.'.format(mac))
+        sys.exit(1)
     flora = dict()
     print('Adding sensor to device list and testing connection ...')
     print('Name:         "{}"'.format(name))
@@ -94,6 +102,8 @@ for [name, mac] in config['Sensors'].items():
     flora_poller = MiFloraPoller(mac=mac, cache_timeout=miflora_cache_timeout, retries=9)
     flora['poller'] = flora_poller
     flora['mac'] = flora_poller._mac
+    flora['refresh'] = sleep_period
+    flora['location'] = location
     try:
         flora_poller.fill_cache()
         flora_poller.parameter_value(MI_LIGHT)
@@ -107,8 +117,6 @@ for [name, mac] in config['Sensors'].items():
         print('MAC address:  {}'.format(flora_poller._mac))
         print('Firmware:     {}'.format(flora_poller.firmware_version()))
         print()
-    flora['refresh'] = sleep_period
-    flora['location'] = ''
     flores[name] = flora
 
 # Discovery Announcement