1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)