-
Notifications
You must be signed in to change notification settings - Fork 1
/
99_myUtils_MiLight.pm
406 lines (372 loc) · 15.6 KB
/
99_myUtils_MiLight.pm
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
##############################################
# $Id: myUtils_MiLight.pm 2020-11-04 Beta-User $
#
package main;
use strict;
use warnings;
sub
myUtils_MiLight_Initialize
{
my $hash = shift;
}
# Enter you functions below _this_ line.
sub milight_toggle_indirect {
my $name = shift // return;
my $Target_Devices = AttrVal($name,"Target_Device","devStrich0");
my $dimmLevel;
my $hash;
for my $setdevice (split (/,/,$Target_Devices)) {
$hash = $defs{$setdevice};
if(lc(ReadingsVal($setdevice,"state","off")) eq "off") {
CommandSet(undef, "$setdevice on");
readingsSingleUpdate($hash,"myLastShort","1", 0);
AnalyzeCommandChain(undef, "sleep 1; set $setdevice brightness 220");
} elsif (ReadingsAge($setdevice,"myLastShort","100") < 3) {
my $lastToggle = ReadingsNum($setdevice, "myLastShort","0");
if ($lastToggle == 1) {
readingsSingleUpdate($hash,"myLastShort","2",0);
$dimmLevel = 110;
} else {
$dimmLevel = 45;
}
CommandSet(undef, "$setdevice brightness $dimmLevel");
} else {
readingsSingleUpdate($hash,"myLastShort","0",0);
CommandSet(undef, "$setdevice off");
}
}
return;
}
sub milight_dimm_indirect {
my $name = shift;
my $event = shift // return;
my $Target_Devices = AttrVal($name,"Target_Device","devStrich0");
for my $setdevice (split (/,/,$Target_Devices)) {
if ($event =~ m/LongRelease/) {
CommandDeleteReading(undef,"-q $setdevice myLastdimmLevel");
} else {
milight_dimm($setdevice);
}
}
return;
}
sub milight_dimm {
my $Target_Device = shift // return;
my $dimmDir = ReadingsVal($Target_Device,"myDimmDir","up");
my $dimmLevel = ReadingsVal($Target_Device,"myLastdimmLevel",ReadingsNum($Target_Device,"brightness","255"));
if ($dimmDir ne "up") {
if ($dimmLevel < 4) {
readingsSingleUpdate($defs{$Target_Device}, "myDimmDir", "up", 0);
} else {
$dimmLevel -= $dimmLevel < 30 ? 3 : $dimmLevel < 70 ? 5 : $dimmLevel < 120 ? 7 : 15;
}
} else {
if ($dimmLevel > 244) {
readingsSingleUpdate($defs{$Target_Device}, "myDimmDir", "down", 0);
} else {
$dimmLevel += $dimmLevel < 30 ? 3 : $dimmLevel < 70 ? 5 : $dimmLevel < 120 ? 7 : 15;
}
}
CommandSet(undef, "$Target_Device brightness $dimmLevel");
readingsSingleUpdate($defs{$Target_Device}, "myLastdimmLevel",$dimmLevel, 0);
}
sub milight_FUT_to_RGBW {
my $name = shift;
my $event = shift // return;
my $rets = json2nameValue($event);
if (defined $rets->{state} && $rets->{state} =~ m/on|off/i) {
my $newState = lc($rets->{state});
CommandSet(undef, "$name $newState");
return { "CommandSet" => "$name $newState" };
}
if (defined $rets->{brightness}) {
my $bri = InternalVal($name,"TYPE","MQTT2_DEVICE") eq "HUEDevice" ? "bri" : "brightness";
CommandSet(undef, "$name $bri $rets->{brightness}");
return { "CommandSet" => "$name $bri $rets->{brightness}" };
}
if (defined $rets->{hue}) {
CommandSet(undef, "$name hue $rets->{hue}");
return { "CommandSet" => "$name hue $rets->{hue}" };
}
if (defined $rets->{command}) {
if ($rets->{command} =~ m/white/) {
CommandSet(undef, "$name command Weiss");
return { "CommandSet" => "$name command Weiss" };
}
return { "CommandSet" => "$rets->{command} not assigned" };
}
if (defined $rets->{saturation}) {
if (InternalVal($name,"TYPE","MQTT2_DEVICE") eq "HUEDevice") {
my $sat = int($rets->{saturation}*2.54);
CommandSet(undef, "$name sat $sat");
return { "CommandSet" => "$name sat $sat" };
} else {
CommandSet(undef, "$name saturation $rets->{saturation}");
return { "CommandSet" => "$name saturation $rets->{saturation}" };
}
}
}
sub milight_FUT_to_HUE {
my $name = shift;
my $event = shift // return;
my $rets = json2nameValue($event);
my $whitecol = shift // 'FFFFFF';
if (defined $rets->{state} && $rets->{state} =~ m/on|off/i) {
my $newState = lc($rets->{state});
CommandSet(undef, "$name $newState");
return { "CommandSet" => "$name $newState" };
}
if (defined $rets->{brightness}) {
my $bri = InternalVal($name,"TYPE","MQTT2_DEVICE") eq "HUEDevice" ? "bri" : "brightness";
CommandSet(undef, "$name $bri $rets->{brightness}");
return { "CommandSet" => "$name $bri $rets->{brightness}" };
}
if (defined $rets->{hue}) {
if (InternalVal($name,"TYPE","MQTT2_DEVICE") eq "HUEDevice") {
my $rgb = Color::hsv2hex($rets->{hue},ReadingsVal($name,"sat",100)/100,sprintf("%.2f",ReadingsVal($name,"bri",255)/255));
CommandSet(undef, "$name rgb $rgb");
return { "CommandSet" => "$name rgb $rgb" };
} else {
CommandSet(undef, "$name hue $rets->{hue}");
return { "CommandSet" => "$name hue $rets->{hue}" };
}
}
if (defined $rets->{command}) {
if ($rets->{command} =~ m/white/) {
if (InternalVal($name,"TYPE","MQTT2_DEVICE") eq "HUEDevice") {
CommandSet(undef, "$name rgb $whitecol");
return { "CommandSet" => "$name rgb $whitecol" };
} else {
CommandSet(undef, "$name command Weiss");
return { "CommandSet" => "$name command Weiss" };
}
}
return { "CommandSet" => "$rets->{command} not assigned" };
}
if (defined $rets->{saturation}) {
if (InternalVal($name,"TYPE","MQTT2_DEVICE") eq "HUEDevice") {
my $sat = int($rets->{saturation}*2.54);
CommandSet(undef, "$name sat $sat");
return { "CommandSet" => "$name sat $sat" };
} else {
CommandSet(undef, "$name saturation $rets->{saturation}");
return { "CommandSet" => "$name saturation $rets->{saturation}" };
}
}
}
sub milight_to_MPD {
my $name = shift;
my $event = shift // return;
return {"CommandSet" => "$name not present, no command issued" } if ReadingsVal($name,"presence","absent") eq "absent";
my $rets = json2nameValue($event);
if (defined $rets->{state} && $rets->{state} =~ m/on/i) {
if (ReadingsVal($name,"state","play") =~ /pause|stop/) {
CommandSet(undef, "$name play");
return { "CommandSet" => "$name play" };
} else {
return { "CommandSet" => "$name already playing" };
}
}
if (defined $rets->{state} && $rets->{state} =~ m/off/i) {
my $command = (ReadingsVal($name,"state","play") eq "pause" ) ? "stop" : "pause";
CommandSet(undef, "$name $command");
return { "CommandSet" => "$name $command" };
}
if (defined $rets->{brightness}) {
my $level = int (round ($rets->{brightness}/2.55,0));
CommandSet(undef, "$name volume $level");
return { "CommandSet" => "$name volume $level" };
}
if (defined $rets->{command}) {
if ($rets->{command} eq "mode_speed_up") {
CommandSet(undef, "$name previous") ;
return { "CommandSet" => "$name previous" } ;
}
if ($rets->{command} eq "mode_speed_down") {
CommandSet(undef, "$name next");
return { "CommandSet" => "$name next" };
}
else {
return { "CommandSet" => "$rets->{command} not assigned" };
}
}
if (defined $rets->{mode}) {
my $gainmode = CommandGet(undef, "$name mpdCMD replay_gain_status") =~ /album/ ? "auto" : "album";
CommandSet(undef, "$name mpdCMD replay_gain_mode $gainmode");
return { "CommandSet" => "$name mpdCMD replay_gain_mode $gainmode" };
}
if (defined $rets->{bulb_mode} && $rets->{bulb_mode} =~ m/white/) {
my $consumer = CommandGet(undef, "$name mpdCMD status") =~ /consume. 0/ ? "1" : "0";
CommandSet(undef, "$name mpdCMD consume $consumer");
return { "CommandSet" => "$name mpdCMD consume $consumer" };
}
return;
}
sub milight_to_shutter2 {
my $name = shift;
my $event = shift // return;
my $type = InternalVal($name,"TYPE","MQTT2_DEVICE");
my $moving = ReadingsVal($name,"motor","stop") =~ /stop/ ? 0 : 1;
$moving = 1 if ReadingsNum($name,"power",0) > 1 && $type eq "ZWave";
my $rets = json2nameValue($event);
my $com = "off";
my $now = gettimeofday;
if (!$moving && defined $rets->{state} && $rets->{state} =~ m/on|off/i) {
if ($now - ReadingsVal($name, "myLastRCOnOff",$now) < 5) {
CommandSetReading(undef,"$name myLastRCOnOff $now");
my $level = $rets->{state} =~ m/off/i ? 0 : 100;
$com = $type eq "ZWave" ? "dim" : "pct";
$level = 99 if ($level == 100 && $type eq "ZWave");
CommandSet(undef, "$name $com $level");
return { "CommandSet" => "$name $com $level" };
}
return CommandSetReading(undef,"$name myLastRCOnOff $now");
}
if (defined $rets->{state} && $rets->{state} =~ m/on|off/i) {
CommandSetReading(undef,"$name myLastRCOnOff $now");
CommandSet(undef,"$name stop");
return { "CommandSet" => "$name stop" };
}
if (defined $rets->{brightness}) {
my $level = int (round ($rets->{brightness}/2.55,0));
$com = $type eq "ZWave" ? "dim" : "pct";
$level = 99 if ($level == 100 && $type eq "ZWave");
CommandSet(undef, "$name $com $level");
return { "CommandSet" => "$name $com $level" };
}
if (defined $rets->{saturation}) {
my $slatname = $name;
my $slatlevel = 100 - $rets->{saturation};
$com = $type eq "ZWave" ? "dim" : "slats";
$slatlevel = 99 if ($slatlevel == 100 && $type eq "ZWave");
my ($def,$defnr) = split(" ", InternalVal($name,"DEF",$name));
$defnr++;
my @slatnames = devspec2array("DEF=$def".'.'.$defnr);
$slatname = shift @slatnames;
CommandSet(undef, "$slatname $com $slatlevel");
return { "CommandSet" => "$slatname $com $slatlevel" };
}
if (defined $rets->{color_temp}) {
my $slatname = $name;
my $slatlevel = 100 - ($rets->{color_temp}/(370-153))*100;
$com = $type eq "ZWave" ? "dim" : "slats";
$slatlevel = 99 if ($slatlevel == 100 && $type eq "ZWave");
my ($def,$defnr) = split(" ", InternalVal($name,"DEF",$name));
$defnr++;
my @slatnames = devspec2array("DEF=$def".'.'.$defnr);
$slatname = shift @slatnames;
CommandSet(undef, "$slatname $com $slatlevel");
return { "CommandSet" => "$slatname $com $slatlevel" };
}
return;
}
sub milight_4_Lights_matrix {
my $event = shift;
my $devA = shift // return;
my $devB = shift // $devA;
my $devC = shift // $devB;
my $devD = shift // $devC;
my $rets = json2nameValue($event);
if (defined $rets->{command}) {
if ($rets->{command} eq "mode_speed_up") {
CommandSet(undef, "$devC toggle") ;
return { "CommandSet" => "$devC toggle" } ;
}
if ($rets->{command} eq "mode_speed_down") {
CommandSet(undef, "$devA toggle");
return { "CommandSet" => "$devA toggle" };
}
if ($rets->{command} eq "set_white") {
CommandSet(undef, "$devB toggle");
return { "CommandSet" => "$devB toggle" };
}
}
if (defined $rets->{mode}) {
CommandSet(undef, "$devD toggle");
return { "CommandSet" => "$devD toggle" };
} else {
my @ondevs = devspec2array("($devA|$devB|$devC|$devD):FILTER=state=on");
if (@ondevs) {
CommandSet(undef, "($devA|$devB|$devC|$devD):FILTER=state=on off") if (defined $rets->{state} && $rets->{state} eq "OFF");
defined $rets->{state} && $rets->{state} eq "OFF" ? return { "CommandSet" => "($devA|$devB|$devC|$devD):FILTER=state=on off" } : return { "CommandSet" => "nothing to do, all already off" };
} else {
CommandSet(undef, "$devA on");
return { "CommandSet" => "$devA on" };
}
}
return;
}
1;
=pod
=begin html
<a name="myUtils_MiLight"></a>
<h3>myUtils_MiLight</h3>
<ul>
<b>Routines to handle remote control signals in MiLight context</b><br>
All MiLight hardware (bulbs and remotes) are represented by MQTT2_DEVICE using a esp8266_milight_hub as described here: https://github.com/sidoh/esp8266_milight_hub.<br>
This pieces of code offer options to build links between MiLight@MQTT2_DEVICE and "other" FHEM devices (e.g. HUE bulbs, shutter devices, Homematic remotes) and can also be used to built an indirect bridge between V6 remotes and V5 bulbs.
</ul>
<ul><ul>
<b>Routines to use MiLight remotes as input device</b><br>
NOTE: As activation of a specific layer on the remote requires to press the "on" button, often the first "on" command will be ignored. You have to press the key twice within a few seconds in these cases. This is especially the case, when controlling other devices than lights to avoid unexpected or unintended behaviour (like starting a music player when only reduction of volume is intended as first step). <br><br>
For most It's recommended to setup a single MQTT2_DEVICE per remote to dispatch all received commands like this one:<br>
<pre>defmod MiLight_RC_WZ MQTT2_DEVICE milight_0x5D47_0
attr MiLight_RC_WZ readingList milight/updates/0x5D47/fut089/0:.* { milight_to_MPD('myMPD',$EVENT) }\
milight/updates/0x5D47/fut089/1:.* { milight_FUT_to_RGBW('Licht_Stehlampe_links',$EVENT) }\
milight/updates/0x5D47/fut089/2:.* { milight_FUT_to_RGBW('Licht_Stehlampe_rechts',$EVENT) }\
milight/updates/0x5D47/fut089/3:.* { milight_4_Lights_matrix($EVENT, 'Licht_WoZi_Vorn_Aussen', 'Licht_WoZi_Vorn_Mitte', 'Licht_WoZi_Hinten_Aussen', 'Licht_WoZi_Hinten_Mitte') }\
milight/updates/0x5D47/fut089/4:.* { milight_to_shutter2('Jalousie_WZ',$EVENT) }\
milight/updates/0x5D47/fut089/5:.* { milight_to_shutter2('Rollladen_WZ_SSO',$EVENT) }\
milight/updates/0x5D47/fut089/6:.* { milight_to_shutter2('Rollladen_WZ_SSW',$EVENT) }\
milight/updates/0x5D47/fut089/7:.* {}\
milight/updates/0x5D47/fut089/8:.* {}\
milight/states/0x5D47/fut089/[0-8]:.* {}
</pre>
The last three lines will just prevent further actions derived frim the not used channels (or the states info including everything) from to hub.<br>
All routines expect one or more target device names to be switched, the received JSON ($EVENT) will be handed over as is and is analysed within the routines.<br><br>
<ul>
<b>milight_FUT_to_RGBW</b><br>
Allows indirect control of
<li>other MiLight devices using a different protocol or </li>
<li>other light devices. Especially HUEDevice should work also for brightness and hue commands.</li>
</ul><br>
<ul>
<b>milight_FUT_to_HUE</b><br>
Just like milight_FUT_to_RGBW, but with extended support for HUEDevice type target devices.
</ul><br>
<ul>
<b>milight_to_shutter2</b><br>
Allows control of shutter devices. Tested with following devices:<br>
<li>HM-LC-Bl1PBU-FM as CUL_HM, shutters only</li>
<li>ZWave actor FGR-223 in venetian mode - lamella position can be controlled via saturation slider</li>
</ul><br>
<ul>
<b>milight_4_Lights_matrix</b><br>
Allows to control a group of 4 simple on/off devices using the four mode buttons (in the middle of the remote).<br>
</ul><br>
<ul>
<b>milight_to_MPD</b><br>
Allows control of a MusicPlayerDeamon - basics like play, pause, stop and volume.<br>
Additionally toggle two replay gain modes and "consumer" setting.
</ul></ul>
<ul>
<br><br>
<b>Routines to use other remote types to control MiLight bulbs</b><br>
<code>milight_dimm_indirect($$)</code> and <code>milight_toggle_indirect($)</code> are intended for the use in notify code to derive commands to one or multiple bulbs. Parameter typically is $NAME or $EVTPART0.<br>
To get the logical link, e.g. from a button to a specific bulb, a userattr value is used, multiple bulbs have to be comma-separated.<br>
</ul>
<ul>
Examples:
</ul>
<ul>
<code>attr Schalter_Spuele_Btn_04 userattr Target_Device<br>attr Schalter_Spuele_Btn_04 Target_Device Licht_Essen</code><br>
This way, one notify can be used to derive actions on various devices
</ul>
<ul>
<code>defmod MiLight_dimm notify Schalter_Spuele_Btn_0[124]:Long..*[\d]+_[\d]+.\(to.VCCU\) {milight_dimm_indirect($NAME,$EVENT)}<br>defmod MiLight_toggle notify Schalter_Spuele_Btn_0[124]:Short.[\d]+_[\d]+.\(to.VCCU\) {milight_toggle_indirect($NAME)}</code><br>
</ul>
</ul>
<ul>
</ul>
=end html
=cut