-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path31_LIFXBulb.pm
123 lines (100 loc) · 2.81 KB
/
31_LIFXBulb.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
package main;
use strict;
use warnings;
use POSIX;
use Device::LIFX;
use Device::LIFX::Constants qw(LIGHT_STATUS);
use SetExtensions;
use Color;
use Data::Dumper;
sub LIFXBulb_Initialize($)
{
my ($hash) = @_;
$hash->{DefFn} = "LIFXBulb_Define";
$hash->{SetFn} = "LIFXBulb_Set";
$hash->{Match} = ".*";
$hash->{ParseFn} = "LIFXBulb_Parse";
$hash->{AttrList} = "IODev ". $readingFnAttributes;
FHEM_colorpickerInit();
}
sub LIFXBulb_Parse($$)
{
my ($hash, $msg) = @_;
my $label = $msg->label();
my $bulb_hash = $modules{LIFXBulb}{defptr}{$label};
if (!defined($bulb_hash)) {
my $mac = $msg->bulb_mac();
$bulb_hash = $modules{LIFXBulb}{defptr}{$mac};
}
if ($msg->type() == LIGHT_STATUS) {
$bulb_hash->{STATE} = ($msg->power()) ? "on" : "off";
DoTrigger($bulb_hash->{NAME}, $bulb_hash->{STATE});
}
return $bulb_hash->{NAME} || $hash->{NAME};
}
sub LIFXBulb_Define($$)
{
my ($hash, $def) = @_;
my ($name, $type, $id) = split(' ', $def);
if (!defined($id)) {
return "Usage: <NAME> LIFXBulb <XX:XX:XX:XX:XX:XX>|Label"
}
my @mac = split(':',$id);
if ($#mac == 5) {
@mac = map {hex($_)} @mac;
$id = pack('C*', @mac);
}
$hash->{STATE} = 'Initialized';
$hash->{ID} = $id;
$hash->{NAME} = $name;
AssignIoPort($hash);
if(defined($hash->{IODev}->{NAME})) {
Log3 $name, 1, "$name: I/O device is " . $hash->{IODev}->{NAME};
} else {
Log3 $name, 1, "$name: no I/O device";
}
$modules{LIFXBulb}{defptr}{$id} = $hash;
return undef;
}
sub LIFXBulb_Undefine($$)
{
my ($hash,$arg) = @_;
RemoveInternalTimer($hash);
return undef;
}
sub LIFXBulb_Set($@)
{
my ($hash,$name,@args) = @_;
my $lifx = $hash->{IODev}->{lifx}{lifx};
my $id = $hash->{ID};
my $bulb = $lifx->get_bulb_by_label($id);
if (!defined($bulb)) {
$bulb = $lifx->get_bulb_by_mac($id);
defined($bulb) ||
return "Can't find bulb with id: $id";
}
if ($args[0] eq 'on') {
$bulb->on();
$hash->{STATE} = 'on';
} elsif ($args[0] eq 'off') {
$bulb->off();
$hash->{STATE} = 'off';
} elsif ($args[0] eq 'color') {
my ($b,$k,$h,$s,$t) = @args[1 .. $#args];
$bulb->color([$h,$s,$b,$k], $t);
} elsif ($args[0] eq 'kelvin') {
my $color = $bulb->color();
my $k = $args[1];
my $t = $args[2] | 0;
$bulb->color([0,0,$color->[2],$k], $t);
} elsif ($args[0] eq 'rgb') {
my ($r,$g,$b) = ($args[1] =~ m/(..)(..)(..)/);
($r,$g,$b) = map {hex($_)} ($r,$g,$b);
$bulb->rgb([$r,$g,$b], 0);
}
else {
return "off:noArg on:noArg toggle:noArg rgb:colorpicker,RGB kelvin:slider,2500,1,7000";
}
return undef;
}
1;