talk to tasmota over mqtt and emulate LG TV remote
[mqtt-homehub] / tasmota-ir-tv.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Term::ReadKey;
6 use Data::Dump qw(dump);
7 use Time::HiRes qw(sleep);
8
9 my $name2ir;
10
11 while(<DATA>) {
12         chomp;
13         my ($code,$name) = split(/\t+/,$_,2);
14         $name2ir->{lc($name)} = $code;
15 }
16 warn "# name2ir = ",dump($name2ir);
17
18 my $direction = {
19         A => 'UP',
20         B => 'DOWN',
21         C => 'RIGHT',
22         D => 'LEFT',
23 };
24
25 sub ir {
26         my $key = shift;
27         $key = lc $key;
28         if ( my $data = $name2ir->{$key} ) {
29                 my $cmd = qq{mosquitto_pub -h rpi2 -q 2 -t cmnd/ir/IRSend -m '{"protocol": "NEC","bits": 32, "data": $data}'};
30                 warn "# [$key] $cmd\n";
31                 system($cmd);
32         } else {
33                 warn "ERROR: missing mapping for [$key]";
34         }
35 }
36
37 ReadMode 4; # Turn off controls keys
38 my $key;
39 my $esc;
40 while(1) {
41         while (not defined ($key = ReadKey(1))) {
42                 # No key yet
43         }
44         print dump($key, $esc), $/;
45         if ( $key eq 'q' ) {
46                 ReadMode 0; # Reset tty mode before exiting
47                 exit 0;
48         }
49         if ( $key eq "\e" ) {
50                 $esc = $key;
51                 $key = '?';
52         } elsif ( $esc ) {
53                 $esc .= $key;
54         }
55
56         if ( $esc && $esc =~ /\e\[([ABCD])/ ) {
57                 printf "arrow: %s %s\n", $1, $direction->{$1};
58                 ir($direction->{$1});
59                 $esc = '';
60         } elsif ( $key eq "\e" && $esc eq "\e" ) { # single ESC
61                 ir('BACK');
62                 $esc = '';
63         } else {
64                 warn "UNKNOWN esc,key = ",dump($esc, $key);
65         }
66
67         ir('MENU') if $key eq 'm';
68         ir('INFO') if $key eq 'i';
69         ir('on/off') if $key eq 'p';
70         ir('settings') if $key eq 's';
71         ir('OK') if $key eq 'o' || $key eq "\n" || $key eq " ";
72
73         ir('EXIT') if $key eq "\x7F"; # backspace
74
75         ir('input') if $key eq 'w';
76         ir('energy') if $key eq 'e';
77
78         ir('Q.View') if $key eq 'a';
79         # s
80         ir('guide') if $key eq 'd';
81
82         ir('HDMI 1') if $key eq 'z';
83         ir('HDMI 2') if $key eq 'x';
84         ir('HDMI 3') if $key eq 'c';
85
86 }
87
88 __DATA__
89 0x20DF10EF      ON/OFF
90 0x20DF0FF0      TV
91 0x20DF9E61      Ratio
92 0x20DFD02F      Input
93 0x20DFA956      Energy
94 0x20DF8877      Key 1
95 0x20DF48B7      Key 2
96 0x20DFC837      Key 3
97 0x20DF28D7      Key 4
98 0x20DFA857      Key 5
99 0x20DF6897      Key 6
100 0x20DFE817      Key 7
101 0x20DF18E7      Key 8
102 0x20DF9867      Key 9
103 0x20DFCA35      List
104 0x20DF08F7      Key 0
105 0x20DF58A7      Q.View
106 0x20DF40BF      Vol+
107 0x20DFC03F      Vol-
108 0x20DFCE31      Vol-
109 0x20DF7887      Fav
110 0x20DF3BC4      3D
111 0x20DF906F      Mute
112 0x20DF00FF      P+
113 0x20DF807F      P-
114 0x20DFC23D      Settings
115 0x20DF55AA      Info
116 0x20DFA25D      Menu
117 0x20DFE01F      Left
118 0x20DF609F      Right
119 0x20DF02FD      Up
120 0x20DF827D      Down
121 0x20DF22DD      OK
122 0x20DF14EB      Back
123 0x20DFD52A      Guide
124 0x20DFDA25      Exit
125 0x20DF4EB1      Red
126 0x20DF8E71      Green
127 0x20DFC639      Yellow
128 0x20DF8679      Blue
129 0x20DF04FB      Text
130 0x20DF847B      T.Opt
131 0x20DF9C63      Subtitle
132 0x20DF8D72      Stop
133 0x20DF0DF2      Play
134 0x20DF5DA2      Pause
135 0x20DFF10E      Backward
136 0x20DF718E      Forward
137 0x20DF7E81      Simplink
138 0x20DF8976      AD
139 0x20DF0CF3      AV Mode
140 0x20DF3EC1      Live TV
141 0x20DF42BD      Live TV
142 0x20DF35CA      Status
143 0x20DF50AF      Audio Language
144 0x20DF57A8      Shortmenu
145 0x20DF5AA5      AV
146 0x20DF5EA1      online manual
147 0x20DF619E      Volume to 50
148 0x20DF21DE      Volume to 80
149 0x20DFE11E      Volume to 100
150 0x20DF6B94      Radio/TV
151 0x20DF708F      doze function
152 0x20DFA35C      Display off
153 0x20DFD728      Standby
154 0x20DF23DC      Power on
155 0x20DFB44B      Settings of remote
156 0x20DF956A      Program guide
157 0x20DFF00F      Radio/TV
158 0x20DF738C      HDMI 1
159 0x20DF33CC      HDMI 2
160 0x20DF9768      HDMI 3
161 0x20DFFD02      Component
162 0x20DFF50A      Live Zoom
163 0x20DFF20D      Input info
164 0x20DFB24D      Picture mode
165 0x20DF09F6      Recordings
166 0x20DF4AB5      Audio mode
167 0x20DF7B84      Register magic remote
168 0x20DFAE51      directly register magic remote
169 0x20DF1CE3      Subtitles
170 0x20DFCC33      Presentation in shop
171 0x20DFBD42      Start recording
172 0x20DFAD52      LG TV Plus
173 0x20DFDD22      Sports mode
174 0x20DF1EE1      Search
175 0x20DFDF20      Warning: instart
176 0x20DF7F80      Warning: Poweronly
177 0x20DFFF00      Warning: Easy adjust
178 0x20DF5FA0      Warning: instop
179 0x20DF5FA0      Enter Password
180 0x20DF3FC0      Warning: Reset to factory