-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.py
193 lines (142 loc) · 6.04 KB
/
plugin.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#
# SNMPreader Plugin
#
# Author: ycahome, 2017
# https://www.domoticz.com/forum
#
#
# version 1.0.0 : Initial
# 1.1.0 : Removed unneeded logging on debug mode
# : Ficed to work with python 3
# : Added interval attribute
# : Minor Ther fixes
#
#
##
"""
<plugin key="SNMPreader" name="SNMP Value Reader" author="ycahome" version="1.1.0" wikilink="m" externallink="https://www.domoticz.com/forum/viewtopic.php?f=65">
<params>
<param field="Address" label="Server IP" width="200px" required="true" default="192.168.1.1"/>
<param field="Mode1" label="OID" width="200px" required="true" default="1.3.6.1.2.1.25.3.2.1.1.2"/>
<param field="Mode2" label="Community" width="200px" required="true" default="public"/>
<param field="Mode4" label="Check Interval(seconds)" width="75px" required="true" default="60"/>
<param field="Mode3" label="Domoticz TypeName" width="200px">
<options>
<option label="Custom" value="Custom"/>
<option label="Text" value="Text"/>
<option label="Temperature" value="Temperature" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import sys
sys.path.append('/usr/lib/python3/dist-packages/')
from pysnmp.entity.rfc3413.oneliner import cmdgen
import json
import urllib.request
import urllib.error
from math import radians, cos, sin, asin, sqrt
from datetime import datetime, timedelta
#############################################################################
# Domoticz call back functions #
#############################################################################
def onStart():
global gdeviceSuffix
global gdeviceTypeName
gdeviceSuffix = "(SNMP)"
interval = int(Parameters["Mode4"])
createDevices()
if Parameters["Mode6"] == "Debug":
DumpConfigToDebug()
ServerIP = str(Parameters["Address"])
snmpOID = str(Parameters["Mode1"])
snmpCommunity = Parameters["Mode2"]
snmpDataValue = str(getSNMPvalue(ServerIP,snmpOID,snmpCommunity))
Domoticz.Heartbeat(interval)
return True
def onHeartbeat():
ServerIP = str(Parameters["Address"])
snmpOID = str(Parameters["Mode1"])
snmpCommunity = Parameters["Mode2"]
# Get new information and update the devices
snmpDataValue = str(getSNMPvalue(ServerIP,snmpOID,snmpCommunity))
UpdateDevice(1,0,snmpDataValue)
Domoticz.Log("SNMP Value (" + ServerIP + "/" + snmpCommunity + "/"+ snmpOID + ") retrieved:"+snmpDataValue)
return True
#############################################################################
# Domoticz helper functions #
#############################################################################
def DumpConfigToDebug():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Log("'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Log("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Log("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Log("Device Name: '" + Devices[x].Name + "'")
Domoticz.Log("Device nValue: " + str(Devices[x].nValue))
Domoticz.Log("Device sValue: '" + Devices[x].sValue + "'")
def UpdateDevice(Unit, nValue, sValue):
# Make sure that the Domoticz device still exists before updating it.
# It can be deleted or never created!
if (Unit in Devices):
Devices[Unit].Update(nValue, str(sValue))
Domoticz.Debug("Update " + str(nValue) + ":'" + str(sValue) + "' (" + Devices[Unit].Name + ")")
#############################################################################
# Device specific functions #
#############################################################################
def createDevices():
# Are there any devices?
if len(Devices) != 0:
# Could be the user deleted some devices, so do nothing
Domoticz.Debug("Devices Already Exist.")
return
# Give the devices a unique unit number. This makes updating them more easy.
# UpdateDevice() checks if the device exists before trying to update it.
# Add Power and temperature device(s)
Domoticz.Device(Name=gdeviceSuffix, Unit=1, TypeName=Parameters["Mode3"]).Create()
Domoticz.Log("Devices created.")
def getSNMPvalue(ServerIP,snmpOID,snmpCommunity):
cmdGen = cmdgen.CommandGenerator()
#genData = cmdgen.CommunityData('public')
genData = cmdgen.CommunityData(str(snmpCommunity))
Domoticz.Debug("genData Loaded." + str(genData))
TTData = cmdgen.UdpTransportTarget((str(ServerIP), 161), retries=2)
Domoticz.Debug("TTData Loaded." + str(TTData))
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(genData,TTData,snmpOID)
Domoticz.Debug("DATA Loaded." + str(varBinds))
# Check for errors and print out results
if errorIndication:
Domoticz.Error(str(errorIndication))
else:
if errorStatus:
Domoticz.Error('%s at %s' % (errorStatus.prettyPrint(),errorIndex and varBinds[int(errorIndex)-1] or '?'))
else:
for name, val in varBinds:
Domoticz.Debug('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
return val.prettyPrint()
#
# Parse an int and return None if no int is given
#
def parseIntValue(s):
try:
return int(s)
except:
return None
#
# Parse a float and return None if no float is given
#
def parseFloatValue(s):
try:
return float(s)
except:
return None