-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
176 lines (144 loc) · 5.88 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
"""
# Domoticz Chacon 433 MHz Python Plugin
## Installation
```bash
cd ~
git clone https://github.com/gaelj/Chacon433Plugin.git ~/domoticz/plugins/Chacon433Plugin
git submodule init
git submodule update
chmod +x ~/domoticz/plugins/Chacon433Plugin/plugin.py
cd ~/domoticz/plugins/Chacon433Plugin
ln -Tsf DomoticzWrapper/DomoticzWrapperClass.py DomoticzWrapperClass.py
ln -Tsf DomoticzWrapper/DomoticzPluginHelper.py DomoticzPluginHelper.py
sudo systemctl restart domoticz.service
```
For more details, see [Using Python Plugins](https://www.domoticz.com/wiki/Using_Python_plugins)
"""
"""
<plugin
key="Chacon433"
name="Chacon433"
author="gaelj"
version="1.0.0"
wikilink="https://github.com/gaelj/Chacon433Plugin//blob/master/README.md"
externallink="https://github.com/gaelj/Chacon433Plugin">
<description>
<h2>Domoticz Chacon 433 interface</h2><br/>
</description>
<params>
<param field="Address" label="Domoticz IP Address" width="200px" required="true" default="127.0.0.1" />
<param field="Port" label="Port" width="40px" required="true" default="8080" />
<param field="Username" label="Username" width="200px" required="false" default="" />
<param field="Password" label="Password" width="200px" required="false" default="" />
<param field="Mode1" label="DIOShutterCode" width="200px" required="true" />
<param field="Mode2" label="Shutter indexes (csv)" width="200px" required="true" default="0"/>
<param field="Mode3" label="Logging Level" width="200px">
<options>
<option label="Normal" value="Normal" default="true"/>
<option label="Verbose" value="Verbose"/>
<option label="Debug - Python Only" value="2"/>
<option label="Debug - Basic" value="62"/>
<option label="Debug - Basic+Messages" value="126"/>
<option label="Debug - Connections Only" value="16"/>
<option label="Debug - Connections+Queue" value="144"/>
<option label="Debug - All" value="-1"/>
</options>
</param>
</params>
</plugin>
"""
import Domoticz
from datetime import datetime, timedelta
import time
from enum import IntEnum
import subprocess
z = None
pluginDevices = None
class PluginConfig:
"""Plugin configuration (singleton)"""
def __init__(self):
self.fldChacon = r'/home/pi/433Utils/RPi_utils/'
self.cmdChacon = 'codesend'
self.ChaconCode = '0FF'
self.DIOShutterCode = z.Parameters.Mode1 # '11111111'
self.fldDio = r'/home/pi/hcc/'
self.cmdDio = 'radioEmission'
class PluginDevices:
def __init__(self, shutterIds):
self.config = PluginConfig()
self.shutters = dict([(int(i + 1), ShutterActuator(i, x))
for i, x in enumerate(shutterIds)])
class ShutterActuator:
"""Shutter actuator"""
def __init__(self, pluginDeviceUnit, shutterNumber):
global z
global pluginDevices
self.state = None
self.shutterNumber = shutterNumber
self.pluginDeviceUnit = int(pluginDeviceUnit)
self.config = PluginConfig()
def SetValue(self, state: bool):
global z
global pluginDevices
state = not state
self.state = state
stateString = 'on' if self.state else 'off'
z.WriteLog(self.config.fldDio + self.config.cmdDio + ' 0 ' +
self.config.DIOShutterCode + ' ' + str(self.shutterNumber) + ' ' + stateString)
z.WriteLog("Unit: " + str(self.pluginDeviceUnit))
subprocess.call(self.config.fldDio + self.config.cmdDio + ' 0 ' + self.config.DIOShutterCode +
' ' + str(self.shutterNumber) + ' ' + stateString, shell=True)
nValue = 0 if state else 1
sValue = ""
z.Devices[self.pluginDeviceUnit + 1].Update(nValue=nValue, sValue=sValue)
self.value = state
def Read(self) -> bool:
global z
global pluginDevices
d = z.Devices[self.pluginDeviceUnit + 1]
self.value = int(d.sValue) if d.sValue is not None and d.sValue != "" else int(
d.nValue) if d.nValue is not None else None
return self.value
def onStart():
global z
global pluginDevices
from DomoticzPluginHelper import \
DomoticzPluginHelper, DeviceParam, ParseCSV, DomoticzDeviceTypes
z = DomoticzPluginHelper(
Domoticz, Settings, Parameters, Devices, Images, {})
z.onStart(3)
LightSwitch_Switch_Blinds = DomoticzDeviceTypes.LightSwitch_Switch_Blinds()
shutterIds = [x.strip()
for x in z.Parameters.Mode2.split(',') if len(x.strip()) > 0]
deviceUnit = 1
for shutterId in shutterIds:
z.InitDevice('Shutter Control ' + str(deviceUnit), deviceUnit,
DeviceType=LightSwitch_Switch_Blinds,
Used=True,
defaultNValue=0,
defaultSValue="0")
deviceUnit = deviceUnit + 1
pluginDevices = PluginDevices(shutterIds)
for unit in z.InitializedDeviceUnits:
z.WriteLog("Initialized unit: " + str(unit))
def onStop():
global z
global pluginDevices
z.onStop()
def onCommand(Unit, Command, Level, Color):
global z
global pluginDevices
z.onCommand(Unit, Command, Level, Color)
if Command == "On":
value = 1
elif Command == "Off":
value = 0
else:
value = Level
z.WriteLog("Received cmd " + str(Command) + " with level: " +
str(Level) + " for unit " + str(Unit))
pluginDevices.shutters[(int(Unit))].SetValue(value)
def onHeartbeat():
global z
global pluginDevices
z.onHeartbeat()