File Coverage

blib/lib/Device/RFXCOM/Decoder/HomeEasy.pm
Criterion Covered Total %
statement 52 52 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 80 80 100.0


line stmt bran cond sub pod time code
1 3     3   5002 use strict;
  3         8  
  3         122  
2 3     3   15 use warnings;
  3         4  
  3         221  
3             package Device::RFXCOM::Decoder::HomeEasy;
4             $Device::RFXCOM::Decoder::HomeEasy::VERSION = '1.163170';
5             # ABSTRACT: Device::RFXCOM::Decoder::HomeEasy decode HomeEasy RF messages
6              
7              
8 3     3   116 use 5.006;
  3         11  
9 3     3   15 use constant DEBUG => $ENV{DEVICE_RFXCOM_DECODER_HOMEEASY_DEBUG};
  3         4  
  3         312  
10 3     3   15 use Carp qw/croak/;
  3         5  
  3         219  
11 3     3   19 use base 'Device::RFXCOM::Decoder';
  3         4  
  3         1461  
12 3     3   1360 use Device::RFXCOM::Response::HomeEasy;
  3         3  
  3         1111  
13              
14              
15             sub decode {
16 81     81 1 151 my ($self, $parent, $message, $bytes, $bits, $result) = @_;
17              
18 81 100 100     662 $bits == 34 or $bits == 38 or return;
19              
20             # HomeEasy devices seem to send duplicates with different byte[4] high nibble
21 4         6 my @b = @{$bytes};
  4         12  
22 4         8 my $b4 = $b[4];
23 4         7 $b[4] &= 0xf;
24 4 100       13 if ($b[4] != $b4) {
25 3         13 $result->{key} = $bits.'!'.(pack "C*", @b);
26 3         10 my $entry = $parent->_cache_get($result);
27 3 100       10 if ($entry) {
28 1         4 $result->{messages} = $entry->{result}->{messages};
29 1         12 $result->{duplicate} = $parent->_cache_is_duplicate($entry);
30 1         7 return 1;
31             }
32 2         3 $b[4] = $b4;
33             }
34              
35 3         9 my $res = from_rf($bits, $bytes);
36              
37             printf "homeeasy c=%s u=%s a=%x\n",
38 3         4 $res->{command}, $res->{unit}, $res->{address} if DEBUG;
39             my %body = (
40             address => (sprintf "%#x",$res->{address}),
41             unit => $res->{unit},
42             command => $res->{command},
43 3         25 );
44              
45 3 100       12 $body{level} = $res->{level} if ($res->{command} eq 'preset');
46              
47 3         5 push @{$result->{messages}}, Device::RFXCOM::Response::HomeEasy->new(%body);
  3         39  
48 3         21 return 1;
49             }
50              
51              
52             sub from_rf {
53 3     3 1 8 my $length = shift;
54 3         4 my $bytes = shift;
55 3         7 my %p = ();
56 3         17 $p{address} = ($bytes->[0] << 18) + ($bytes->[1] << 10) +
57             ($bytes->[2] << 2) + ($bytes->[3] >> 6);
58 3         7 my $command = ($bytes->[3] >> 4) & 0x3;
59 3 100       13 $p{unit} = ($command & 0x2) ? 'group' : ($bytes->[3] & 0xf);
60 3 100       25 if ($length == 38) {
61 1         3 $p{command} = 'preset';
62 1         4 $p{level} = $bytes->[4] >> 4;
63             } else {
64 2 100       10 $p{command} = ($command & 0x1) ? 'on' : 'off';
65             }
66 3         8 return \%p;
67             }
68              
69             1;
70              
71             __END__