Browse Source

Do not skip because of initial connection fail

Thomas Dietrich 8 năm trước cách đây
mục cha
commit
0a62b0c411
1 tập tin đã thay đổi với 17 bổ sung14 xóa
  1. 17 14
      miflora-mqtt-daemon.py

+ 17 - 14
miflora-mqtt-daemon.py

@@ -36,11 +36,11 @@ def print_line(text, error = False, warning=False, sd_notify=False, console=True
     timestamp = strftime('%Y-%m-%d %H:%M:%S', localtime())
     if console:
         if error:
-            print(Fore.MAGENTA + '[{}] '.format(timestamp) + Fore.RED + '{}'.format(text) + Fore.RESET, file=sys.stderr)
+            print(Fore.RED + Style.BRIGHT + '[{}] '.format(timestamp) + Style.RESET_ALL + '{}'.format(text) + Style.RESET_ALL, file=sys.stderr)
         elif warning:
-            print(Fore.MAGENTA + '[{}] '.format(timestamp) + Fore.YELLOW + '{}'.format(text) + Fore.RESET)
+            print(Fore.YELLOW + '[{}] '.format(timestamp) + Style.RESET_ALL + '{}'.format(text) + Style.RESET_ALL)
         else:
-            print(Fore.MAGENTA + '[{}] '.format(timestamp) + Fore.RESET + '{}'.format(text))
+            print(Fore.GREEN + '[{}] '.format(timestamp) + Style.RESET_ALL + '{}'.format(text) + Style.RESET_ALL)
 
     timestamp_sd = strftime('%b %d %H:%M:%S', localtime())
     if sd_notify:
@@ -64,6 +64,7 @@ def on_publish(client, userdata, mid):
 
 
 def flores_to_openhab_items(flores, reporting_mode):
+    print_line('Generating openHAB "miflora.items" file ...')
     items = list()
     items.append('// miflora.items - Generated by miflora-mqtt-daemon.')
     items.append('// Adapt to your needs! Things you probably want to modify:')
@@ -87,7 +88,7 @@ def flores_to_openhab_items(flores, reporting_mode):
         print('\n'.join(items))
     #elif reporting_mode == 'mqtt-homie':
     else:
-        print('Sorry, given reporting_mode not supported for the export to openHAB items')
+        raise IOError('Given reporting_mode not supported for the export to openHAB items')
 
 
 # Load configuration file
@@ -171,16 +172,14 @@ for [name, mac] in config['Sensors'].items():
         flora_poller.parameter_value(MI_LIGHT)
         flora['firmware'] = flora_poller.firmware_version()
     except IOError:
-        print_line('Initial connection to Mi Flora sensor "{}" ({}) failed. Sensor will not be used'.format(name_pretty, mac), error=True, sd_notify=True)
-        print()
-        continue
+        print_line('Initial connection to Mi Flora sensor "{}" ({}) failed. Please ensure a good link quality.'.format(name_pretty, mac), error=True, sd_notify=True)
     else:
         print('Internal name: "{}"'.format(name_clean))
         print('Device name:   "{}"'.format(flora_poller.name()))
         print('MAC address:   {}'.format(flora_poller._mac))
         print('Firmware:      {}'.format(flora_poller.firmware_version()))
         print_line('Initial connection to Mi Flora sensor "{}" ({}) successful'.format(name_pretty, mac), sd_notify=True)
-        print()
+    print()
     flores[name_clean] = flora
 
 # Discovery Announcement
@@ -234,26 +233,30 @@ elif reporting_mode == 'mqtt-homie':
 
 print_line('Initialization complete, starting MQTT publish loop', console=False, sd_notify=True)
 
-#flores_to_openhab_items(flores, reporting_mode)
+flores_to_openhab_items(flores, reporting_mode)
 
 # Sensor data retrieval and publication
 while True:
     for [flora_name, flora] in flores.items():
         data = dict()
-        retries = 3
+        attempts = 2
         flora['poller']._cache = None
+        flora['poller']._last_read = None
         print_line('Retrieving data from sensor "{}" ...'.format(flora['pretty']))
-        while retries > 0 and not flora['poller']._cache:
+        while attempts != 0 and not flora['poller']._cache:
             try:
                 flora['poller'].fill_cache()
                 flora['poller'].parameter_value(MI_LIGHT)
             except IOError:
-                retries = retries - 1
-                if retries > 0:
+                attempts = attempts - 1
+                if attempts > 0:
                     print_line('Retrying ...', warning = True)
+                flora['poller']._cache = None
+                flora['poller']._last_read = None
 
         if not flora['poller']._cache:
             print_line('Failed to retrieve data from Mi Flora sensor "{}" ({})'.format(flora['pretty'], flora['mac']), error = True, sd_notify = True)
+            print()
             continue
 
         for param,_ in parameters.items():
@@ -283,7 +286,7 @@ while True:
     print_line('Status messages published', console=False, sd_notify=True)
 
     if daemon_enabled:
-        print('Sleeping ({} seconds) ...'.format(sleep_period))
+        print_line('Sleeping ({} seconds) ...'.format(sleep_period))
         sleep(sleep_period)
         print()
     else: