46_TRX_LIGHT.pm 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. # $Id: 46_TRX_LIGHT.pm 11592 2016-06-01 21:15:30Z wherzig $
  2. ##############################################################################
  3. #
  4. # 46_TRX_LIGHT.pm
  5. # FHEM module for lighting protocols:
  6. # X10 lighting, ARC, ELRO AB400D, Waveman, Chacon EMW200,
  7. # IMPULS, AC (KlikAanKlikUit, NEXA, CHACON, HomeEasy UK),
  8. # HomeEasy EU, ANSLUT, Ikea Koppla
  9. #
  10. # Copyright (C) 2012-2016 by Willi Herzig (Willi.Herzig@gmail.com)
  11. #
  12. # This file is part of fhem.
  13. #
  14. # Fhem is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation, either version 2 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # Fhem is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  26. #
  27. ##############################################################################
  28. #
  29. # values for "set global verbose"
  30. # 4: log unknown protocols
  31. # 5: log decoding hexlines for debugging
  32. #
  33. package main;
  34. use strict;
  35. use warnings;
  36. my $time_old = 0;
  37. my $TRX_LIGHT_type_default = "ds10a";
  38. my $TRX_LIGHT_X10_type_default = "x10";
  39. my $DOT = q{_};
  40. my %light_device_codes = ( # HEXSTRING => "NAME", "name of reading",
  41. # 0x10: Lighting1
  42. 0x1000 => [ "X10", "light" ],
  43. 0x1001 => [ "ARC", "light" ],
  44. 0x1002 => [ "AB400D", "light" ],
  45. 0x1003 => [ "WAVEMAN", "light" ],
  46. 0x1004 => [ "EMW200", "light"],
  47. 0x1005 => [ "IMPULS", "light"],
  48. 0x1006 => [ "RISINGSUN", "light"],
  49. 0x1007 => [ "PHILIPS_SBC", "light"],
  50. 0x1008 => [ "ENER010", "light"], # Energenie ENER010: untested
  51. 0x1009 => [ "ENER5", "light"], # Energenie 5-gang: untested
  52. 0x100A => [ "COCO_GDR2", "light"], # COCO GDR2-2000R: untestedCOCO
  53. # 0x11: Lighting2
  54. 0x1100 => [ "AC", "light"],
  55. 0x1101 => [ "HOMEEASY", "light"],
  56. 0x1102 => [ "ANSLUT", "light"],
  57. # 0x12: Lighting3
  58. 0x1200 => [ "KOPPLA", "light"], # IKEA Koppla
  59. # 0x13: Lighting4
  60. 0x1300 => [ "PT2262", "light"], # PT2262 raw messages
  61. # 0x14: Lighting5
  62. 0x1400 => [ "LIGHTWAVERF", "light"], # LightwaveRF
  63. 0x1401 => [ "EMW100", "light"], # EMW100
  64. 0x1402 => [ "BBSB", "light"], # BBSB
  65. 0x1403 => [ "MDREMOTE", "light"], # MDREMOTE LED dimmer
  66. 0x1404 => [ "RSL2", "light"], # Conrad RSL2
  67. 0x1405 => [ "LIVOLO", "light"], # Livolo
  68. 0x1406 => [ "TRC02", "light"], # RGB TRC02
  69. # 0x15: Lighting6
  70. 0x1500 => [ "BLYSS", "light"], # Blyss
  71. # 0x16: Chime
  72. 0x1600 => [ "BYRONSX", "light"], # Byron SX
  73. 0x1601 => [ "BYRONMP", "chime"], # Byron MP001
  74. 0x1602 => [ "SELECTPLUS", "chime"], # SelectPlus
  75. 0x1603 => [ "RFU", "chime"], # RFU
  76. 0x1604 => [ "ENVIVO", "chime"], # Envivo
  77. # 0x17: Fan
  78. 0x1700 => [ "SIEMENS_SF01", "light"], # Siemens SF01
  79. # 0x18: Curtain1
  80. 0x1800 => [ "HARRISON", "light"], # Harrison Curtain
  81. # 0x19: Blinds1
  82. 0x1900 => [ "ROLLER_TROL", "light"], # Roller Trol
  83. 0x1901 => [ "HASTA_OLD", "light"], # Hasta old
  84. 0x1902 => [ "AOK_RF01", "light"], # A-OK RF01
  85. 0x1903 => [ "AOK_AC114", "light"], # A-OK AC114
  86. 0x1904 => [ "RAEX_YR1326", "light"], # Raex YR1326
  87. 0x1905 => [ "MEDIA_MOUNT", "light"], # Media Mount
  88. 0x1906 => [ "DC106", "light"], # DC/RMF/Yooda
  89. 0x1907 => [ "FOREST", "light"], # Forest
  90. 0x1A00 => [ "RFY", "light"], # RTS RFY
  91. 0x1A01 => [ "RFY_ext", "light"], # RTS RFY ext
  92. );
  93. my %light_device_commands = ( # HEXSTRING => commands
  94. # 0x10: Lighting1
  95. 0x1000 => [ "off", "on", "dim", "bright", "", "all_off", "all_on"], # X10
  96. 0x1001 => [ "off", "on", "", "", "", "all_off", "all_on", "chime"], # ARC
  97. 0x1002 => [ "off", "on"], # AB400D
  98. 0x1003 => [ "off", "on"], # WAVEMAN
  99. 0x1004 => [ "off", "on"], # EMW200
  100. 0x1005 => [ "off", "on"], # IMPULS
  101. 0x1006 => [ "off", "on"], # RisingSun
  102. 0x1007 => [ "off", "on", "", "", "", "all_off", "all_on"], # Philips SBC
  103. 0x1008 => [ "off", "on", "", "", "", "all_off", "all_on"], # Energenie ENER010
  104. 0x1009 => [ "off", "on"], # Energenie 5-gang
  105. 0x100A => [ "off", "on"], # COCO GDR2-2000R
  106. # 0x11: Lighting2
  107. 0x1100 => [ "off", "on", "level", "all_off", "all_on", "all_level"], # AC
  108. 0x1101 => [ "off", "on", "level", "all_off", "all_on", "all_level"], # HOMEEASY
  109. 0x1102 => [ "off", "on", "level", "all_off", "all_on", "all_level"], # ANSLUT
  110. # 0x12: Lighting3
  111. 0x1200 => [ "bright", "", "", "", "", "", "", "", "dim", "", "", "", "", "", "", "",
  112. "on", "level1", "level2", "level3", "level4", "level5", "level6", "level7", "level8", "level9", "off", "", "program", "", "", "", "",], # Koppla
  113. # 0x13: Lighting4
  114. 0x1300 => [ "Lighting4"], # Lighting4: PT2262
  115. # 0x14: Lighting5
  116. 0x1400 => [ "off", "on", "all_off", "mood1", "mood2", "mood3", "mood4", "mood5", "reserved1", "reserved2", "unlock", "lock", "all_lock", "close", "stop", "open", "level"], # LightwaveRF, Siemens
  117. 0x1401 => [ "off", "on", "learn"], # EMW100 GAO/Everflourish
  118. 0x1402 => [ "off", "on", "all_off", "all_on"], # BBSB new types
  119. 0x1403 => [ "power", "light", "bright", "dim", "100", "50", "25", "mode+", "speed-", "speed+", "mode-"], # MDREMOTE
  120. 0x1404 => [ "off", "on", "all_off", "all_on"], # Conrad RSL
  121. 0x1405 => [ "all_off", "on_off", "dim+", "dim-"], # Livolo
  122. 0x1406 => [ "off", "on", "bright", "dim", "vivid", "pale", "color"], # TRC02
  123. # 0x15: Lighting6
  124. 0x1500 => [ "on", "off", "all_on", "all_off"], # Blyss
  125. # 0x16: Chime
  126. 0x1600 => [ "", "tubular3_1", "solo1", "bigben1", "", "tubular2_1", "tubular2_2", "", "dingdong", "solo2", " ", "", "", "tubular3_2"], # Byron SX
  127. 0x1601 => [ "ring"], # Byron MP001
  128. 0x1602 => [ "ring"], # SelectPlus
  129. 0x1603 => [ "ring"], # RFU
  130. 0x1604 => [ "ring"], # Envivo
  131. # 0x17: Fan
  132. 0x1700 => [ "", "timer", "-", "learn", "+", "confirm", "light", "on", "off"], # Siemens SF01
  133. # 0x18: Curtain1
  134. 0x1800 => [ "open", "close", "stop", "program"], # Harrison Curtain
  135. # 0x19: Blinds1
  136. 0x1900 => [ "open", "close", "stop", "confirm_pair", "set_limit"], # Roller Trol
  137. 0x1901 => [ "open", "close", "stop", "confirm_pair", "set_limit"], # Hasta old
  138. 0x1902 => [ "open", "close", "stop", "confirm_pair"], # A-OK RF01
  139. 0x1903 => [ "open", "close", "stop", "confirm_pair"], # A-OK AC114
  140. 0x1904 => [ "open", "close", "stop", "confirm_pair", "set_upper_limit", "set_lower_limit", "delete_limits", "change_dir", "left", "right"], # Raex YR1326
  141. 0x1905 => [ "down", "up", "stop"], # Media Mount
  142. 0x1906 => [ "open", "close", "stop", "confirm"], # DC/RMF/Yooda
  143. 0x1907 => [ "open", "close", "stop", "confirm_pair"], # Forest
  144. 0x1A00 => [ "stop", "up", "", "down", "", "", "", "program"], # RTS RFY
  145. 0x1A01 => [ "stop", "up", "", "down", "", "", "", "program"], # RTS RFY ext
  146. );
  147. my %light_device_c2b; # DEVICE_TYPE->hash (reverse of light_device_codes)
  148. sub
  149. TRX_LIGHT_Initialize($)
  150. {
  151. my ($hash) = @_;
  152. foreach my $k (keys %light_device_codes) {
  153. $light_device_c2b{$light_device_codes{$k}->[0]} = $k;
  154. }
  155. $hash->{Match} = "^..(10|11|12|13|14|15|16|17|18|19|1A).*";
  156. $hash->{SetFn} = "TRX_LIGHT_Set";
  157. $hash->{DefFn} = "TRX_LIGHT_Define";
  158. $hash->{UndefFn} = "TRX_LIGHT_Undef";
  159. $hash->{ParseFn} = "TRX_LIGHT_Parse";
  160. $hash->{AttrList} = "IODev ignore:1,0 do_not_notify:1,0 repeat ".
  161. $readingFnAttributes;
  162. }
  163. #####################################
  164. sub
  165. TRX_LIGHT_SetState($$$$)
  166. {
  167. my ($hash, $tim, $vt, $val) = @_;
  168. $val = $1 if($val =~ m/^(.*) \d+$/);
  169. # to be done. Just accept everything right now.
  170. #return "Undefined value $val" if(!defined($fs20_c2b{$val}));
  171. return undef;
  172. }
  173. #############################
  174. sub
  175. TRX_LIGHT_Do_On_Till($@)
  176. {
  177. my ($hash, @a) = @_;
  178. return "Timespec (HH:MM[:SS]) needed for the on-till command" if(@a != 3);
  179. my ($err, $hr, $min, $sec, $fn) = GetTimeSpec($a[2]);
  180. return $err if($err);
  181. my @lt = localtime;
  182. my $hms_till = sprintf("%02d:%02d:%02d", $hr, $min, $sec);
  183. my $hms_now = sprintf("%02d:%02d:%02d", $lt[2], $lt[1], $lt[0]);
  184. if($hms_now ge $hms_till) {
  185. Log3 $hash, 4, "on-till: won't switch as now ($hms_now) is later than $hms_till";
  186. return "";
  187. }
  188. my $tname = $hash->{NAME} . "_timer";
  189. CommandDelete(undef, $tname) if($defs{$tname});
  190. my @b = ($a[0], "on");
  191. TRX_LIGHT_Set($hash, @b);
  192. CommandDefine(undef, "$tname at $hms_till set $a[0] off");
  193. }
  194. #############################
  195. sub
  196. TRX_LIGHT_Do_On_For_Timer($@)
  197. {
  198. my ($hash, @a) = @_;
  199. return "Timespec (HH:MM[:SS]) needed for the on-for-timer command" if(@a != 3);
  200. my ($err, $hr, $min, $sec, $fn) = GetTimeSpec($a[2]);
  201. return $err if($err);
  202. my $hms_for_timer = sprintf("+%02d:%02d:%02d", $hr, $min, $sec);
  203. my $tname = $hash->{NAME} . "_timer";
  204. CommandDelete(undef, $tname) if($defs{$tname});
  205. my @b = ($a[0], "on");
  206. TRX_LIGHT_Set($hash, @b);
  207. CommandDefine(undef, "$tname at $hms_for_timer set $a[0] off");
  208. }
  209. ###################################
  210. sub
  211. TRX_LIGHT_Set($@)
  212. {
  213. my ($hash, @a) = @_;
  214. my $ret = undef;
  215. my $na = int(@a);
  216. return "no set value specified" if($na < 2 || $na > 3);
  217. # look for device_type
  218. my $name = $a[0];
  219. my $command = $a[1];
  220. my $command_state = $a[1];
  221. my $level = 0;
  222. my $color = 0;
  223. my $arg3 = "";
  224. # special for on-till
  225. return TRX_LIGHT_Do_On_Till($hash, @a) if($command eq "on-till");
  226. # special for on-for-timer
  227. return TRX_LIGHT_Do_On_For_Timer($hash, @a) if($command eq "on-for-timer");
  228. if ($na == 3) {
  229. $arg3 = $a[2];
  230. if ($na == 3 && $command eq "level" ) {
  231. $level = $a[2];
  232. }
  233. elsif ($na == 3 && $command eq "color" ) {
  234. $color = $a[2];
  235. }
  236. }
  237. my $device_type = $hash->{TRX_LIGHT_type};
  238. my $deviceid = $hash->{TRX_LIGHT_deviceid};
  239. if ($device_type eq "MS14A") {
  240. return "No set implemented for $device_type";
  241. }
  242. if ( lc($hash->{TRX_LIGHT_devicelog}) eq "window" || lc($hash->{TRX_LIGHT_devicelog}) eq "door" ||
  243. lc($hash->{TRX_LIGHT_devicelog}) eq "motion" ||
  244. lc($hash->{TRX_LIGHT_devicelog}) eq "ring" ||
  245. lc($hash->{TRX_LIGHT_devicelog}) eq "lightsensor" || lc($hash->{TRX_LIGHT_devicelog}) eq "photosensor" ||
  246. lc($hash->{TRX_LIGHT_devicelog}) eq "lock"
  247. ) {
  248. return "No set implemented for $device_type";
  249. }
  250. my $device_type_num = $light_device_c2b{$device_type};
  251. if(!defined($device_type_num)) {
  252. return "Unknown device_type, choose one of " .
  253. join(" ", sort keys %light_device_c2b);
  254. }
  255. my $protocol_type = $device_type_num >> 8; # high bytes
  256. # Now check if the command is valid and retrieve the command id:
  257. my $rec = $light_device_commands{$device_type_num};
  258. my $i;
  259. for ($i=0; $i <= $#$rec && ($rec->[$i] ne $command); $i++) { ;}
  260. if($rec->[0] eq "Lighting4") { # for Lighting4
  261. my $command_codes = $hash->{TRX_LIGHT_commandcodes}.",";
  262. my $l = $command_codes;
  263. $l =~ s/([0-9]*):([a-z]*),/$2 /g;
  264. Log3 $name, 5,"TRX_LIGHT_Set() PT2262: l=$l";
  265. if (!($command =~ /^[0-2]*$/)) { # if it is base4 just accept it
  266. if ($command ne "?" && $command_codes =~ /([0-9]*):$command,/ ) {
  267. Log3 $name, 5,"TRX_LIGHT_Set() PT2262: arg=$command found=$1";
  268. $command = $1;
  269. } else {
  270. Log3 $name, 5,"TRX_LIGHT_Set() PT2262: else arg=$command l='$l'";
  271. my $error = "Unknown command $command, choose one of $l ";
  272. Log3 $name, 1, "TRX_LIGHT_Set() PT2262".$error if ($command ne "?");
  273. return $error;
  274. }
  275. }
  276. } elsif ($i > $#$rec) {
  277. my $l = join(" ", sort @$rec);
  278. if ($device_type eq "AC" || $device_type eq "HOMEEASY" || $device_type eq "ANSLUT") {
  279. $l =~ s/ level / level:slider,0,1,15 /;
  280. }
  281. elsif ($device_type eq "TRC02") {
  282. $l =~ s/ color / color:slider,14,2,128 /;
  283. }
  284. #my $error = "Unknown command $command, choose one of $l";
  285. my $error = "Unknown command $command, choose one of $l "."on-till on-for-timer";
  286. Log3 $name, 1, "TRX_LIGHT_Set()".$error if ($command ne "?");
  287. return $error;
  288. }
  289. my $seqnr = 0;
  290. my $cmnd = $i;
  291. my $hex_prefix;
  292. my $hex_command;
  293. if ($protocol_type == 0x10) {
  294. my $house;
  295. my $unit;
  296. if ($deviceid =~ /(.)(.*)/ ) {
  297. $house = ord("$1");
  298. $unit = $2;
  299. } else {
  300. Log3 $name, 1, "TRX_LIGHT_Set() lighting1 wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  301. return "error set name=$name deviceid=$deviceid";
  302. }
  303. # lighting1
  304. $hex_prefix = sprintf "0710";
  305. $hex_command = sprintf "%02x%02x%02x%02x%02x00", $device_type_num & 0xff, $seqnr, $house, $unit, $cmnd;
  306. Log3 $name, 5, "TRX_LIGHT_Set() name=$name device_type=$device_type, deviceid=$deviceid house=$house, unit=$unit command=$command";
  307. Log3 $name, 5,"TRX_LIGHT_Set hexline=$hex_prefix$hex_command";
  308. } elsif ($protocol_type == 0x11) {
  309. # lighting2
  310. if (uc($deviceid) =~ /^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/ ) {
  311. ;
  312. } else {
  313. Log3 $name, 1,"TRX_LIGHT_Set() lighting2 wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  314. return "error set name=$name deviceid=$deviceid";
  315. }
  316. $hex_prefix = sprintf "0B11";
  317. $hex_command = sprintf "%02x%02x%s%02x%02x00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd, $level;
  318. if ($command eq "level") {
  319. $command .= sprintf " %d", $level;
  320. $command_state = $command;
  321. }
  322. Log3 $name, 5, "TRX_LIGHT_Set() lighting2 name=$name device_type=$device_type, deviceid=$deviceid command=$command";
  323. Log3 $name, 5, "TRX_LIGHT_Set() lighting2 hexline=$hex_prefix$hex_command";
  324. } elsif ($protocol_type == 0x12) {
  325. # lighting3
  326. my $koppla_id = "";
  327. if (uc($deviceid) =~ /^([0-1][0-9])([0-9A-F])([0-9A-F][0-9A-F])$/ ) {
  328. # $1 = system, $2 = high bits channel, $3 = low bits channel
  329. my $koppla_system = $1 - 1;
  330. if ($koppla_system > 15) {
  331. return "error set name=$name deviceid=$deviceid. system must be in range 01-16";
  332. }
  333. $koppla_id = sprintf("%02X",$koppla_system).$3."0".$2;
  334. Log3 $name, 5,"TRX_LIGHT_Set() lighting3: deviceid=$deviceid kopplaid=$koppla_id";
  335. } else {
  336. Log3 $name, 1,"TRX_LIGHT_Set() lighting3 wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid. Wrong deviceid must be 3 digits with range 0000 - f3FF";
  337. return "error set name=$name deviceid=$deviceid. Wrong deviceid must be 5 digits consisting of 2 digit decimal value for system (01-16) and a 3 hex digit channel (000 - 3ff)";
  338. }
  339. $hex_prefix = sprintf "0812";
  340. $hex_command = sprintf "%02x%02x%s%02x00", $device_type_num & 0xff, $seqnr, $koppla_id, $cmnd;
  341. if ($command eq "level") {
  342. $command .= sprintf " %d", $level;
  343. $command_state = $command;
  344. }
  345. Log3 $name, 5,"TRX_LIGHT_Set() lighting3 name=$name device_type=$device_type, deviceid=$deviceid command=$command";
  346. Log3 $name, 5,"TRX_LIGHT_Set() lighting3 hexline=$hex_prefix$hex_command";
  347. } elsif ($protocol_type == 0x13) {
  348. # lighting4 (PT2262)
  349. my $pt2262_cmd;
  350. my $base_4;
  351. my $bindata;
  352. my $hexdata;
  353. if ($command =~ /^[0-3]*$/) { # if it is base4 just append it
  354. $pt2262_cmd = $deviceid.$command;
  355. } else {
  356. return "TRX_LIGHT_Set() PT2262: cmd=$command name=$name not found";
  357. }
  358. if (uc($pt2262_cmd) =~ /^[0-3][0-3][0-3][0-3][0-3][0-3][0-3][0-3][0-3][0-3][0-3][0-3]$/ ) {
  359. $base_4 = $pt2262_cmd;
  360. # convert base4 to binary:
  361. my %b42b = (0 => "00", 1 => "01", 2 => "10", 3 => "11");
  362. ($bindata = $base_4) =~ s/(.)/$b42b{lc $1}/g;
  363. $hexdata = unpack("H*", pack("B*", $bindata));
  364. Log3 $name, 5,"TRX_LIGHT_Set() PT2262: base4='$base_4', binary='$bindata' hex='$hexdata'";
  365. } else {
  366. Log3 $name, 5,"TRX_LIGHT_Set() lighting4:PT2262 cmd='$pt2262_cmd' needs to be base4 and has 12 digits (name=$name device_type=$device_type, deviceid=$deviceid)";
  367. return "error set name=$name deviceid=$pt2262_cmd (needs to be base4 and has 12 digits)";
  368. }
  369. $hex_prefix = sprintf "0913";
  370. $hex_command = sprintf "00%02x%s015E00", $seqnr, $hexdata;
  371. Log3 $name, 5, "TRX_LIGHT_Set() lighting4:PT2262 name=$name cmd=$command cmd_state=$command_state hexdata=$hexdata";
  372. Log3 $name, 5, "TRX_LIGHT_Set() lighting4:PT2262 hexline=$hex_prefix$hex_command";
  373. } elsif ($protocol_type == 0x14) {
  374. # lighting5
  375. if (uc($deviceid) =~ /^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/ ) {
  376. ;
  377. } else {
  378. Log3 $name, 1, "TRX_LIGHT_Set() lighting5 wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  379. return "error set name=$name deviceid=$deviceid";
  380. }
  381. if ($command eq "color") {
  382. $cmnd = $color;
  383. $command .= sprintf " %d", $color;
  384. $command_state = $command;
  385. }
  386. $hex_prefix = sprintf "0A14";
  387. $hex_command = sprintf "%02x%02x%s%02x%02x00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd, $level;
  388. if ($command eq "level") {
  389. $command .= sprintf " %d", $level;
  390. $command_state = $command;
  391. }
  392. Log3 $name, 5, "TRX_LIGHT_Set() lighting5 name=$name device_type=$device_type, deviceid=$deviceid command=$command";
  393. Log3 $name, 5, "TRX_LIGHT_Set() lighting5 hexline=$hex_prefix$hex_command";
  394. } elsif ($protocol_type == 0x16) {
  395. # Chime
  396. if (uc($deviceid) =~ /^[0-9A-F][0-9A-F]$/ ) {
  397. $hex_command = sprintf "%02x%02x00%s%02x00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd;
  398. } elsif (uc($deviceid) =~ /^[0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/ ) {
  399. $hex_command = sprintf "%02x%02x00%s00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd;
  400. } elsif (uc($deviceid) =~ /^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/ ) {
  401. $hex_command = sprintf "%02x%02x%s00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd;
  402. } else {
  403. Log3 $name, 1, "TRX_LIGHT_Set() chime wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  404. return "error set name=$name deviceid=$deviceid";
  405. }
  406. $hex_prefix = sprintf "0716";
  407. Log3 $name, 5, "TRX_LIGHT_Set() chime name=$name device_type=$device_type, deviceid=$deviceid command=$command";
  408. Log3 $name, 5, "TRX_LIGHT_Set() chime hexline=$hex_prefix$hex_command";
  409. } elsif ($protocol_type == 0x17) {
  410. # fan
  411. if (uc($deviceid) =~ /^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/ ) {
  412. ;
  413. } else {
  414. Log3 $name, 1, "TRX_LIGHT_Set() fan wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  415. return "error set name=$name deviceid=$deviceid";
  416. }
  417. $hex_prefix = sprintf "0717";
  418. $hex_command = sprintf "%02x%02x%s%02xx00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd;
  419. Log3 $name, 5, "TRX_LIGHT_Set() fan name=$name device_type=$device_type, deviceid=$deviceid command=$command";
  420. Log3 $name, 5, "TRX_LIGHT_Set() fan hexline=$hex_prefix$hex_command";
  421. } elsif ($protocol_type == 0x18) {
  422. # curtain1
  423. my $house;
  424. my $unit;
  425. if ($deviceid =~ /(.)(.*)/ ) {
  426. $house = ord("$1");
  427. $unit = $2;
  428. } else {
  429. Log3 $name, 1, "TRX_LIGHT_Set() curtain1 wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  430. return "error set name=$name deviceid=$deviceid";
  431. }
  432. $hex_prefix = sprintf "0718";
  433. $hex_command = sprintf "%02x%02x%02x%02x%02x00", $device_type_num & 0xff, $seqnr, $house, $unit, $cmnd;
  434. Log3 $name, 5, "TRX_LIGHT_Set() curtain1 name=$name device_type=$device_type, deviceid=$deviceid house=$house, unit=$unit command=$command";
  435. Log3 $name, 5,"TRX_LIGHT_Set curtain1 hexline=$hex_prefix$hex_command";
  436. } elsif ($protocol_type == 0x19) {
  437. # Blinds1
  438. if (uc($deviceid) =~ /^[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]$/ ) {
  439. ;
  440. } else {
  441. Log3 $name, 1, "TRX_LIGHT_Set() Blinds1 wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  442. return "error set name=$name deviceid=$deviceid";
  443. }
  444. $hex_prefix = sprintf "0919";
  445. $hex_command = sprintf "%02x%02x%s%02x00", $device_type_num & 0xff, $seqnr, $deviceid, $cmnd;
  446. Log3 $name, 5, "TRX_LIGHT_Set() Blinds1 name=$name device_type=$device_type, deviceid=$deviceid command=$command";
  447. Log3 $name, 5, "TRX_LIGHT_Set() Blinds1 hexline=$hex_prefix$hex_command";
  448. } elsif ($protocol_type == 0x1A) {
  449. my $unitid;
  450. my $unitcode;
  451. if (uc($deviceid) =~ /^([0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F])(0[0-9A-F])$/ ) {
  452. $unitid = $1;
  453. $unitcode = $2;
  454. if (($device_type_num == 0x1A00) && !($unitcode =~ /^0[0-4]$/)) {
  455. Log3 $name, 1, "TRX_LIGHT_Set() RFY wrong unitcode: name=$name device_type=$device_type, unitid=$unitid unitcode=$unitcode";
  456. return "error set name=$name deviceid=$deviceid";
  457. }
  458. } else {
  459. Log3 $name, 1, "TRX_LIGHT_Set() RFY wrong deviceid: name=$name device_type=$device_type, deviceid=$deviceid";
  460. return "error set name=$name deviceid=$deviceid";
  461. }
  462. $hex_prefix = sprintf "0C1A";
  463. $hex_command = sprintf "%02x%02x%s%s%02x0000000000", $device_type_num & 0xff, $seqnr, $unitid, $unitcode, $cmnd;
  464. Log3 $name, 5, "TRX_LIGHT_Set() RFY name=$name device_type=$device_type, unitid=$unitid unitcode=$unitcode command=$command";
  465. Log3 $name, 5, "TRX_LIGHT_Set() RFY hexline=$hex_prefix$hex_command";
  466. } else {
  467. return "No set implemented for $device_type . Unknown protocol type";
  468. }
  469. for (my $repeat = $attr{$name}{repeat} || 1; $repeat >= 1; $repeat = $repeat - 1) {
  470. IOWrite($hash, $hex_prefix, $hex_command);
  471. }
  472. my $tn = TimeNow();
  473. $hash->{CHANGED}[0] = $command_state;
  474. $hash->{STATE} = $command_state;
  475. $hash->{READINGS}{state}{TIME} = $tn;
  476. $hash->{READINGS}{state}{VAL} = $command_state;
  477. return $ret;
  478. }
  479. #####################################
  480. sub
  481. TRX_LIGHT_Define($$)
  482. {
  483. my ($hash, $def) = @_;
  484. my @a = split("[ \t][ \t]*", $def);
  485. my $a = int(@a);
  486. my $type = "";
  487. my $deviceid = "";
  488. my $devicelog = "";
  489. my $commandcodes = "";
  490. if (int(@a) > 2 && uc($a[2]) eq "PT2262") {
  491. if (int(@a) != 3 && int(@a) != 6) {
  492. Log3 $hash, 1, "TRX_LIGHT_Define() wrong syntax '@a'. \nCorrect syntax is 'define <name> TRX_LIGHT PT2262 [<deviceid> <devicelog> <commandcodes>]'";
  493. return "wrong syntax: define <name> TRX_LIGHT PT2262 [<deviceid> <devicelog> [<commandcodes>]]";
  494. }
  495. } elsif(int(@a) != 5 && int(@a) != 7) {
  496. Log3 $hash, 1, "TRX_LIGHT_Define() wrong syntax '@a'. \nCorrect syntax is 'define <name> TRX_LIGHT <type> <deviceid> <devicelog> [<deviceid2> <devicelog2>]'";
  497. return "wrong syntax: define <name> TRX_LIGHT <type> <deviceid> <devicelog> [<deviceid2> <devicelog2>]";
  498. }
  499. my $name = $a[0];
  500. $type = uc($a[2]) if (int(@a) > 2);
  501. $deviceid = $a[3] if (int(@a) > 3);
  502. $devicelog = $a[4] if (int(@a) > 4);
  503. $commandcodes = $a[5] if ($type eq "PT2262" && int(@a) > 5);
  504. if ($type ne "X10" && $type ne "ARC" && $type ne "MS14A" && $type ne "AB400D" && $type ne "WAVEMAN" && $type ne "EMW200" && $type ne "IMPULS" && $type ne "RISINGSUN" && $type ne "PHILIPS_SBC" && $type ne "AC" && $type ne "HOMEEASY" && $type ne "ANSLUT" && $type ne "KOPPLA" && $type ne "LIGHTWAVERF" && $type ne "EMW100" && $type ne "BBSB" && $type ne "TRC02" && $type ne "PT2262" && $type ne "ENER010" && $type ne "ENER5" && $type ne "COCO_GDR2" && $type ne "MDREMOTE" && $type ne "RSL2" && $type ne "LIVOLO" && $type ne "BLYSS" && $type ne "BYRONSX" && $type ne "SIEMENS_SF01" && $type ne "HARRISON" && $type ne "ROLLER_TROL" && $type ne "HASTA_OLD" && $type ne "AOK_RF01" && $type ne "AOK_AC114" && $type ne "RAEX_YR1326" && $type ne "MEDIA_MOUNT" && $type ne "DC106" && $type ne "FOREST" && $type ne "RFY" && $type ne "RFY_ext" && $type ne "SELECTPLUS") {
  505. Log3 $name, 1,"TRX_LIGHT_Define() wrong type: $type";
  506. return "TRX_LIGHT: wrong type: $type";
  507. }
  508. my $my_type;
  509. if ($type eq "MS14A") {
  510. $my_type = "X10"; # device will be received as X10
  511. } else {
  512. $my_type = $type;
  513. }
  514. my $device_name = "TRX".$DOT.$my_type;
  515. if ($deviceid ne "") { $device_name .= $DOT.$deviceid };
  516. $hash->{TRX_LIGHT_deviceid} = $deviceid;
  517. $hash->{TRX_LIGHT_devicelog} = $devicelog;
  518. $hash->{TRX_LIGHT_commandcodes} = $commandcodes if ($type eq "PT2262");
  519. $hash->{TRX_LIGHT_type} = $type;
  520. #$hash->{TRX_LIGHT_CODE} = $deviceid;
  521. $modules{TRX_LIGHT}{defptr}{$device_name} = $hash;
  522. if (int(@a) == 7) {
  523. # there is a second deviceid:
  524. #
  525. my $deviceid2 = $a[5];
  526. my $devicelog2 = $a[6];
  527. my $device_name2 = "TRX".$DOT.$my_type.$DOT.$deviceid2;
  528. $hash->{TRX_LIGHT_deviceid2} = $deviceid2;
  529. $hash->{TRX_LIGHT_devicelog2} = $devicelog2;
  530. #$hash->{TRX_LIGHT_CODE2} = $deviceid2;
  531. $modules{TRX_LIGHT}{defptr2}{$device_name2} = $hash;
  532. }
  533. AssignIoPort($hash);
  534. return undef;
  535. }
  536. #####################################
  537. sub
  538. TRX_LIGHT_Undef($$)
  539. {
  540. my ($hash, $name) = @_;
  541. delete($modules{TRX_LIGHT}{defptr}{$name});
  542. return undef;
  543. }
  544. ############################################
  545. # T R X _ L I G H T _ p a r s e _ X 1 0 ( )
  546. #-------------------------------------------
  547. sub TRX_LIGHT_parse_X10 ($$)
  548. {
  549. my ($hash, $bytes) = @_;
  550. my $error = "";
  551. #my $device;
  552. my $type = $bytes->[0];
  553. my $subtype = $bytes->[1];
  554. my $dev_type;
  555. my $dev_reading;
  556. my $rest;
  557. my $type_subtype = ($type << 8) + $subtype;
  558. if (exists $light_device_codes{$type_subtype}) {
  559. my $rec = $light_device_codes{$type_subtype};
  560. ($dev_type, $dev_reading) = @$rec;
  561. } else {
  562. $error = sprintf "TRX_LIGHT: error undefined type=%02x, subtype=%02x", $type, $subtype;
  563. Log3 $hash, 1, "TRX_LIGHT_parse_X10() ".$error;
  564. return $error;
  565. }
  566. if ($dev_type eq "BBSB") { return " "; } # ignore BBSB messages temporarily because of receiving problems
  567. my $device;
  568. my $data;
  569. if ($type == 0x10 || $type == 0x18) {
  570. my $dev_first = "?";
  571. my %x10_housecode =
  572. (
  573. 0x41 => "A",
  574. 0x42 => "B",
  575. 0x43 => "C",
  576. 0x44 => "D",
  577. 0x45 => "E",
  578. 0x46 => "F",
  579. 0x47 => "G",
  580. 0x48 => "H",
  581. 0x49 => "I",
  582. 0x4A => "J",
  583. 0x4B => "K",
  584. 0x4C => "L",
  585. 0x4D => "M",
  586. 0x4E => "N",
  587. 0x4F => "O",
  588. 0x50 => "P",
  589. );
  590. my $devnr = $bytes->[3]; # housecode
  591. if (exists $x10_housecode{$devnr}) {
  592. $dev_first = $x10_housecode{$devnr};
  593. } else {
  594. $error = sprintf "TRX_LIGHT: x10_housecode wrong housecode=%02x", $devnr;
  595. Log3 $hash, 1, "TRX_LIGHT_parse_X10() ".$error;
  596. return $error;
  597. }
  598. my $unit = $bytes->[4]; # unitcode
  599. $device = sprintf '%s%0d', $dev_first, $unit;
  600. $data = $bytes->[5];
  601. } elsif ($type == 0x11) {
  602. $device = sprintf '%02x%02x%02x%02x%02x', $bytes->[3], $bytes->[4], $bytes->[5], $bytes->[6], $bytes->[7];
  603. $data = $bytes->[8];
  604. } elsif ($type == 0x14) {
  605. $device = sprintf '%02x%02x%02x%02x', $bytes->[3], $bytes->[4], $bytes->[5], $bytes->[6];
  606. $data = $bytes->[7];
  607. } elsif ($type == 0x15) {
  608. $device = sprintf '%02x%02x%c%d', $bytes->[3], $bytes->[4], $bytes->[5], $bytes->[6];
  609. $data = $bytes->[7];
  610. } elsif ($type == 0x16) { #Chime
  611. if ($subtype == 0x00) {
  612. $device = sprintf '%02x', $bytes->[4];
  613. $data = $bytes->[5];
  614. } else {
  615. $device = sprintf '%02x', $bytes->[3], $bytes->[4];
  616. $data = 0;
  617. }
  618. } elsif ($type == 0x17) { # Fan
  619. $device = sprintf '%02x%02x%02x', $bytes->[3], $bytes->[4], $bytes->[5];
  620. $data = $bytes->[6];
  621. } elsif ($type == 0x19) { # Blinds1
  622. $device = sprintf '%02x%02x%02x%02x', $bytes->[3], $bytes->[4], $bytes->[5], $bytes->[6];
  623. $data = $bytes->[7];
  624. } elsif ($type == 0x1A) { # RFY
  625. $device = printf '%02x%02x%02x%02x', $bytes->[3], $bytes->[4], $bytes->[5], $bytes->[6];
  626. $data = $bytes->[7];
  627. } else {
  628. $error = sprintf "TRX_LIGHT: wrong type=%02x", $type;
  629. Log3 $hash, 1, "TRX_LIGHT_parse_X10() ".$error;
  630. return $error;
  631. }
  632. my $hexdata = sprintf '%02x', $data;
  633. my $command = "";
  634. if (exists $light_device_commands{$type_subtype}) {
  635. my $code = $light_device_commands{$type_subtype};
  636. if (exists $code->[$data]) {
  637. $command = $code->[$data];
  638. } else {
  639. $error = sprintf "TRX_LIGHT: unknown cmd type_subtype=%02x cmd=%02x", $type_subtype, $data;
  640. Log3 $hash, 1, "TRX_LIGHT_parse_X10() ".$error;
  641. return $error;
  642. }
  643. } else {
  644. $error = sprintf "TRX_LIGHT: unknown type_subtype %02x data=%02x", $type_subtype, $data;
  645. Log3 $hash, 1, "TRX_LIGHT_parse_X10() ".$error;
  646. return $error;
  647. }
  648. #my @res;
  649. my $current = "";
  650. #--------------
  651. my $device_name = "TRX".$DOT.$dev_type.$DOT.$device;
  652. Log3 $hash, 5, "TRX_LIGHT: device_name=$device_name data=$hexdata";
  653. my $firstdevice = 1;
  654. my $def = $modules{TRX_LIGHT}{defptr}{$device_name};
  655. if(!$def) {
  656. $firstdevice = 0;
  657. $def = $modules{TRX_LIGHT}{defptr2}{$device_name};
  658. if (!$def) {
  659. Log3 $hash, 5, "TRX_LIGHT_parse_X10() UNDEFINED $device_name TRX_LIGHT $dev_type $device $dev_reading";
  660. Log3 $hash, 3, "TRX_LIGHT_parse_X10() Unknown device $device_name, please define it";
  661. return "UNDEFINED $device_name TRX_LIGHT $dev_type $device $dev_reading";
  662. }
  663. }
  664. # Use $def->{NAME}, because the device may be renamed:
  665. my $name = $def->{NAME};
  666. return "" if(IsIgnored($name));
  667. Log3 $name, 5, "TRX_LIGHT_parse_X10() $name devn=$device_name first=$firstdevice command=$command, cmd=$hexdata";
  668. my $n = 0;
  669. my $tm = TimeNow();
  670. my $val = "";
  671. my $device_type = $def->{TRX_LIGHT_type};
  672. my $sensor = "";
  673. if ($device_type eq "MS14A") {
  674. # for ms14a behave like x10
  675. $device_type = "X10";
  676. }
  677. if (lc($def->{TRX_LIGHT_devicelog}) eq "window" || lc($def->{TRX_LIGHT_devicelog}) eq "door") {
  678. $command = ($command eq "on") ? "Open" : "Closed" ;
  679. } elsif (lc($def->{TRX_LIGHT_devicelog}) eq "motion") {
  680. $command = ($command eq "on") ? "alert" : "normal" ;
  681. } elsif (lc($def->{TRX_LIGHT_devicelog}) eq "lightsensor" || lc($def->{TRX_LIGHT_devicelog}) eq "photosensor") {
  682. $command = ($command eq "on") ? "dark" : "bright" ;
  683. } elsif (lc($def->{TRX_LIGHT_devicelog}) eq "lock") {
  684. $command = ($command eq "on") ? "Closed" : "Open" ;
  685. } elsif (lc($def->{TRX_LIGHT_devicelog}) eq "ring") {
  686. $command = ($command eq "on") ? "normal" : "alert" ;
  687. }
  688. readingsBeginUpdate($def);
  689. if ($type == 0x10 || $type == 0x11 || $type == 0x14 || $type == 0x16) {
  690. # try to use it for all types:
  691. $current = $command;
  692. if ($type == 0x11 && $command eq "level") {
  693. # append level number
  694. my $level = $bytes->[9];
  695. $current .= sprintf " %d", $level;
  696. } elsif ($type == 0x14 && $command eq "level") {
  697. # append level number
  698. my $level = $bytes->[8];
  699. $current .= sprintf " %d", $level;
  700. }
  701. $sensor = $firstdevice == 1 ? $def->{TRX_LIGHT_devicelog} : $def->{TRX_LIGHT_devicelog2};
  702. $val .= $current;
  703. if ($sensor ne "none") { readingsBulkUpdate($def, $sensor, $current); }
  704. } else {
  705. $error = sprintf "TRX_LIGHT: error unknown sensor type=%x device_type=%s devn=%s first=%d command=%s", $type, $device_type, $device_name, $firstdevice, $command;
  706. Log3 $name, 1, "TRX_LIGHT_parse_X10() ".$error;
  707. return $error;
  708. }
  709. if (($firstdevice == 1) && $val) {
  710. #$def->{STATE} = $val;
  711. readingsBulkUpdate($def, "state", $val);
  712. }
  713. readingsEndUpdate($def, 1);
  714. return $name;
  715. }
  716. ########################################################
  717. # T R X _ L I G H T _ p a r s e _ P T 2 2 6 2 ( )
  718. #-------------------------------------------------------
  719. sub TRX_LIGHT_parse_PT2262 ($$)
  720. {
  721. my ($hash, $bytes) = @_;
  722. my $error = "";
  723. #my $device;
  724. my $type = $bytes->[0];
  725. my $subtype = $bytes->[1];
  726. my $dev_type;
  727. my $dev_reading;
  728. my $rest;
  729. my $type_subtype = ($type << 8) + $subtype;
  730. if (exists $light_device_codes{$type_subtype}) {
  731. my $rec = $light_device_codes{$type_subtype};
  732. ($dev_type, $dev_reading) = @$rec;
  733. } else {
  734. $error = sprintf "TRX_LIGHT: PT2262 error undefined type=%02x, subtype=%02x", $type, $subtype;
  735. Log3 $hash, 1, "TRX_LIGHT_parse_PT2262() ".$error;
  736. return $error;
  737. }
  738. my $device;
  739. $device = "";
  740. my $command = "error";
  741. my $current = "";
  742. my $hexdata = sprintf '%02x%02x%02x', $bytes->[3], $bytes->[4], $bytes->[5];
  743. my $hex_length = length($hexdata);
  744. my $bin_length = $hex_length * 4;
  745. my $bindata = unpack("B$bin_length", pack("H$hex_length", $hexdata));
  746. #my @a = ($bindata =~ /[0-1]{2}/g);
  747. my $base_4 = $bindata;
  748. $base_4 =~ s/(.)(.)/$1*2+$2/eg;
  749. my $codes = $base_4;
  750. #$codes =~ tr/0123/UMED/; # Up,Middle,Error,Down
  751. $codes =~ s/0/up /g; #
  752. $codes =~ s/1/middle /g; #
  753. $codes =~ s/2/err /g; #
  754. $codes =~ s/3/down /g; #
  755. my $device_name = "TRX".$DOT.$dev_type;
  756. my $command_codes = "";
  757. my $command_rest = "";
  758. my $def;
  759. # look for defined device with longest ID matching first:
  760. for (my $i=11; $i>0;$i--){
  761. if ($modules{TRX_LIGHT}{defptr}{$device_name.$DOT.substr($base_4,0,$i)}) {
  762. $device = substr($base_4,0,$i);
  763. $def = $modules{TRX_LIGHT}{defptr}{$device_name.$DOT.substr($base_4,0,$i)};
  764. $command_codes = $def->{TRX_LIGHT_commandcodes};
  765. $command_rest = substr($base_4,$i);
  766. Log3 $hash, 5, "TRX_LIGHT_parse_PT2262() found device_name=$device_name i=$i code=$base_4 commandcodes='$command_codes' command_rest='$command_rest' ";
  767. }
  768. }
  769. #--------------
  770. if ($device ne "") {
  771. # found a device
  772. Log3 $hash, 5, "TRX_LIGHT: PT2262 found device_name=$device_name data=$hexdata";
  773. $device_name .= $DOT.$device
  774. } else {
  775. # no specific device found. Using generic one:
  776. Log3 $hash, 5, "TRX_LIGHT: PT2262 device_name=$device_name data=$hexdata";
  777. $def = $modules{TRX_LIGHT}{defptr}{$device_name};
  778. if(!$def) {
  779. $dev_reading = "";
  780. Log3 $hash, 5, "TRX_LIGHT_parse_PT2262() UNDEFINED $device_name TRX_LIGHT $dev_type $device $dev_reading";
  781. Log3 $hash, 3, "TRX_LIGHT_parse_PT2262() Unknown device $device_name, please define it";
  782. return "UNDEFINED $device_name TRX_LIGHT $dev_type $device $dev_reading";
  783. }
  784. }
  785. # Use $def->{NAME}, because the device may be renamed:
  786. my $name = $def->{NAME};
  787. return "" if(IsIgnored($name));
  788. Log3 $name, 5, "TRX_LIGHT_parse_PT2262() $name devn=$device_name command=$command, cmd=$hexdata";
  789. my $n = 0;
  790. my $val = "";
  791. my $device_type = $def->{TRX_LIGHT_type};
  792. my $sensor = $def->{TRX_LIGHT_devicelog};
  793. readingsBeginUpdate($def);
  794. $current = $command;
  795. if ($device eq "") {
  796. #readingsBulkUpdate($def, "hex", $hexdata);
  797. #readingsBulkUpdate($def, "bin", $bindata);
  798. #readingsBulkUpdate($def, "base_4", $base_4);
  799. #readingsBulkUpdate($def, "codes", $codes);
  800. $val = $base_4;
  801. } else {
  802. # look for command code:
  803. $command_codes .= ",";
  804. #if ($command_codes =~ /$command_rest:(.*),/o ) {
  805. if ($command_codes =~ /$command_rest:([a-z|A-Z]*),/ ) {
  806. Log3 $name, 5,"PT2262: found=$1";
  807. $command = $1;
  808. }
  809. Log3 $name, 5,"TRX_LIGHT_parse_PT2262() readingsBulkUpdate($def, $sensor, $command)";
  810. $val = $command;
  811. if ($sensor ne "none") { readingsBulkUpdate($def, $sensor, $val); }
  812. }
  813. readingsBulkUpdate($def, "state", $val);
  814. readingsEndUpdate($def, 1);
  815. return $name;
  816. }
  817. ####################################
  818. # T R X _ L I G H T _ P a r s e ( )
  819. #-----------------------------------
  820. sub
  821. TRX_LIGHT_Parse($$)
  822. {
  823. my ($iohash, $hexline) = @_;
  824. my $time = time();
  825. # convert to binary
  826. my $msg = pack('H*', $hexline);
  827. if ($time_old ==0) {
  828. Log3 $iohash, 5, "TRX_LIGHT_Parse() decoding delay=0 hex=$hexline";
  829. } else {
  830. my $time_diff = $time - $time_old ;
  831. Log3 $iohash, 5, "TRX_LIGHT_Parse() decoding delay=$time_diff hex=$hexline";
  832. }
  833. $time_old = $time;
  834. # convert string to array of bytes. Skip length byte
  835. my @rfxcom_data_array = ();
  836. foreach (split(//, substr($msg,1))) {
  837. push (@rfxcom_data_array, ord($_) );
  838. }
  839. my $num_bytes = ord($msg);
  840. if ($num_bytes < 3) {
  841. return "";
  842. }
  843. my $type = $rfxcom_data_array[0];
  844. #Log3 $iohash, 5, "TRX_LIGHT: num_bytes=$num_bytes hex=$hexline type=$type";
  845. my $res = "";
  846. if ($type == 0x10 || $type == 0x11 || $type == 0x12 || $type == 0x14 || $type == 0x15 || $type == 0x16 || $type == 0x17 || $type == 0x18 || $type == 0x19) {
  847. Log3 $iohash, 5, "TRX_LIGHT_Parse() X10 num_bytes=$num_bytes hex=$hexline";
  848. $res = TRX_LIGHT_parse_X10($iohash, \@rfxcom_data_array);
  849. Log3 $iohash, 1, "TRX_LIGHT_Parse() unsupported hex=$hexline" if ($res eq "");
  850. return $res;
  851. } elsif ($type == 0x13) {
  852. Log3 $iohash, 5, "TRX_LIGHT_Parse()Lighting4/PT2262 num_bytes=$num_bytes hex=$hexline";
  853. $res = TRX_LIGHT_parse_PT2262($iohash, \@rfxcom_data_array);
  854. Log3 $iohash, 1, "TRX_LIGHT_Parse() unsupported hex=$hexline" if ($res eq "");
  855. return $res;
  856. } else {
  857. Log3 $iohash, 1, "TRX_LIGHT_Parse() not implemented num_bytes=$num_bytes hex=$hexline";
  858. }
  859. return "";
  860. }
  861. 1;
  862. =pod
  863. =begin html
  864. <a name="TRX_LIGHT"></a>
  865. <h3>TRX_LIGHT</h3>
  866. <ul>
  867. The TRX_LIGHT module receives and sends X10, ARC, ELRO AB400D, Waveman, Chacon EMW200, IMPULS, RisingSun, AC, HomeEasy EU and ANSLUT lighting devices (switches and remote control). Allows to send Philips SBC (receive not possible). ARC is a protocol used by devices from HomeEasy, KlikAanKlikUit, ByeByeStandBy, Intertechno, ELRO, AB600, Duewi, DomiaLite and COCO with address code wheels. AC is the protocol used by different brands with units having a learning mode button:
  868. KlikAanKlikUit, NEXA, CHACON, HomeEasy UK. <br> You need to define an RFXtrx433 transceiver receiver first.
  869. See <a href="#TRX">TRX</a>.
  870. <br><br>
  871. <a name="TRX_LIGHTdefine"></a>
  872. <b>Define</b>
  873. <ul>
  874. <code>define &lt;name&gt; TRX_LIGHT &lt;type&gt; &lt;deviceid&gt; &lt;devicelog&gt; [&lt;deviceid2&gt; &lt;devicelog2&gt;] </code> <br>
  875. <code>define &lt;name&gt; TRX_LIGHT PT2262 &lt;deviceid&gt; &lt;devicelog&gt; &lt;commandcodes&gt;</code> <br>
  876. <br>
  877. <code>&lt;type&gt;</code>
  878. <ul>
  879. specifies the type of the device: <br>
  880. Lighting devices:
  881. <ul>
  882. <li> <code>MS14A</code> (X10 motion sensor. Reports [normal|alert] on the first deviceid (motion sensor) and [on|off] for the second deviceid (light sensor)) </li>
  883. <li> <code>X10</code> (All other x10 devices. Report [off|on|dim|bright|all_off|all_on] on both deviceids.)</li>
  884. <li> <code>ARC</code> (ARC devices. ARC is a protocol used by devices from HomeEasy, KlikAanKlikUit, ByeByeStandBy, Intertechno, ELRO, AB600, Duewi, DomiaLite and COCO with address code wheels. Report [off|on|all_off|all_on|chime].)</li>
  885. <li> <code>AB400D</code> (ELRO AB400D devices. Report [off|on].)</li>
  886. <li> <code>WAVEMAN</code> (Waveman devices. Report [off|on].)</li>
  887. <li> <code>EMW200</code> (Chacon EMW200 devices. Report [off|on|all_off|all_on].)</li>
  888. <li> <code>IMPULS</code> (IMPULS devices. Report [off|on].)</li>
  889. <li> <code>RISINGSUN</code> (RisingSun devices. Report [off|on].)</li>
  890. <li> <code>PHILIPS_SBC</code> (Philips SBC devices. Send [off|on|all_off|all_on].)</li>
  891. <li> <code>ENER010</code> (Energenie ENER010 devices. deviceid: [A-P][1-4]. Send [off|on|all_off|all_on].)</li>
  892. <li> <code>ENER5</code> (Energenie 5-gang devices. deviceid: [A-P][1-10]. Send [off|on].)</li>
  893. <li> <code>COCO_GDR2</code> (ECOCO GDR2-2000R devices. deviceid: [A-D][1-4]. Send [off|on].)</li>
  894. <li> <code>AC</code> (AC devices. AC is the protocol used by different brands with units having a learning mode button: KlikAanKlikUit, NEXA, CHACON, HomeEasy UK. Report [off|on|level &lt;NUM&gt;|all_off|all_on|all_level &lt;NUM&gt;].)</li>
  895. <li> <code>HOMEEASY</code> (HomeEasy EU devices. Report [off|on|level|all_off|all_on|all_level].)</li>
  896. <li> <code>ANSLUT</code> (Anslut devices. Report [off|on|level|all_off|all_on|all_level].)</li>
  897. <li> <code>PT2262</code> (Devices using PT2262/PT2272 (coder/decoder) chip. To use this enable Lighting4 in RFXmngr. Please note that this disables ARC. For more information see <a href="http://www.fhemwiki.de/wiki/RFXtrx#PT2262_empfangen_und_senden_mit_TRX_LIGHT.pm">FHEM-Wiki</a>)</li>
  898. <li> <code>LIGHTWAVERF</code> (LightwaveRF devices. Commands ["off", "on", "all_off", "mood1", "mood2", "mood3", "mood4", "mood5", "reserved1", "reserved2", "unlock", "lock", "all_lock", "close", "stop", "open", "level"].)</li>
  899. <li> <code>EMW100</code> (EMW100 devices. Commands ["off", "on", "learn"].)</li>
  900. <li> <code>BBSB</code> (BBSB devices. Commands ["off", "on", "all_off", "all_on"].)</li>
  901. <li> <code>MDREMOTE</code> (MDREMOTE LED dimmer devices. Commands ["power", "light", "bright", "dim", "100", "50", "25", "mode+", "speed-", "speed+", "mode-"].)</li>
  902. <li> <code>RSL2</code> (Conrad RSL2 devices. Commands ["off", "on", "all_off", "all_on"].)</li>
  903. <li> <code>LIVOLO</code> (Livolo devices. Commands ["all_off", "on_off", "dim+", "dim-"].)</li>
  904. <li> <code>TRC02</code> (RGB TRC02 devices. Commands ["off", "on", "bright", "dim", "vivid", "pale", "color"].)</li>
  905. <li> <code>BLYSS</code> (Blyss devices. deviceid: [A-P][1-5]. Commands ["off", "on", "all_off", "all_on"].)</li>
  906. <li> <code>BYRONSX</code> (Byron SX chime devices. deviceid: 00-FF. Commands [ "tubular3_1", "solo1", "bigben1", "tubular2_1", "tubular2_2", "solo2", "tubular3_2"].)</li>
  907. <li> <code>SELECTPLUS</code> (SELECTPLUS] chime devices. deviceid: 0000-FFFF. Commands [ "ring"].)</li>
  908. <li> <code>SIEMENS_SF01</code> (Siemens SF01 devices. deviceid: 000000-007FFF. Commands [ "timer", "-", "learn", "+", "confirm", "light", "on", "off" ].)</li>
  909. <li> <code>HARRISON</code> (Harrison curtain devices. deviceid: 00-FF. Commands [ "open", "close", "stop", "program" ].)</li>
  910. <li> <code>ROLLER_TROL</code> (Roller Trol blind devices. deviceid: 00000100-00FFFF0F. Commands [ "open", "close", "stop", "confirm_pair", "set_limit" ].)</li>
  911. <li> <code>HASTA_OLD</code> (Hasta old blind devices. deviceid: 00000100-00FFFF0F. Commands [ "open", "close", "stop", "confirm_pair", "set_limit" ].)</li>
  912. <li> <code>AOK_RF01</code> (A-OK RF01 blind devices. deviceid: 00000100-FFFFFF0F. Commands [ "open", "close", "stop", "confirm_pair" ].)</li>
  913. <li> <code>AOK_AC114</code> (A-OK AC114 blind devices. deviceid: 00000100-FFFFFF0F. Commands [ "open", "close", "stop", "confirm_pair" ].)</li>
  914. <li> <code>RAEX_YR1326</code> (Raex YR1326 blind devices. deviceid: 00000100-FFFFFF0F. Commands [ "open", "close", "stop", "confirm_pair", "set_upper_limit", "set_lower_limit", "delete_limits", "change_dir", "left", "right"].)</li>
  915. <li> <code>MEDIA_MOUNT</code> (Media Mount blind devices. deviceid: 00000100-FFFFFF0F. Commands [ "down", "up", "stop" ].)</li>
  916. <li> <code>DC106</code> (DC/RMF/Yooda blind devices. deviceid: 00000100-FFFFFFF0. Commands [ "open", "close", "stop", "confirm" ].)</li>
  917. <li> <code>FOREST</code> (Forest blind devices. deviceid: 00000100-FFFFFFF0. Commands [ "open", "close", "stop", "confirm_pair" ].)</li>
  918. <li> <code>RFY</code> (Somfy RTS devices. deviceid: 000001-0FFFFF, unicode: 01-04 (00 = allunits). Commands [ "up", "down", "stop", "program" ].)</li>
  919. <li> <code>RFY_ext</code> (Somfy RTS devices. deviceid: 000001-0FFFFF, unicode: 00-0F. Commands [ "up", "down", "stop", "program" ].)</li>
  920. </ul>
  921. </ul>
  922. <br>
  923. <code>&lt;deviceid&gt;</code>
  924. <ul>
  925. specifies the first device id of the device. <br>
  926. A lighting device normally has a house code A..P followed by a unitcode 1..16 (example "B1").<br>
  927. For AC, HomeEasy EU and ANSLUT it is a 10 Character-Hex-String for the deviceid, consisting of <br>
  928. - unid-id: 8-Char-Hex: 00000001 to 03FFFFFF<br>
  929. - unit-code: 2-Char-Hex: 01 to 10 <br>
  930. For LIGHTWAVERF, EMW100, BBSB, MDREMOTE, RSL2, LIVOLO and TRC02 it is a 8 Character-Hex-String for the deviceid, consisting of <br>
  931. - unid-id: 8-Char-Hex: 000001 to FFFFFF<br>
  932. - unit-code: 2-Char-Hex: 01 to 10 <br>
  933. For RFY and RFY-ext it is a 8 Character-Hex-String for the deviceid, consisting of <br>
  934. - unid-id: 8-Char-Hex: 000001 to FFFFFF<br>
  935. - unit-code: 2-Char-Hex: 01 to 04 for RFY (00 for all units) and 00 to 0F for RFY_ext <br>
  936. </ul>
  937. <br>
  938. <code>&lt;devicelog&gt;</code>
  939. <ul>
  940. is the name of the Reading used to report. Suggested: "motion" for motion sensors. If you use "none" then no additional Reading is reported. Just the state is used to report the change.
  941. </ul>
  942. <br>
  943. <code>&lt;deviceid2&gt;</code>
  944. <ul>
  945. is optional and specifies the second device id of the device if it exists. For example ms14a motion sensors report motion status on the first deviceid and the status of the light sensor on the second deviceid.
  946. </ul>
  947. <br>
  948. <code>&lt;devicelog2&gt;</code>
  949. <ul>
  950. is optional for the name used for the Reading of <code>&lt;deviceid2&gt;</code>.If you use "none" then no addional Reading is reported. Just the state is used to report the change.
  951. </ul>
  952. <br>
  953. <code>&lt;commandcodes&gt;</code>
  954. <ul>
  955. is used for PT2262 and specifies the possible base4 digits for the command separated by : and a string that specifies a string that is the command. Example '<code>0:off,1:on</code>'.
  956. </ul>
  957. <br>
  958. Example: <br>
  959. <code>define motion_sensor2 TRX_LIGHT MS14A A1 motion A2 light</code>
  960. <br>
  961. <code>define Steckdose TRX_LIGHT ARC G2 light</code>
  962. <br>
  963. <code>define light TRX_LIGHT AC 0101010101 light</code>
  964. <br>
  965. </ul>
  966. <br>
  967. <a name="TRX_LIGHTset"></a>
  968. <b>Set </b>
  969. <ul>
  970. <code>set &lt;name&gt; &lt;value&gt; [&lt;levelnum&gt;]</code>
  971. <br><br>
  972. where <code>value</code> is one of:<br>
  973. <pre>
  974. off
  975. on
  976. dim # only for X10, KOPPLA
  977. bright # only for X10, KOPPLA
  978. all_off # only for X10, ARC, EMW200, AC, HOMEEASY, ANSLUT
  979. all_on # only for X10, ARC, EMW200, AC, HOMEEASY, ANSLUT
  980. chime # only for ARC
  981. level &lt;levelnum&gt; # only AC, HOMEEASY, ANSLUT: set level to &lt;levelnum&gt; (range: 0=0% to 15=100%)
  982. on-till # Special, see the note
  983. on-for-timer # Special, see the note
  984. ring # Byron MP001,SelectPlus, RFU, Envivo
  985. </pre>
  986. Example: <br>
  987. <code>set Steckdose on</code>
  988. <br>
  989. <br>
  990. Notes:
  991. <ul>
  992. <li><code>on-till</code> requires an absolute time in the "at" format
  993. (HH:MM:SS, HH:MM) or { &lt;perl code&gt; }, where the perl code
  994. returns a time specification).
  995. If the current time is greater than the specified time, then the
  996. command is ignored, else an "on" command is generated, and for the
  997. given "till-time" an off command is scheduleld via the at command.
  998. </li>
  999. <li><code>on-for-timer</code> requires a relative time in the "at" format
  1000. (HH:MM:SS, HH:MM) or { &lt;perl code&gt; }, where the perl code
  1001. returns a time specification).
  1002. </li>
  1003. </ul>
  1004. </ul><br>
  1005. <a name="TRX_LIGHTget"></a>
  1006. <b>Get</b> <ul>N/A</ul><br>
  1007. <a name="TRX_LIGHTattr"></a>
  1008. <b>Attributes</b>
  1009. <ul>
  1010. <li><a href="#ignore">ignore</a></li>
  1011. <li><a href="#do_not_notify">do_not_notify</a></li>
  1012. <li><a href="#readingFnAttributes">readingFnAttributes</a></li>
  1013. </ul>
  1014. </ul>
  1015. =end html
  1016. =cut