blib/lib/Protocol/IR/Format/Wig.pm

filter: Statementstmt 100% 76 / 76 Branchbran 82.3% 28 / 34 Conditioncond 55.5% 20 / 36 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 79.3% 142 / 179 CC 36 SCAR 28.4
CC
36
Cov
85%
decode
27.8
export
12.5
_read_input
4.0
CRAP 17.1
« blib/lib/Protocol/IR/Format/CSV.pm ↑ Summary blib/lib/Protocol/IR/Proto/RC5.pm »
1 package Protocol::IR::Format::Wig;
2 55 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 55 our $VERSION = '1.5';
6  
7 55 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 55 ▶use Protocol::IR::Code;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use Protocol::IR::Format::Pronto;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 55 ▶use Protocol::IR::Format::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 55 ▶use constant FORMAT_VERSION => 'hair-wig/3';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # wig is the portable IR code set format
16 # used by the HAIR Home Assistant integration
17 # (https://github.com/DAB-LABS/HAIR): one JSON file, one remote, raw
18 # Pronto hex as the payload.
19 #
20 # We emit hair-wig/3, the current recipe major: every signal carries
21 # an explicit ditto_count and bypass_protocol, plus an optional
22 # send_count when a code carries a repeat count other than the default
23 # single press. On import, signals are decoded fresh through the
24 # registered protocols (the file never carries decoded fields) and each
25 # signal's transmit recipe is kept on the resulting Protocol::IR::Code.
26  
27 33 ▶sub decode {
Subroutine decode: called CC=22 SCAR=33.2
Pod documented
28 33 my ($class, $input, $registry) = @_;
29 33 ▶ die "No wig input provided\n" unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 1 32 33
30  
31 32 my $text = _read_input($input);
32 32 $text =~ s/^\x{FEFF}//; # Strip UTF-8 BOM if present
33  
34 32 my $data = eval { JSON::PP->new->utf8->decode($text) };
35 32 ▶ die "Invalid wig JSON: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 31 32
36 31 ▶ die "wig top level must be a JSON object\n" unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 1 30 31
37 # A JSON IR database document (a "commands" list with raw Pronto hex
38 # payloads, no hair-wig "format" field) carries the same signals a wig
39 # does, so the wig entry point imports it interchangeably.
40 30 ▶ if (!defined $data->{format} && (ref $data->{commands} eq 'ARRAY' || defined $data->{codeset})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data->{"format"} and ref $data->{"commands"} eq "ARRAY" || defined $data->{"codeset"}) 3 27 30
Condition66%4/6
not defined $data->{"format"} and ref $data->{"commands"} eq "ARRAY" || defined $data->{"codeset"}
!ll&&!rl&&r
2703
ref $data->{"commands"} eq "ARRAY" or defined $data->{"codeset"}
l!l&&r!l&&!r
210
MC/DC33%1/3
not defined $data->{"format"} and ref $data->{"commands"} eq "ARRAY" || defined $data->{"codeset"}
not defined $data->{"format"} ref $data->{"commands"} eq "ARRAY" defined $data->{"codeset"}
Truth table75%3/4
not(A) and B or C
A= not defined $data->{"format"}B= ref $data->{"commands"} eq "ARRAY"C= defined $data->{"codeset"}
ABCresult
0XX0
1000
11X1
1011
41 3 return Protocol::IR::Format::JSON->decode($input, $registry);
42 }
43 die "Unsupported wig format: " . ($data->{format} // '(missing)') . "\n"
44 27 ▶ unless defined $data->{format} && $data->{format} =~ m{^hair-wig/([1-3])$};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"format"} and $data->{"format"} =~ m[^hair-wig/([1-3])$] 1 26 27
Condition60%3/5
defined $data->{"format"} and $data->{"format"} =~ m[^hair-wig/([1-3])$]
!ll&&!rl&&r
0126
$data->{"format"} // "(missing)"
l!l
10
MC/DC50%1/2
defined $data->{"format"} and $data->{"format"} =~ m[^hair-wig/([1-3])$]
defined $data->{"format"} $data->{"format"} =~ m[^hair-wig/([1-3])$]
MC/DC0%0/1
$data->{"format"} // "(missing)"
$data->{"format"}
Truth table66%2/3
A and B
A= defined $data->{"format"}B= $data->{"format"} =~ m[^hair-wig/([1-3])$]
ABresult
0X0
100
111
Truth table50%1/2
A
A= $data->{"format"}
Aresult
11
00
45  
46 26 my @decoded_codes;
47 26 ▶ for my $sig (@{ $data->{signals} || [] }) {
Condition50%1/2
$data->{'signals'} || []
l!l
260
MC/DC0%0/1
$data->{'signals'} || []
$data->{'signals'}
Truth table50%1/2
A
A= $data->{'signals'}
Aresult
11
00
48 48 ▶ next unless ref $sig eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sig eq "HASH" 0 48 48
49 48 ▶ next unless defined $sig->{pronto} && $sig->{pronto} ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig->{"pronto"} and $sig->{"pronto"} ne "" 0 48 48
Condition33%1/3
defined $sig->{"pronto"} and $sig->{"pronto"} ne ""
!ll&&!rl&&r
0048
MC/DC0%0/2
defined $sig->{"pronto"} and $sig->{"pronto"} ne ""
defined $sig->{"pronto"} $sig->{"pronto"} ne ""
Truth table33%1/3
A and B
A= defined $sig->{"pronto"}B= $sig->{"pronto"} ne ""
ABresult
0X0
100
111
50  
51 48 my $code = eval { $registry->import_format('Pronto', $sig->{pronto}) };
52 48 ▶ die "wig signal '" . ($sig->{alias} // '') . "' cannot be decoded: $@\n"
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $code 0 48 48
Condition20%1/5
$@ or not defined $code
l!l&&r!l&&!r
0048
$sig->{"alias"} // ""
l!l
00
MC/DC0%0/2
$@ or not defined $code
$@ not defined $code
MC/DC0%0/1
$sig->{"alias"} // ""
$sig->{"alias"}
Truth table33%1/3
A or not(B)
A= $@B= not defined $code
ABresult
1X1
011
000
Truth table0%0/2
A
A= $sig->{"alias"}
Aresult
11
00
53 if $@ || !defined $code;
54  
55 48 ▶ $code->alias($sig->{alias} // '');
Condition50%1/2
$sig->{"alias"} // ""
l!l
480
MC/DC0%0/1
$sig->{"alias"} // ""
$sig->{"alias"}
Truth table50%1/2
A
A= $sig->{"alias"}
Aresult
11
00
56 48 ▶ $code->ditto_count($sig->{ditto_count} // 0);
Condition100%2/2
$sig->{"ditto_count"} // 0
l!l
462
MC/DC100%1/1
$sig->{"ditto_count"} // 0
$sig->{"ditto_count"}
Truth table100%2/2
A
A= $sig->{"ditto_count"}
Aresult
11
00
57 48 ▶ $code->bypass_protocol($sig->{bypass_protocol} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$sig->{'bypass_protocol'} ? : 5 43 48
58 # send_count (how many times the whole signal transmits per press) is
59 # optional with a default of 1; preserve it so a wig -> wig or later
60 # export round trip keeps it.
61 48 ▶ $code->send_count($sig->{send_count}) if defined $sig->{send_count};
Branch: 1 branch
BranchTrueFalseTotal
if defined $sig->{"send_count"} 0 48 48
62 48 push @decoded_codes, $code;
63 }
64  
65 26 return \@decoded_codes;
66 }
67  
68 # Generate a wig from one Protocol::IR::Code or an arrayref of them.
69 #
70 # Options:
71 # name - remote name (required by the format; default 'Untitled')
72 # brand - manufacturer name
73 # model - model string
74 # kind - device kind slug (tv, soundbar, ...)
75 # notes - free-form notes
76 # origin - provenance string; default marks this library as the converter
77 # wig_id - identity UUID (default: a freshly minted v4 UUID)
78 # format - format major to emit (default: hair-wig/3)
79 32 ▶sub export {
Subroutine export: called CC=12 SCAR=25.3
Pod documented
80 32 my ($class, $codes, $registry, %opts) = @_;
81 32 ▶ $codes = [$codes] unless ref $codes eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $codes eq "ARRAY" 18 14 32
82  
83 32 my @signals;
84 32 for my $code (@$codes) {
85 78 my $pronto = $registry->export_code($code, 'Pronto');
86 78 ▶ my %sig = (
Branch: 1 branch
BranchTrueFalseTotal
$code->bypass_protocol ? : 4 74 78
Condition50%1/2
$code->ditto_count // 0
l!l
780
MC/DC0%0/1
$code->ditto_count // 0
$code->ditto_count
Truth table50%1/2
A
A= $code->ditto_count
Aresult
11
00
87 alias => $code->alias,
88 pronto => $pronto,
89 ditto_count => $code->ditto_count // 0,
90 bypass_protocol => $code->bypass_protocol ? JSON::PP::true : JSON::PP::false,
91 );
92 # send_count is optional with a default of 1, so only a repeat count
93 # that differs from the default is written (the canonical forms leave
94 # it out); a code with no recorded repeat (send_count 0) also omits
95 # it, keeping existing exports byte-stable.
96 78 ▶ $sig{send_count} = $code->send_count if $code->send_count >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $code->send_count >= 2 0 78 78
97 78 push @signals, \%sig;
98 }
99  
100 my %wig = (
101 format => $opts{format} // FORMAT_VERSION,
102 name => $opts{name} // 'Untitled',
103 wig_id => $opts{wig_id} // _new_uuid(),
104 32 ▶ origin => $opts{origin} // 'converted by Protocol::IR::Converter',
Condition66%6/9
$opts{"format"} // "hair-wig/3"
l!l
032
$opts{"name"} // "Untitled"
l!l
1517
$opts{"wig_id"} // _new_uuid()
l!l&&r!l&&!r
0320
$opts{"origin"} // "converted by Protocol::IR::Converter"
l!l
1022
MC/DC0%0/1
$opts{"format"} // "hair-wig/3"
$opts{"format"}
MC/DC100%1/1
$opts{"name"} // "Untitled"
$opts{"name"}
MC/DC0%0/2
$opts{"wig_id"} // _new_uuid()
$opts{"wig_id"} _new_uuid()
MC/DC100%1/1
$opts{"origin"} // "converted by Protocol::IR::Converter"
$opts{"origin"}
Truth table50%1/2
A
A= $opts{"format"}
Aresult
11
00
Truth table100%2/2
A
A= $opts{"name"}
Aresult
11
00
Truth table33%1/3
A // B
A= $opts{"wig_id"}B= _new_uuid()
ABresult
1X1
011
000
Truth table100%2/2
A
A= $opts{"origin"}
Aresult
11
00
105 signals => \@signals,
106 );
107 32 for my $key (qw(brand model kind notes)) {
108 128 ▶ $wig{$key} = $opts{$key} if defined $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{$key} 13 115 128
109 }
110  
111 32 my $json = JSON::PP->new->utf8->canonical->space_after->indent(4);
112 32 return $json->encode(\%wig) . "\n";
113 }
114  
115 # Treat $input as file content when it looks like multi-line data,
116 # otherwise as a path to a file (falling back to treating it as a
117 # single-line JSON string).
118 32 ▶sub _read_input {
Subroutine _read_input: called CC=4 SCAR=13.9
119 32 my ($input) = @_;
120 32 ▶ return $input if $input =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ /[\r\n]/ 21 11 32
121  
122 11 ▶ if (-e $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $input) 6 5 11
123 6 ▶ open my $fh, '<', $input or die "Cannot open wig file '$input': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input or die "Cannot open wig file '${input}': $!\n" 6 0 6
124 6 local $/;
125 6 my $text = <$fh>;
126 6 close $fh;
127 6 return $text;
128 }
129 5 return $input;
130 }
131  
132 # A random UUID v4: eight groups of hex in the 8-4-4-4-12 layout, with
133 # the version and variant bits set.
134 32 ▶sub _new_uuid {
Subroutine _new_uuid: called CC=1 SCAR=0.0
135 32 my @bytes = map { sprintf('%02x', int(rand(256))) } 1 .. 16;
136 32 my $hex = join '', @bytes;
137 32 substr($hex, 12, 1, '4');
138 32 substr($hex, 16, 1, sprintf('%x', 0x8 | (hex(substr($hex, 16, 1)) & 0x3)));
139 32 return join '-',
140 substr($hex, 0, 8),
141 substr($hex, 8, 4),
142 substr($hex, 12, 4),
143 substr($hex, 16, 4),
144 substr($hex, 20, 12);
145 }
146  
147 55 1;
148  
149 =head1 NAME
150  
151 Protocol::IR::Format::Wig - HAIR wig JSON import and export
152  
153 =head1 VERSION
154  
155 version 1.5
156  
157 =head1 SYNOPSIS
158  
159 use Protocol::IR::Converter;
160  
161 my $converter = Protocol::IR::Converter->new();
162 my @codes = @{ $converter->import_format('CSV', 'remote.csv') };
163  
164 # Export Protocol::IR::Code objects to a HAIR hair-wig/3 JSON document
165 my $wig = $converter->export_codes('wig', \@codes,
166 name => 'Tigersecu DVR',
167 brand => 'Tigersecu',
168 model => 'TS-1080',
169 kind => 'dvr',
170 notes => 'Converted from the IRDB sample set',
171 );
172  
173 # Import a wig (file path or JSON string) back into Protocol::IR::Code objects
174 my $imported = $converter->import_format('wig', 'remote.wig.json');
175 for my $code (@$imported) {
176 print $code->alias . " (" . $code->protocol . ")\n";
177 }
178  
179 =head1 DESCRIPTION
180  
181 wig is the portable IR code set format used by
182 the HAIR Home Assistant integration
183 (L<https://github.com/DAB-LABS/HAIR>): one JSON file, one remote, raw
184 Pronto hex as the payload.
185  
186 C<Protocol::IR::Format::Wig> emits C<hair-wig/3>, the current recipe major: every
187 signal carries an explicit C<ditto_count> and C<bypass_protocol>, plus an optional
188 C<send_count> (how many times the whole signal transmits per press) when a code
189 carries a repeat count other than the default. On import,
190 signals are decoded fresh through the registered protocols (the file never
191 carries decoded fields), and each signal's transmit recipe is kept on the
192 resulting L<Protocol::IR::Code>. Formats C<hair-wig/1> through C<hair-wig/3> are
193 accepted on import.
194  
195 =over 4
196  
197 =item Example C<hair-wig/3> document (a NEC signal)
198  
199 {
200 "format": "hair-wig/3",
201 "name": "Samsung TV",
202 "brand": "Samsung",
203 "origin": "converted from IRDB",
204 "signals": [
205 {
206 "alias": "",
207 "bypass_protocol": false,
208 "ditto_count": 0,
209 "pronto": "0000 006D 0022 0000 0157 00AC 0015 0015 ..."
210 }
211 ],
212 "wig_id": "d50a492e-a604-4c32-8bcb-aa542da06023"
213 }
214  
215 =back
216  
217 =head1 METHODS
218  
219 =head2 export
220  
221 my $json = $class->export($codes, $registry, %opts);
222  
223 Serializes one L<Protocol::IR::Code> or an arrayref of them into a C<hair-wig/3> JSON
224 document. Options:
225  
226 =over 4
227  
228 =item * C<name> -- remote name (required by the format; default
229 C<'Untitled'>)
230  
231 =item * C<brand>, C<model>, C<kind>, C<notes> -- optional remote metadata
232  
233 =item * C<origin> -- provenance string; defaults to
234 C<'converted by Protocol::IR::Converter'>
235  
236 =item * C<wig_id> -- identity UUID (default: a freshly minted v4 UUID)
237  
238 =item * C<format> -- format major to emit (default: C<hair-wig/3>)
239  
240 =back
241  
242 =head2 decode
243  
244 my $codes = $class->decode($input, $registry);
245  
246 Parses a wig file path or JSON string and returns an arrayref of L<Protocol::IR::Code>
247 objects. C<alias>, C<ditto_count>, C<bypass_protocol>, and C<send_count> when
248 present are preserved on each code. Dies if the JSON is invalid, the format
249 is unsupported, or a signal's Pronto payload cannot be decoded.
250  
251 =head1 SUPPORT
252  
253 Source code: L<https://github.com/bwarden/perl-protocol-ir>
254  
255 Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues>
256  
257 =head1 AUTHOR
258  
259 Brett T. Warden <bwarden@cpan.org>
260  
261 =head1 COPYRIGHT AND LICENSE
262  
263 Copyright (c) 2026 Brett T. Warden
264  
265 This library is free software; you can redistribute it and/or modify it
266 under the terms of the GNU General Public License as published by the
267 Free Software Foundation; either version 2 of the License, or (at your
268 option) any later version.
« blib/lib/Protocol/IR/Format/CSV.pm ↑ Summary blib/lib/Protocol/IR/Proto/RC5.pm »