-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprovision.nix
298 lines (275 loc) · 7.18 KB
/
provision.nix
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{ pkgs }:
let
tasmota = import ./tasmota.nix;
# https://tasmota.github.io/docs/Commands
# Custom NodeMCU board with an AM-2301 temperature and humidity sensor and an
# HC-SR501 PIR motion sensor
# https://tasmota.github.io/docs/PIR-Motion-Sensors/
config-2c-3a-e8-0e-63-de = [
{
cmnd = "Wifi";
value = 3; # select explicitly the Wi-Fi mode 802.11b/g (2.4 GHz) for better connectivity
}
{
cmnd = "Template";
value = tasmota.template {
name = "MySensor";
gpio = {
GPIO14 = tasmota.component.AM2301;
};
};
}
{
cmnd = "SwitchMode1";
value = 1;
}
{
cmnd = "SwitchTopic";
value = 0;
}
{
cmnd = "Rule1";
value = "on Switch1#state=1 do publish stat/%topic%/PIR1 ON endon on Switch1#state=0 do Publish stat/%topic%/PIR1 OFF endon";
}
{
cmnd = "Rule1";
value = 0;
}
{
cmnd = "SensorRetain";
value = "on";
}
{
cmnd = "Module";
value = 0;
}
];
# Custom NodeMCU board with an AM-2301 temperature and humidity sensor on GPIO14
config-2c-3a-e8-08-23-20 = [
{
cmnd = "Template";
value = tasmota.template {
name = "MySensor";
gpio = {
GPIO14 = tasmota.component.AM2301;
};
};
}
{
cmnd = "SetOption56";
value = "1"; # Wi-Fi network scan to select strongest signal on restart.
}
{
cmnd = "SensorRetain";
value = "on";
}
{
cmnd = "Module";
value = 0;
}
];
# MagicHome LED controller
config-60-01-94-96-80-4e = [
{
cmnd = "Template";
value = tasmota.template {
name = "ZJ-ESP-IR-B-v2.3";
gpio = with tasmota.component; {
GPIO4 = IRrecv;
GPIO5 = PWM1;
GPIO12 = PWM3;
GPIO13 = PWM2;
};
};
}
{
cmnd = "Fade";
value = 1;
}
{
cmnd = "Speed";
value = 1;
}
{
cmnd = "PowerRetain";
value = "on";
}
{
cmnd = "Module";
value = 0;
}
];
# Sonoff Basic with a switch on GPIO14
config-60-01-94-68-c8-18 = [
{
cmnd = "Template";
value = tasmota.template {
name = "Sonoff Basic";
gpio = with tasmota.component; {
GPIO0 = Button1;
GPIO1 = User;
GPIO2 = User;
GPIO3 = User;
GPIO4 = User;
GPIO12 = Relay1;
GPIO13 = Led1i;
GPIO14 = Switch1;
};
base = tasmota.base.SonoffBasic;
};
}
{
cmnd = "PowerRetain";
value = "on";
}
{
cmnd = "Module";
value = 0;
}
];
# Sonoff TH16 with an Si7021 temperature and humidity sensor
config-a4-cf-12-d8-92-ea = [
{
cmnd = "Template";
value = tasmota.template {
name = "Sonoff TH";
gpio = with tasmota.component; {
GPIO0 = Button1;
GPIO1 = User;
GPIO2 = User;
GPIO3 = User;
GPIO4 = User;
GPIO12 = Relay1;
GPIO13 = Led1i;
GPIO14 = SI7021;
};
base = tasmota.base.SonoffTH;
};
}
{
cmnd = "SetOption56";
value = "1"; # Wi-Fi network scan to select strongest signal on restart.
}
{
cmnd = "SensorRetain";
value = "on";
}
{
cmnd = "Module";
value = 0;
}
];
# Sonoff TH16 with an Si7021 temperature and humidity sensor
config-a4-cf-12-d8-a2-dd = [
{
cmnd = "Template";
value = tasmota.template {
name = "Sonoff TH";
gpio = with tasmota.component; {
GPIO0 = Button1;
GPIO1 = User;
GPIO2 = User;
GPIO3 = User;
GPIO4 = User;
GPIO12 = Relay1;
GPIO13 = Led1i;
GPIO14 = SI7021;
};
base = tasmota.base.SonoffTH;
};
}
{
cmnd = "SensorRetain";
value = "on";
}
{
cmnd = "Module";
value = 0;
}
];
backlogMessage = with builtins; config:
let
mkCommand = { cmnd, value }: "${cmnd} ${toString value}";
in
assert length config <= 30;
concatStringsSep ";" (map mkCommand config);
IrRemote1 = {
# See the command list https://tasmota.github.io/docs/Commands/#light
"0x00FF906F" = "Dimmer +";
"0x00FFB847" = "Dimmer -";
"0x00FFF807" = "Power OFF";
"0x00FFB04F" = "Power ON";
# White
"0x00FFA857" = "Color ffffff"; # W
# Red shades
"0x00FF9867" = "Color ff0000"; # R
"0x00FFE817" = "Color ff5500";
"0x00FF02FD" = "Color ff8000";
"0x00FF50AF" = "Color ffd500";
"0x00FF38C7" = "Color ffff00";
};
IrRemote2 = {
# Green shades
"0x00FFD827" = "Color 00ff00"; # G
"0x00FF48B7" = "Color 00ff55";
"0x00FF32CD" = "Color 00ff80";
"0x00FF7887" = "Color 00ffd5";
"0x00FF28D7" = "Color 00d5ff";
};
IrRemote3 = {
# Blue shades
"0x00FF8877" = "Color 0000ff"; # B
"0x00FF6897" = "Color 3333ff";
"0x00FF20DF" = "Color 5500ff";
"0x00FF708F" = "Color aa00ff";
"0x00FFF00F" = "Color ff00d4";
# Schemes
"0x00FFB24D" = ""; # FLASH
"0x00FF00FF" = ""; # STROBE
"0x00FF58A7" = "Scheme 2"; # FADE
"0x00FF30CF" = "Scheme 4"; # SMOOTH
};
ruleMessage = with builtins; keyMap:
let
lengthLimit = 511; # https://tasmota.github.io/docs/Rules/#rule-syntax
mkRule = code: command: "ON IrReceived#Data=${code} DO ${command} ENDON";
rules = attrValues (mapAttrs mkRule keyMap);
message = concatStringsSep " " rules;
in
assert stringLength message <= lengthLimit; message;
send = device: command: value:
"${pkgs.mosquitto}/bin/mosquitto_pub --host nuc --topic cmnd/${device}/${command} --message '${toString value}'";
tasmota_0E63DE = pkgs.writeScript "tasmota_0E63DE" ''
${send "tasmota_0E63DE" "Backlog" (backlogMessage config-2c-3a-e8-0e-63-de)}
'';
tasmota_082320 = pkgs.writeScript "tasmota_082320" ''
${send "tasmota_082320" "Backlog" (backlogMessage config-2c-3a-e8-08-23-20)}
'';
tasmota_68C818 = pkgs.writeScript "tasmota_68C818" ''
${send "tasmota_68C818" "Backlog" (backlogMessage config-60-01-94-68-c8-18)}
'';
tasmota_D892EA = pkgs.writeScript "tasmota_D892EA" ''
${send "tasmota_D892EA" "Backlog" (backlogMessage config-a4-cf-12-d8-92-ea)}
'';
tasmota_D8A2DD = pkgs.writeScript "tasmota_D8A2DD" ''
${send "tasmota_D8A2DD" "Backlog" (backlogMessage config-a4-cf-12-d8-a2-dd)}
'';
tasmota_96804E = pkgs.writeScript "tasmota_96804E" ''
${send "tasmota_96804E" "Rule1" (ruleMessage IrRemote1)}
${send "tasmota_96804E" "Rule1" 1}
${send "tasmota_96804E" "Rule2" (ruleMessage IrRemote2)}
${send "tasmota_96804E" "Rule2" 1}
${send "tasmota_96804E" "Rule3" (ruleMessage IrRemote3)}
${send "tasmota_96804E" "Rule3" 1}
${send "tasmota_96804E" "Backlog" (backlogMessage config-60-01-94-96-80-4e)}
'';
in
pkgs.runCommand "provisioning_scripts" { } ''
mkdir $out
cp ${tasmota_0E63DE} $out/tasmota_0E63DE.sh
cp ${tasmota_082320} $out/tasmota_082320.sh
cp ${tasmota_68C818} $out/tasmota_68C818.sh
cp ${tasmota_D892EA} $out/tasmota_D892EA.sh
cp ${tasmota_D8A2DD} $out/tasmota_D8A2DD.sh
cp ${tasmota_96804E} $out/tasmota_96804E.sh
''