forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P029_DomoOutput.py
130 lines (120 loc) · 4.55 KB
/
_P029_DomoOutput.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
#!/usr/bin/env python3
#############################################################################
################# (Domoticz) Output helper for RPIEasy ######################
#############################################################################
#
# Only receiver! Communication is implemented through plugin_receivedata()
# Primarily for Domoticz, but can be used any MQTT based controllers.
#
# Copyright (C) 2018-2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import gpios
import lib.lib_gpiohelper as gpiohelper
import Settings
class Plugin(plugin.PluginProto):
PLUGIN_ID = 29
PLUGIN_NAME = "Output - Output Helper"
PLUGIN_VALUENAME1 = "State"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SINGLE
self.vtype = rpieGlobals.SENSOR_TYPE_SWITCH
self.valuecount = 1
self.senddataoption = True
self.recdataoption = True
self.pullupoption = False
self.inverselogicoption = True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0]=0
self.sync()
def webform_load(self):
webserver.addFormNote("Please make sure to select <a href='pinout'>pin configured for Output!</a>")
webserver.addFormCheckBox("Preserve state at startup","p029_preserve",self.taskdevicepluginconfig[0])
webserver.addFormCheckBox("Response to remote commands for non-Domoticz controllers","p029_report",self.taskdevicepluginconfig[1])
return True
def webform_save(self,params):
if (webserver.arg("p029_preserve",params)=="on"):
self.taskdevicepluginconfig[0] = True
else:
self.taskdevicepluginconfig[0] = False
if (webserver.arg("p029_report",params)=="on"):
self.taskdevicepluginconfig[1] = True
else:
self.taskdevicepluginconfig[1] = False
self.sync()
return True
def sync(self):
if self.enabled:
if self.taskdevicepin[0]>=0:
v1 = gpios.HWPorts.input(self.taskdevicepin[0])
if self.pininversed:
v1 = (1 - v1)
if self.taskdevicepluginconfig[0]==True:
ot = False
for p in range(len(Settings.Pinout)):
if str(Settings.Pinout[p]["BCM"])==str(self.taskdevicepin[0]):
if Settings.Pinout[p]["startupstate"] in [4,5,6]:
ot = True
break
if ot==False:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Pin is not an Output, State preserving disabled")
self.taskdevicepluginconfig[0] = False
if v1 != self.uservar[0]:
if self.taskdevicepluginconfig[0]==True:
self.set_value(1,int(self.uservar[0]),True) # restore previous state from uservar
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,self.taskname+": Restoring previous GPIO value "+str(self.uservar[0]))
else:
self.uservar[0] = v1 # store actual pin state into uservar
if self.taskdevicepluginconfig[1]:
self.plugin_senddata()
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,self.taskname+": Syncing actual GPIO value "+str(v1))
elif self.taskdevicepluginconfig[1]:
self.plugin_senddata()
self.initialized = True
if self.initialized:
if self.taskdevicepluginconfig[0]==True:
sps = "en"
else:
sps = "dis"
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"State preserving is "+sps+"abled")
def set_value(self,valuenum,value,publish=True,suserssi=-1,susebattery=-1): # Also reacting and handling Taskvalueset
if self.initialized:
if self.taskdevicepin[0]>=0:
try:
if 'on' in str(value).lower() or str(value)=="1":
val = 1
value = 1
else:
val = 0
value = 0
if self.pininversed:
val = (1 - val)
value = (1 - value)
except:
val = 0
try:
gpios.HWPorts.output(self.taskdevicepin[0],val) # try to set gpio according to requested status
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Please set up GPIO type before use "+str(e))
plugin.PluginProto.set_value(self,valuenum,value,publish,suserssi,susebattery)
def plugin_receivedata(self,data): # set value based on mqtt input
if (len(data)>0) and self.initialized and self.enabled:
if 'on' in str(data[0]).lower() or str(data[0])=="1":
val = 1
else:
val = 0
self.set_value(1,val,self.taskdevicepluginconfig[1])
# print("Data received:",data) # DEBUG
def plugin_write(self,cmd):
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0].strip().lower() in gpiohelper.commandlist:
res = gpiohelper.gpio_commands(cmd)
return res