ソースを参照

Zigbee heating valve follow check improved, battery check added

master
Dirk Alders 1年前
コミット
33042e3f3c
4個のファイルの変更54行の追加20行の削除
  1. 1
    0
      check_z_heat_vlv_battery
  2. 10
    3
      check_z_heat_vlv_follow
  3. 39
    15
      z_server/devices/__init__.py
  4. 4
    2
      z_server/z_protocol.py

+ 1
- 0
check_z_heat_vlv_battery ファイルの表示

@@ -0,0 +1 @@
1
+check_z_heat_vlv_follow

check_z_heat_vlv → check_z_heat_vlv_follow ファイルの表示

@@ -7,7 +7,7 @@ import time
7 7
 from z_server import config
8 8
 from z_server import tcp_socket
9 9
 from z_server.z_protocol import server as client_prot
10
-from z_server.z_protocol import DID_FOLLOWS_HEATING_SETPOINT
10
+from z_server.z_protocol import DID_FOLLOWS_HEATING_SETPOINT, DID_BATTERY_LEVEL
11 11
 from z_server import socket_protocol
12 12
 import sys
13 13
 
@@ -30,7 +30,14 @@ if __name__ == '__main__':
30 30
         "roo": args.roo,
31 31
         "fun": "FUN_HEA"    # <-- Const, because script is for heat_vlv only
32 32
     }
33
-    sp.send(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_HEATING_SETPOINT, data)
34 33
     #
35
-    sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_FOLLOWS_HEATING_SETPOINT).get_data()
34
+    if sys.argv[0].endswith('check_z_heat_vlv_follow'):
35
+        sp.send(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_HEATING_SETPOINT, data)
36
+        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_FOLLOWS_HEATING_SETPOINT).get_data()
37
+    elif sys.argv[0].endswith('check_z_heat_vlv_battery'):
38
+        sp.send(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, data)
39
+        sp_data = sp.receive(socket_protocol.SID_READ_RESPONSE, DID_BATTERY_LEVEL).get_data()
40
+    else:
41
+        sys.stderr.write('No action for command called "%s"\n' % sys.argv[0])
42
+        sys.exit(100)
36 43
     nagios.Nagios().exit(**sp_data)

+ 39
- 15
z_server/devices/__init__.py ファイルの表示

@@ -14,6 +14,10 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
14 14
 class base(object):
15 15
     FOLLOW_REQUEST_WARNING = 5      # Seconds, till warning comes up, if device does not follow the command
16 16
     FOLLOW_REQUEST_ERROR = 60       # Seconds, till error comes up, if device does not follow the command
17
+    FOLLOW_KEYS = ["current_heating_setpoint", ]
18
+    #
19
+    BATTERY_LVL_WARNING = 10
20
+    BATTERY_LVL_ERROR = 5
17 21
 
18 22
     def __init__(self, mqtt_client: mqtt.mqtt_client, topic):
19 23
         self.topic = topic
@@ -23,6 +27,8 @@ class base(object):
23 27
         #
24 28
         self.__target_storage__ = {}
25 29
         self.__state_storage__ = {}
30
+        #
31
+        self.battery = None
26 32
 
27 33
     def __rx__(self, client, userdata, message):
28 34
         pass
@@ -38,21 +44,37 @@ class base(object):
38 44
         logger.debug("Device state identified: %s: %s", key, repr(value))
39 45
 
40 46
     def status(self, key):
41
-        tm_s, value_s = self.__state_storage__.get(key, (0, None))
42
-        try:
43
-            tm_t, value_t = self.__target_storage__[key]
44
-        except KeyError:
45
-            if value_s is not None:
46
-                return {"status": nagios.Nagios.WARNING, "msg": "Current temperature setpoint %.1f°C (age=%.1fs), but never received a setpoint" % (value_s, time.time()-tm_s)}
47
-            return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exists, but no data received or unknown monitoring"}
48
-        else:
49
-            tm = time.time()
50
-            dt = tm - tm_t
51
-            if value_t != value_s and dt > self.FOLLOW_REQUEST_ERROR:
52
-                return {"status": nagios.Nagios.ERROR, "msg": "Requested setpoint unequal valve setpoint since %.1fs" % dt}
53
-            elif value_t != value_s and dt > self.FOLLOW_REQUEST_WARNING:
54
-                return {"status": nagios.Nagios.WARNING, "msg": "Requested setpoint unequal valve setpoint since %.1fs" % dt}
55
-            return {"status": nagios.Nagios.OK, "msg": "Requested setpoint equal valve setpoint"}
47
+        #
48
+        # FOLLOW SETPOINT
49
+        #
50
+        if key in self.FOLLOW_KEYS:
51
+            tm_s, value_s = self.__state_storage__.get(key, (0, None))
52
+            try:
53
+                tm_t, value_t = self.__target_storage__[key]
54
+            except KeyError:
55
+                if value_s is not None:
56
+                    return {"status": nagios.Nagios.WARNING, "msg": "Current temperature setpoint %.1f°C (age=%.1fs), but never received a setpoint" % (value_s, time.time()-tm_s)}
57
+                return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exists, but no data received or unknown monitoring"}
58
+            else:
59
+                tm = time.time()
60
+                dt = tm - tm_t
61
+                if value_t != value_s and dt > self.FOLLOW_REQUEST_ERROR:
62
+                    return {"status": nagios.Nagios.ERROR, "msg": "Requested setpoint unequal valve setpoint since %.1f°C (age=%.1fs)" % (value_s, time.time()-tm_s)}
63
+                elif value_t != value_s and dt > self.FOLLOW_REQUEST_WARNING:
64
+                    return {"status": nagios.Nagios.WARNING, "msg": "Requested setpoint unequal valve setpoint since %.1f°C (age=%.1fs)" % (value_s, time.time()-tm_s)}
65
+                return {"status": nagios.Nagios.OK, "msg": "Requested setpoint equal valve setpoint %.1f°C (age=%.1fs)" % (value_s, time.time()-tm_s)}
66
+        #
67
+        # BATTERY
68
+        #
69
+        elif key == "battery":
70
+            if self.battery is None:
71
+                return {"status": nagios.Nagios.UNKNOWN, "msg": "Device exists, but no data received or unknown monitoring"}
72
+            elif self.battery < self.BATTERY_LVL_ERROR:
73
+                return {"status": nagios.Nagios.ERROR, "msg": "Battery level critical low (%.1f%%)" % self.battery}
74
+            elif self.battery < self.BATTERY_LVL_WARNING:
75
+                return {"status": nagios.Nagios.WARNING, "msg": "Battery level low (%.1f%%)" % self.battery}
76
+            else:
77
+                return {"status": nagios.Nagios.OK, "msg": "Battery okay (%.1f%%)" % self.battery}
56 78
 
57 79
 
58 80
 class group(object):
@@ -95,6 +117,8 @@ class brennenstuhl_heatingvalve(base):
95 117
                 self.target("current_heating_setpoint", payload["current_heating_setpoint"])
96 118
             if message.topic == self.topic:
97 119
                 self.state("current_heating_setpoint", payload["current_heating_setpoint"])
120
+        if "battery" in payload and message.topic == self.topic:
121
+            self.battery = payload["battery"]
98 122
 
99 123
 
100 124
 class silvercrest_powerplug(base):

+ 4
- 2
z_server/z_protocol.py ファイルの表示

@@ -3,6 +3,7 @@ import socket_protocol
3 3
 # from devdi.topic import topic_by_props
4 4
 
5 5
 DID_FOLLOWS_HEATING_SETPOINT = 'current_heating_setpoint'
6
+DID_BATTERY_LEVEL = 'battery'
6 7
 
7 8
 
8 9
 class server(socket_protocol.pure_json_protocol):
@@ -15,9 +16,10 @@ class server(socket_protocol.pure_json_protocol):
15 16
         #              DID_FOLLOWS_HEATING_SETPOINT, 'current_heating_setpoint')
16 17
         #
17 18
         if not self.__comm_inst__.IS_CLIENT:
18
-            self.register_callback(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_HEATING_SETPOINT, self.follow_heating_setpoint)
19
+            self.register_callback(socket_protocol.SID_READ_REQUEST, DID_FOLLOWS_HEATING_SETPOINT, self.device_status)
20
+            self.register_callback(socket_protocol.SID_READ_REQUEST, DID_BATTERY_LEVEL, self.device_status)
19 21
 
20
-    def follow_heating_setpoint(self, msg):
22
+    def device_status(self, msg):
21 23
         if msg.get_status() == socket_protocol.STATUS_OKAY:
22 24
             try:
23 25
                 dev = self.__devices__.get_str(**msg.get_data())

読み込み中…
キャンセル
保存