ctucx.git: smartied

smarthome server

commit c2ad1433709b263f30c266a35a6d78240b6f8a95
parent 9ab3f5e24ec70be3171746ff827dab95eeef2940
Author: Leah (ctucx) <leah@ctu.cx>
Date: Fri, 14 May 2021 18:57:55 +0200

implement new device: zigbee2mqttMotionSensor
3 files changed, 37 insertions(+), 2 deletions(-)
A
src/devices/zigbee2mqttMotionSensor.nim
|
30
++++++++++++++++++++++++++++++
M
src/smartied.nim
|
3
++-
M
src/types.nim
|
6
+++++-
diff --git a/src/devices/zigbee2mqttMotionSensor.nim b/src/devices/zigbee2mqttMotionSensor.nim
@@ -0,0 +1,29 @@
+import asyncdispatch, strutils, json, tables, options
+import ../types, ../vars, ../deviceActionHandler
+import nmqtt
+
+proc handleMotionSensor (topic: string, message: string) =
+  try:
+    let deviceName  = zigbee2mqttDevices[topic]
+    let config      = server.config.devices[deviceName]
+    let recivedData = parseJson(message)
+
+    if recivedData.hasKey("occupancy"):
+      if recivedData["occupancy"].getBool == true:
+        for action in config.occupyActions:
+          discard waitFor handleDeviceAction(action)
+
+      if recivedData["occupancy"].getBool == false:
+        for action in config.clearActions:
+          discard waitFor handleDeviceAction(action)
+
+  except:
+    echo "Error[handleMotionSensor]:\n", getCurrentExceptionMsg()
+
+proc initZigbee2MqttMotionSensors* () {.async.} =
+  for key, device in server.config.devices.pairs():
+    if device.type != Zigbee2MqttMotionSensor: continue
+
+    zigbee2mqttDevices["zigbee2mqtt/" & device.deviceName.get] = key
+
+    await mqttContext.subscribe("zigbee2mqtt/" & device.deviceName.get, 2, handleMotionSensor)+
\ No newline at end of file
diff --git a/src/smartied.nim b/src/smartied.nim
@@ -1,7 +1,7 @@
 import asyncdispatch, json, tables, os, posix
 import types, modbus, mqtt, influx, vars, utils, options
 import frontend
-import devices/[modbusPowermeter, modbusRelayboard, lacrosseSensors, zigbee2mqttLamp, zigbee2mqttRelay, zigbee2mqttRemote]
+import devices/[modbusPowermeter, modbusRelayboard, lacrosseSensors, zigbee2mqttLamp, zigbee2mqttRelay, zigbee2mqttRemote, zigbee2mqttMotionSensor]
 
 proc CtrlCHook() {.noconv.} =
   echo "Ctrl+C fired! \nStopping Server now!"

@@ -54,6 +54,7 @@ proc main() {.async.} =
     asyncCheck initZigbee2MqttLamps()
     asyncCheck initZigbee2MqttRelays()
     asyncCheck initZigbee2MqttRemotes()
+    asyncCheck initZigbee2MqttMotionSensors()
 
   asyncCheck serveFrontend()
 
diff --git a/src/types.nim b/src/types.nim
@@ -34,7 +34,8 @@ type DeviceType* = enum
   LacrosseTempSensor,
   Zigbee2MqttLamp,
   Zigbee2MqttRelay,
-  Zigbee2MqttRemote
+  Zigbee2MqttRemote,
+  Zigbee2MqttMotionSensor
 
 type Zigbee2MqttLampType* = enum
   RGB,

@@ -85,6 +86,9 @@ type DeviceConfig* = object
     lampType*: Zigbee2MqttLampType
   of Zigbee2MqttRemote:
     actions*: Table[string, seq[Action]]
+  of Zigbee2MqttMotionSensor:
+    occupyActions*: seq[Action]
+    clearActions*: seq[Action]
   else:
     unusedValue: Option[bool]