37_harmony.pm 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. # $Id: 37_harmony.pm 13311 2017-02-02 16:35:54Z justme1968 $
  2. package main;
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6. use JSON;
  7. use MIME::Base64;
  8. use IO::Socket::INET;
  9. use Encode qw(encode_utf8);
  10. #use XML::Simple qw(:strict);
  11. use HttpUtils;
  12. my $harmony_isFritzBox = undef;
  13. sub
  14. harmony_isFritzBox()
  15. {
  16. $harmony_isFritzBox = int( qx( [ -f /usr/bin/ctlmgr_ctl ] && echo 1 || echo 0 ) ) if( !defined($harmony_isFritzBox) );
  17. return $harmony_isFritzBox;
  18. }
  19. sub
  20. harmony_decode_json($)
  21. {
  22. my ($data) = @_;
  23. return eval { decode_json($data) } if( harmony_isFritzBox() );
  24. return eval { JSON->new->utf8(0)->decode($data) };
  25. }
  26. sub
  27. harmony_Initialize($)
  28. {
  29. my ($hash) = @_;
  30. $hash->{ReadFn} = "harmony_Read";
  31. $hash->{DefFn} = "harmony_Define";
  32. $hash->{NotifyFn} = "harmony_Notify";
  33. $hash->{UndefFn} = "harmony_Undefine";
  34. $hash->{SetFn} = "harmony_Set";
  35. $hash->{GetFn} = "harmony_Get";
  36. $hash->{AttrFn} = "harmony_Attr";
  37. $hash->{AttrList} = "disable:1 nossl:1 $readingFnAttributes";
  38. $hash->{FW_detailFn} = "harmony_detailFn";
  39. $hash->{FW_showStatus} = 1;
  40. }
  41. #####################################
  42. sub
  43. harmony_Define($$)
  44. {
  45. my ($hash, $def) = @_;
  46. my @a = split("[ \t][ \t]*", $def);
  47. return "Usage: define <name> harmony [username password] ip" if(@a < 3 || @a > 5);
  48. return "Usage: define <name> harmony [username password] ip" if(@a == 4 && $a[2] ne "DEVICE" );
  49. delete( $hash->{helper}{username} );
  50. delete( $hash->{helper}{password} );
  51. my $name = $a[0];
  52. if( @a == 3 ) {
  53. my $ip = $a[2];
  54. $hash->{ip} = $ip;
  55. } elsif( @a == 4 ) {
  56. my $id = $a[3];
  57. return "$name: device '$id' already defined" if( defined($modules{$hash->{TYPE}}{defptr}{$id}) );
  58. $hash->{id} = $id;
  59. $modules{$hash->{TYPE}}{defptr}{$id} = $hash;
  60. } elsif( @a == 5 ) {
  61. my $username = harmony_encrypt($a[2]);
  62. my $password = harmony_encrypt($a[3]);
  63. my $ip = $a[4];
  64. $hash->{DEF} = "$username $password $ip";
  65. $hash->{helper}{username} = $username;
  66. $hash->{helper}{password} = $password;
  67. $hash->{ip} = $ip;
  68. }
  69. $hash->{NAME} = $name;
  70. $hash->{STATE} = "Initialized";
  71. $hash->{ConnectionState} = "Initialized";
  72. #$attr{$name}{nossl} = 1 if( !$init_done && harmony_isFritzBox() );
  73. $hash->{NOTIFYDEV} = "global";
  74. if( $init_done ) {
  75. harmony_connect($hash) if( !defined($hash->{id}) );
  76. }
  77. return undef;
  78. }
  79. sub
  80. harmony_Notify($$)
  81. {
  82. my ($hash,$dev) = @_;
  83. return if($dev->{NAME} ne "global");
  84. return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
  85. harmony_connect($hash) if( !defined($hash->{id}) );
  86. return undef;
  87. }
  88. sub
  89. harmony_Undefine($$)
  90. {
  91. my ($hash, $arg) = @_;
  92. if( defined($hash->{id}) ) {
  93. delete( $modules{$hash->{TYPE}}{defptr}{$hash->{id}} );
  94. return undef;
  95. }
  96. RemoveInternalTimer($hash);
  97. harmony_disconnect($hash);
  98. return undef;
  99. }
  100. sub
  101. harmony_detailFn()
  102. {
  103. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  104. my $hash = $defs{$d};
  105. return if( !defined( $hash->{discoveryinfo} ) );
  106. my $clientId = $hash->{discoveryinfo}->{setupSessionClient};
  107. $clientId =~ s/^\w*-//;
  108. my $hubIP = $hash->{discoveryinfo}->{ip};
  109. my $hubName = $hash->{discoveryinfo}->{friendlyName};
  110. $hubName =~ s/ /%20/;
  111. return "<a href=\"http://sl.dhg.myharmony.com/mobile/2/production/?locale=de-DE&clientId=$clientId&hubIP=$hubIP&hubName=$hubName&settings\" target=\"_blank\">myHarmony config</a><br>"
  112. }
  113. sub
  114. harmony_idOfActivity($$;$)
  115. {
  116. my ($hash, $label, $default) = @_;
  117. my $quoted_label = $label;
  118. $quoted_label =~ s/\./ /g;
  119. $quoted_label = quotemeta($quoted_label);
  120. foreach my $activity (@{$hash->{config}->{activity}}) {
  121. return $activity->{id} if( $activity->{label} =~ m/^$label$/ );
  122. return $activity->{id} if( $activity->{label} =~ m/^$quoted_label$/ );
  123. }
  124. return $default;
  125. }
  126. sub
  127. harmony_labelOfActivity($$;$)
  128. {
  129. my ($hash, $id, $default) = @_;
  130. foreach my $activity (@{$hash->{config}->{activity}}) {
  131. return $activity->{label} if( $activity->{id} == $id );
  132. }
  133. return $default;
  134. }
  135. sub
  136. harmony_activityOfId($$)
  137. {
  138. my ($hash, $id) = @_;
  139. foreach my $activity (@{$hash->{config}->{activity}}) {
  140. return $activity if( $activity->{id} == $id );
  141. }
  142. return undef;
  143. }
  144. sub
  145. harmony_idOfDevice($$;$)
  146. {
  147. my ($hash, $label, $default) = @_;
  148. my $quoted_label = $label;
  149. $quoted_label =~ s/\./ /g;
  150. $quoted_label = quotemeta($quoted_label);
  151. foreach my $device (@{$hash->{config}->{device}}) {
  152. return $device->{id} if( $device->{label} =~ m/^$label$/ );
  153. return $device->{id} if( $device->{label} =~ m/^$quoted_label$/ );
  154. }
  155. return $default;
  156. }
  157. sub
  158. harmony_labelOfDevice($$;$)
  159. {
  160. my ($hash, $id, $default) = @_;
  161. return undef if( $id eq '<unknown>' );
  162. return undef if( !defined($hash->{config}) );
  163. foreach my $device (@{$hash->{config}->{device}}) {
  164. return $device->{label} if( $device->{id} == $id );
  165. }
  166. return $default;
  167. }
  168. sub
  169. harmony_deviceOfId($$)
  170. {
  171. my ($hash, $id) = @_;
  172. return undef if( !defined($hash->{config}) );
  173. foreach my $device (@{$hash->{config}->{device}}) {
  174. return $device if( $device->{id} == $id );
  175. }
  176. return undef;
  177. }
  178. sub
  179. harmony_actionOfCommand($$)
  180. {
  181. my ($device, $command) = @_;
  182. return undef if( ref($device) ne "HASH" );
  183. $command = lc($command);
  184. foreach my $group (@{$device->{controlGroup}}) {
  185. foreach my $function (@{$group->{function}}) {
  186. #if( lc($function->{name}) eq $command ) {
  187. if( lc($function->{name}) =~ m/^$command$/ ) {
  188. return harmony_decode_json($function->{action});
  189. }
  190. }
  191. }
  192. return undef;
  193. }
  194. sub
  195. harmony_hubOfDevice($)
  196. {
  197. my ($id) = @_;
  198. foreach my $d (sort keys %defs) {
  199. next if( !defined($defs{$d}) );
  200. next if( $defs{$d}->{TYPE} ne "harmony" );
  201. next if( $defs{$d}->{id} );
  202. next if( !harmony_deviceOfId($defs{$d}, $id) );
  203. Log3 undef, 3, "harmony: IODev for device $id is $d" ;
  204. return $d;
  205. }
  206. }
  207. sub
  208. harmony_Set($$@)
  209. {
  210. my ($hash, $name, $cmd, @params) = @_;
  211. my ($param, $param2) = @params;
  212. #$cmd = lc( $cmd );
  213. my $list = "";
  214. if( defined($hash->{id}) ) {
  215. if( !$hash->{hub} ) {
  216. $hash->{hub} = harmony_hubOfDevice($hash->{id});
  217. return "no hub found for device $name ($hash->{id})" if( !$hash->{hub} );
  218. }
  219. if( $cmd ne "?" && !$param ) {
  220. $cmd = "PowerOn" if( $cmd eq "on" );
  221. $cmd = "PowerOff" if( $cmd eq "off" );
  222. my $device = harmony_deviceOfId( $defs{$hash->{hub}}, $hash->{id} );
  223. if( harmony_actionOfCommand( $device, $cmd ) ) {
  224. $param = $cmd;
  225. $cmd = "command";
  226. }
  227. }
  228. if( $cmd eq "command" ) {
  229. $param2 = $param;
  230. $param = $hash->{id};
  231. $hash = $defs{$hash->{hub}};
  232. } elsif( $cmd eq "hidDevice" || $cmd eq "text" || $cmd eq "cursor" || $cmd eq "special" || $cmd eq "hid" ) {
  233. my $id = $hash->{id};
  234. $hash = $defs{$hash->{hub}};
  235. my $device = harmony_deviceOfId( $hash, $id );
  236. return "unknown device" if( !$device );
  237. return "no keyboard associated with device $device->{label}" if( !$device->{IsKeyboardAssociated} );
  238. if( !$hash->{hidDevice} || $hash->{hidDevice} ne $id ) {
  239. $hash->{hidDevice} = $id;
  240. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.engine?sethiddevice' token=''>deviceId=$id</oa>");
  241. sleep( 3 );
  242. }
  243. return if( $cmd eq "hidDevice" );
  244. } else {
  245. $list = "command hidDevice:noArg text cursor:up,down,left,right,pageUp,pageDown,home,end special:previousTrack,nextTrack,stop,playPause,volumeUp,volumeDown,mute";
  246. #my $device = harmony_deviceOfId( $defs{$hash->{hub}}, $hash->{id} );
  247. #$list .= " on off" if( !defined($device->{isManualPower}) || $device->{isManualPower} eq "false" || !$device->{isManualPower} );
  248. return "Unknown argument $cmd, choose one of $list";
  249. }
  250. }
  251. if( $cmd ne "?" && !$param ) {
  252. if( $cmd eq 'off' ) {
  253. $cmd = "activity";
  254. $param = "-1";
  255. } elsif( my $activity = harmony_activityOfId($hash, $hash->{currentActivityID}) ) {
  256. if( harmony_actionOfCommand( $activity, $cmd ) ) {
  257. $param = $cmd;
  258. $cmd = "command";
  259. }
  260. }
  261. }
  262. if( $cmd eq 'activity' ) {
  263. $param = harmony_idOfActivity($hash, $param) if( $param && $param !~ m/^([\d-])+$/ );
  264. return "unknown activity" if( !$param );
  265. harmony_sendEngineGet($hash, "startactivity", "activityId=$param:timestamp=0");
  266. delete $hash->{channelAfterStart};
  267. $hash->{channelAfterStart} = $param2 if( $param2 );
  268. return undef;
  269. } elsif( $cmd eq "channel" ) {
  270. delete $hash->{channelAfterStart};
  271. return "no current activity" if( !defined($hash->{currentActivityID}) || $hash->{currentActivityID} == -1 );
  272. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  273. return "no device with 'channel changing role' in current activity $activity->{label}" if( !$activity->{isTuningDefault} );
  274. return "missing channel" if( !$param );
  275. harmony_sendEngineGet($hash, "changeChannel", "channel=$param:timestamp=0");
  276. return undef;
  277. } elsif( $cmd eq "command" ) {
  278. my $action;
  279. if( !$param2 ) {
  280. return "unknown activity" if( !$hash->{currentActivityID} );
  281. return "unknown command" if( !$param );
  282. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  283. return "unknown activity" if( !$activity );
  284. $action = harmony_actionOfCommand( $activity, $param );
  285. return "unknown command $param" if( !$action );
  286. } else {
  287. $param = harmony_idOfDevice($hash, $param) if( $param && $param !~ m/^([\d-])+$/ );
  288. return "unknown device" if( !$param );
  289. return "unknown command" if( !$param2 );
  290. my $device = harmony_deviceOfId( $hash, $param );
  291. return "unknown device" if( !$device );
  292. $action = harmony_actionOfCommand( $device, $param2 );
  293. return "unknown command $param2" if( !$action );
  294. }
  295. Log3 $name, 4, "$name: sending $action->{command} for ". harmony_labelOfDevice($hash, $action->{deviceId} );
  296. my $payload = "status=press:action={'command'::'$action->{command}','type'::'$action->{type}','deviceId'::'$action->{deviceId}'}:timestamp=0";
  297. harmony_sendEngineRender($hash, "holdAction", $payload);
  298. select(undef, undef, undef, (0.1));
  299. $payload = "status=release:action={'command'::'$action->{command}','type'::'$action->{type}','deviceId'::'$action->{deviceId}'}:timestamp=100";
  300. harmony_sendEngineRender($hash, "holdAction", $payload);
  301. return undef;
  302. } elsif( $cmd eq "getCurrentActivity" ) {
  303. harmony_sendEngineGet($hash, "getCurrentActivity");
  304. return undef;
  305. } elsif( $cmd eq "getConfig" ) {
  306. harmony_sendEngineGet($hash, "config");
  307. return undef;
  308. } elsif( $cmd eq "hidDevice" ) {
  309. my $id = $param;
  310. if( !$id && $hash->{currentActivityID} ) {
  311. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  312. return "unknown activity" if( !$activity );
  313. return "no device with 'keyboard text entry role' in current activity $activity->{label}" if( !$activity->{KeyboardTextEntryActivityRole} );
  314. $id = $activity->{KeyboardTextEntryActivityRole};
  315. } else {
  316. $id = harmony_idOfDevice($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  317. return "unknown device $param" if( $param && !$id );
  318. my $device = harmony_deviceOfId( $hash, $id );
  319. return "unknown device" if( !$device );
  320. return "no keyboard associated with device $device->{label}" if( !$device->{IsKeyboardAssociated} );
  321. }
  322. $hash->{hidDevice} = $id;
  323. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.engine?sethiddevice' token=''>deviceId=$id</oa>");
  324. return undef;
  325. } elsif( $cmd eq "hid" || $cmd eq "text" || $cmd eq "cursor" || $cmd eq "special" ) {
  326. return "nothing to send" if( !$param );
  327. return "unknown activity" if( !$hash->{currentActivityID} );
  328. return "unknown command" if( !$param );
  329. if( !$hash->{hidDevice} ) {
  330. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  331. return "unknown activity" if( !$activity );
  332. return "no device with 'keyboard text entry role' in current activity $activity->{label}" if( !$activity->{KeyboardTextEntryActivityRole} );
  333. }
  334. if( $cmd eq "text" ) {
  335. $hash->{hid} = "" if( !$hash->{hid} );
  336. $hash->{hid} .= join(' ', @params);
  337. $param = undef;
  338. } elsif( $cmd eq "cursor" ) {
  339. $param = lc( $param ) if( $param );
  340. $param = "0700004A" if( $param eq "home" );
  341. $param = "0700004B" if( $param eq "pageup" );
  342. $param = "0700004D" if( $param eq "end" );
  343. $param = "0700004E" if( $param eq "pagedown" );
  344. $param = "0700004F" if( $param eq "right" );
  345. $param = "07000050" if( $param eq "left" );
  346. $param = "07000051" if( $param eq "down" );
  347. $param = "07000052" if( $param eq "up" );
  348. return "unknown cursor direction $param" if( $param !~ m/^07/ );
  349. } elsif( $cmd eq "special" ) {
  350. $param = lc( $param ) if( $param );
  351. $param = "01000081" if( $param eq "systempower" );
  352. $param = "01000082" if( $param eq "systemsleep" );
  353. $param = "01000083" if( $param eq "systemwake" );
  354. $param = "0C0000B5" if( $param eq "nexttrack" );
  355. $param = "0C0000B6" if( $param eq "previoustrack" );
  356. $param = "0C0000B7" if( $param eq "stop" );
  357. $param = "0C0000CD" if( $param eq "playpause" );
  358. $param = "0C0000E9" if( $param eq "volumeup" );
  359. $param = "0C0000EA" if( $param eq "volumedown" );
  360. $param = "0C0000E2" if( $param eq "mute" );
  361. return "unknown special key $param" if( $param !~ m/^0(1|C)/ );
  362. }
  363. harmony_sendHID($hash, $param);
  364. return undef;
  365. } elsif( $cmd eq "reconnect" ) {
  366. delete $hash->{helper}{UserAuthToken} if( $param eq "all" );
  367. delete $hash->{identity} if( $param eq "all" );
  368. harmony_connect($hash);
  369. return undef;
  370. } elsif( $cmd eq "autocreate" ) {
  371. return harmony_autocreate($hash,$param);
  372. return undef;
  373. } elsif( $cmd eq "sleeptimer" ) {
  374. my $interval = $param?$param*60:60*60;
  375. $interval = -1 if( $interval < 0 );
  376. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.engine?setsleeptimer'>interval=$interval</oa>", "setsleeptimer");
  377. return undef;
  378. } elsif( $cmd eq "sync" ) {
  379. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='setup.sync' token=''/>");
  380. return undef;
  381. } elsif( $cmd eq "update" ) {
  382. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?update' token=''>format=json</oa>");
  383. return undef;
  384. } elsif( $cmd eq "xxx" ) {
  385. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?check' token=''>format=json</oa>");
  386. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?status' token=''>format=json</oa>");
  387. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?update' token=''>format=json</oa>");
  388. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation?notify' token=''></oa>");
  389. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation?getState' token=''></oa>");
  390. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation.state?notify' token=''></oa>");
  391. #harmony_sendIq($hash, "<query xmlns='jabber:iq:roster'/>");
  392. #harmony_sendIq($hash, "<query xmlns='http://jabber.org/protocol/disco#info'/>");
  393. #harmony_sendIq($hash, "<query xmlns='http://jabber.org/protocol/disco#items'/>");
  394. return undef;
  395. }
  396. if( $hash->{config} ) {
  397. return undef if( !defined($hash->{config}) );
  398. my $activities;
  399. if( $hash->{config}->{activity} ) {
  400. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  401. next if( $activity->{id} == -1 );
  402. $activities .= "," if( $activities );
  403. $activities .= $activity->{label};
  404. }
  405. }
  406. if( my $activity = harmony_activityOfId($hash, -1) ) {
  407. $activities .= "," if( $activities );
  408. $activities .= $activity->{label};
  409. }
  410. if( $activities ) {
  411. $activities =~ s/ /./g;
  412. $list .= " activity:$activities";
  413. }
  414. my $hidDevices;
  415. my $autocreateDevices;
  416. if( $hash->{config}->{device} ) {
  417. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  418. if( $device->{IsKeyboardAssociated} ) {
  419. $hidDevices .= "," if( $hidDevices );
  420. $hidDevices .= harmony_labelOfDevice($hash, $device->{id} );
  421. }
  422. if( !defined($modules{$hash->{TYPE}}{defptr}{$device->{id}}) ) {
  423. $autocreateDevices .= "," if( $autocreateDevices );
  424. $autocreateDevices .= harmony_labelOfDevice($hash, $device->{id} );
  425. }
  426. }
  427. }
  428. if( $hidDevices ) {
  429. $hidDevices =~ s/ /./g;
  430. $list .= " hidDevice:,$hidDevices";
  431. }
  432. if( $autocreateDevices ) {
  433. $autocreateDevices =~ s/ /./g;
  434. $list .= " autocreate:$autocreateDevices,";
  435. }
  436. }
  437. $list .= " channel" if( defined($hash->{currentActivityID}) && $hash->{currentActivityID} != -1 );
  438. $list .= " command getConfig:noArg getCurrentActivity:noArg off:noArg reconnect:noArg sleeptimer sync:noArg text cursor:up,down,left,right,pageUp,pageDown,home,end special:previousTrack,nextTrack,stop,playPause,volumeUp,volumeDown,mute";
  439. $list .= " update:noArg" if( $hash->{hubUpdate} );
  440. return "Unknown argument $cmd, choose one of $list";
  441. }
  442. sub
  443. harmony_getLoginToken($)
  444. {
  445. my ($hash) = @_;
  446. #return if( defined($hash->{helper}{UserAuthToken}) );
  447. if( 0 ) {
  448. my $https = "https";
  449. $https = "http" if( AttrVal($hash->{NAME}, "nossl", 0) );
  450. my $json = encode_json( { clientId => '',
  451. clientTypeId => 'ControlApp' } );
  452. my($err,$data) = HttpUtils_BlockingGet({
  453. url => "$https://svcs.myharmony.com/discovery/Discovery.svc/json/GetJson2Uris",
  454. timeout => 10,
  455. #noshutdown => 1,
  456. #httpversion => "1.1",
  457. header => "Content-Type: application/json;charset=utf-8",
  458. data => $json,
  459. });
  460. harmony_dispatch( {hash=>$hash,type=>'GetJson2Uris'},$err,$data );
  461. }
  462. return if( defined($hash->{helper}{UserAuthToken}) );
  463. if( 1 || !$hash->{helper}{username} ) {
  464. $hash->{helper}{UserAuthToken} = "";
  465. return;
  466. }
  467. my $https = "https";
  468. $https = "http" if( AttrVal($hash->{NAME}, "nossl", 0) );
  469. my $json = encode_json( { email => harmony_decrypt($hash->{helper}{username}),
  470. password => harmony_decrypt($hash->{helper}{password}) } );
  471. my($err,$data) = HttpUtils_BlockingGet({
  472. url => "$https://svcs.myharmony.com/CompositeSecurityServices/Security.svc/json/GetUserAuthToken",
  473. timeout => 10,
  474. #noshutdown => 1,
  475. #httpversion => "1.1",
  476. header => "Content-Type: application/json;charset=utf-8",
  477. data => $json,
  478. });
  479. harmony_dispatch( {hash=>$hash,type=>'token'},$err,$data );
  480. }
  481. sub
  482. harmony_attr2hash($)
  483. {
  484. my ($attr) = @_;
  485. my @args = split(' ', $attr);
  486. my %params = ();
  487. while (@args) {
  488. my $arg = shift(@args);
  489. my ($name,$value) = split("=", $arg,2);
  490. while( $value && $value =~ m/^'/ && $value !~ m/'$/ ) {
  491. my $next = shift(@args);
  492. last if( !defined($next) );
  493. $value .= " ". $next;
  494. }
  495. $params{$name} = substr( $value, 1, -1 );
  496. }
  497. return \%params;
  498. }
  499. sub
  500. harmony_CDATA2hash($)
  501. {
  502. my ($cdata) = @_;
  503. my @args = split(':', $cdata);
  504. my %params = ();
  505. while (@args) {
  506. my $arg = shift(@args);
  507. my ($name,$value) = split("=", $arg,2);
  508. #fix for updates=table: 0x...
  509. if( $args[0] && $args[0] !~ m/=/ ) {
  510. my $next = shift(@args);
  511. last if( !defined($next) );
  512. $value .= ":". $next;
  513. }
  514. ##fix for http://...
  515. #if( $args[0] && $args[0] =~ m/^\/\// ) {
  516. # my $next = shift(@args);
  517. # last if( !defined($next) );
  518. # $value .= ":". $next;
  519. #}
  520. #fix for json {...<key>:<value>...}
  521. while( $value && $value =~ m/^{/ && $value !~ m/}$/ ) {
  522. my $next = shift(@args);
  523. last if( !defined($next) );
  524. $value .= ":". $next;
  525. }
  526. $params{$name} = $value if( $name );
  527. }
  528. return \%params;
  529. }
  530. use constant { CTRL => 0x01,
  531. SHIFT => 0x02,
  532. ALT => 0x04,
  533. GUI => 0x08,
  534. RIGHT_CTRL => 0x10,
  535. RIGHT_SHIFT => 0x20,
  536. RIGHT_ALT => 0x40,
  537. RIGHT_GUI => 0x80,
  538. };
  539. my %keys = ( '1' => '0702001E',
  540. '2' => '0702001F',
  541. '3' => '07020020',
  542. '4' => '07020021',
  543. '5' => '07020022',
  544. '6' => '07020023',
  545. '7' => '07020024',
  546. '8' => '07020025',
  547. '9' => '07020026',
  548. '0' => '07020027',
  549. '\\n'=> '07000028',
  550. '\\e'=> '07000029',
  551. '\\t'=> '0700002B',
  552. ' ' => '0700002C',
  553. '!' => '0702001E',
  554. '"' => '0702001F',
  555. '§' => '07020020',
  556. '$' => '07020021',
  557. '%' => '07020022',
  558. '&' => '07020023',
  559. '/' => '07020024',
  560. '(' => '07020025',
  561. ')' => '07020026',
  562. '=' => '07020027',
  563. 'ß' => '0700002D',
  564. '´' => '0700002E',
  565. 'ü' => '0700002F',
  566. '+' => '07000030',
  567. '#' => '07000031',
  568. 'ö' => '07000033',
  569. 'ä' => '07000034',
  570. '<' => '07000035',
  571. ',' => '07000036',
  572. '.' => '07000037',
  573. '-' => '07000038',
  574. '?' => '0702002D',
  575. '`' => '0702002E',
  576. 'Ü' => '0702002F',
  577. '*' => '07020030',
  578. "'" => '07020031',
  579. 'Ö' => '07020033',
  580. 'Ä' => '07020034',
  581. '>' => '07020035',
  582. ';' => '07020036',
  583. ':' => '07020037',
  584. '_' => '07020038',
  585. 'F1' => '0700003A',
  586. 'F2' => '0700003B',
  587. 'F3' => '0700003C',
  588. 'F4' => '0700003D',
  589. 'F5' => '0700003E',
  590. 'F6' => '0700003F',
  591. 'F7' => '07000040',
  592. 'F8' => '07000041',
  593. 'F9' => '07000042',
  594. 'F10' => '07000043',
  595. 'F11' => '07000044',
  596. 'F12' => '07000045',
  597. 'KP/' => '07000054',
  598. 'KP*' => '07000055',
  599. 'KP-' => '07000056',
  600. 'KP+' => '07000057',
  601. 'KP\\n' => '07000058',
  602. 'KP1' => '07000059',
  603. 'KP2' => '0700005A',
  604. 'KP3' => '0700005C',
  605. 'KP4' => '0700005C',
  606. 'KP5' => '0700005D',
  607. 'KP6' => '0700005E',
  608. 'KP7' => '0700005F',
  609. 'KP8' => '07000060',
  610. 'KP9' => '07000061',
  611. 'KP0' => '07000062',
  612. );
  613. sub
  614. harmony_char2hid($)
  615. {
  616. my ($char) = @_;
  617. my $ret;
  618. if( $char ge '1' && $char le '9' ) {
  619. $ret = sprintf( "070000%02X", 0x1E + ord($char) - ord('1') );
  620. } elsif( $char ge 'a' && $char le 'z' ) {
  621. $ret = sprintf( "070000%02X", 0x04 + ord($char) - ord('a') );
  622. } elsif( $char ge 'A' && $char le 'Z' ) {
  623. $ret = sprintf( "070200%02X", 0x04 + ord($char) - ord('A') );
  624. } elsif( defined( $keys{$char} ) ) {
  625. $ret = $keys{$char};
  626. }
  627. return $ret;
  628. }
  629. sub
  630. harmony_updateActivity($$;$)
  631. {
  632. my ($hash,$id,$modifier) = @_;
  633. $modifier = "" if( !$modifier );
  634. if( $hash->{currentActivityID} && $hash->{currentActivityID} ne $id ) {
  635. my $id = $hash->{currentActivityID};
  636. $hash->{previousActivityID} = $id;
  637. my $previous = harmony_labelOfActivity($hash,$id,$id);
  638. readingsSingleUpdate( $hash, "previousActivity", $previous, 0 );
  639. }
  640. if( !$modifier && defined($modules{$hash->{TYPE}}{defptr}) ) {
  641. if( my $activity = harmony_activityOfId($hash, $id)) {
  642. foreach my $id (keys %{$activity->{fixit}}) {
  643. if( my $hash = $modules{$hash->{TYPE}}{defptr}{$id} ) {
  644. my $state = $activity->{fixit}->{$id}->{Power};
  645. $state = "Manual" if( !$state );
  646. readingsSingleUpdate( $hash, "power", lc($state), 1 );
  647. }
  648. }
  649. }
  650. }
  651. $hash->{currentActivityID} = $id;
  652. my $activity = harmony_labelOfActivity($hash,$id,$id);
  653. readingsSingleUpdate( $hash, "currentActivity", "$modifier$activity", 1 );
  654. $activity =~ s/ /./g;
  655. if( !$modifier && $activity ne ReadingsVal($hash->{NAME},"activity", "" ) ) {
  656. readingsSingleUpdate( $hash, "activity", $activity, 1 );
  657. harmony_sendEngineGet($hash, "changeChannel", "channel=$hash->{channelAfterStart}:timestamp=0") if( $hash->{channelAfterStart} );
  658. delete $hash->{channelAfterStart};
  659. }
  660. delete $hash->{hidDevice} if( $id == -1 );
  661. }
  662. sub
  663. harmony_Read($)
  664. {
  665. my ($hash) = @_;
  666. my $name = $hash->{NAME};
  667. my $buf;
  668. my $ret = sysread($hash->{CD}, $buf, 1024*1024);
  669. if(!defined($ret) || $ret <= 0) {
  670. harmony_disconnect( $hash );
  671. InternalTimer(gettimeofday()+2, "harmony_connect", $hash, 0);
  672. return;
  673. }
  674. my $data = $hash->{helper}{PARTIAL};
  675. $data .= $buf;
  676. #FIXME: should use real xmpp/xml parser
  677. # see forum https://forum.fhem.de/index.php/topic,14163.msg575033.html#msg575033
  678. $data =~ s/<iq\/>//g;
  679. $data =~ s/<\/iq><iq/<\/iq>\n<iq/g;
  680. my @lines = split( "\n", $data );
  681. foreach my $line (@lines) {
  682. if( $line =~ m/^<(\w*)\s*([^>]*)?\/>(.*)?/ ) {
  683. Log3 $name, 5, "$name: tag: $1, attr: $2";
  684. $data = $3;
  685. if( $line eq "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>" ) {
  686. $hash->{ConnectionState} = "LoggedIn";
  687. if( $hash->{helper}{UserAuthToken} ) {
  688. harmony_getSessionToken($hash);
  689. } else {
  690. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.ping?get' token=''></oa>");
  691. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.system?systeminfo' token=''></oa>");
  692. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.deviceinfo?get' token=''></oa>");
  693. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.setup/vnd.logitech.account?getProvisionInfo' token=''></oa>");
  694. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.statedigest?get' token=''>format=json</oa>");
  695. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?check' token=''>format=json</oa>");
  696. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?status' token=''>format=json</oa>");
  697. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?update' token=''>format=json</oa>");
  698. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=dynamite:://HomeAutomationService/Config/:encode=true</oa>");
  699. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation?getstate' token=''></oa>");
  700. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.pair'>name=1vm7ATw/tN6HXGpQcCs/A5MkuvI#iOS6.0.1#iPhone</oa>");
  701. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='connect.discoveryinfo?get'>format=json</oa>");
  702. #harmony_sendEngineGet($hash, "config");
  703. }
  704. RemoveInternalTimer($hash);
  705. InternalTimer(gettimeofday()+50, "harmony_ping", $hash, 0);
  706. }
  707. $line = $3;
  708. }
  709. if( $line =~ m/^<(\w*)([^>]*)>(.*)<\/\1>(.*)?/ ) {
  710. Log3 $name, 5, "$name: tag: $1, attr: $2";
  711. #Log3 $name, 5, " data: $3";
  712. $data = $4;
  713. my $tag = $1;
  714. my $attr = $2;
  715. my $content = $3;
  716. #if( $content =~ m/^<(\w*)([^>]*)>(.*)<\/\1>(.*)?/ ) {
  717. # Log3 $name, 1, "$name: tag: $1, attr: $2";
  718. # Log3 $name, 1, Dumper harmony_attr2hash($2);
  719. #}
  720. if( $content =~ m/<!\[CDATA\[(.*)\]\]>/ ) {
  721. my $cdata = $1;
  722. my $json;
  723. my $decoded;
  724. if( $cdata =~ m/^{.*}$/ ) {
  725. $json = harmony_decode_json($cdata);
  726. $decoded = $json;
  727. } else {
  728. $decoded = harmony_CDATA2hash($cdata);
  729. }
  730. my $error = $decoded->{errorCode};
  731. if( $error && $error != 200 ) {
  732. Log3 $name, 2, "$name: error ($error): $decoded->{errorString}";
  733. }
  734. if( ($tag eq "iq" && $content =~ m/statedigest\?get'/)
  735. || ($tag eq "message" && $content =~ m/type="connect.stateDigest\?notify"/) ) {
  736. Log3 $name, 4, "$name: statedigest: $cdata";
  737. if( $decoded ) {
  738. if( defined($decoded->{syncStatus}) ) {
  739. harmony_sendEngineGet($hash, "config") if( $hash->{syncStatus} && !$decoded->{syncStatus} );
  740. $hash->{syncStatus} = $decoded->{syncStatus};
  741. }
  742. if( defined($decoded->{hubUpdate}) && $decoded->{hubUpdate} eq "true" && !$hash->{hubUpdate} ) {
  743. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?check' token=''>format=json</oa>");
  744. }
  745. $hash->{activityStatus} = $decoded->{activityStatus} if( defined($decoded->{activityStatus}) );
  746. $hash->{hubSwVersion} = $decoded->{hubSwVersion} if( defined($decoded->{hubSwVersion}) );
  747. $hash->{hubUpdate} = ($decoded->{hubUpdate} eq 'true'?1:0) if( defined($decoded->{hubUpdate}) );
  748. my $modifier = "";
  749. $modifier = "starting " if( $hash->{activityStatus} == 1 );
  750. $modifier = "stopping " if( $hash->{activityStatus} == 3 );
  751. harmony_updateActivity($hash, $decoded->{activityId}, $modifier) if( defined($decoded->{activityId}) );
  752. if( defined($decoded->{sleepTimerId}) ) {
  753. if( $decoded->{sleepTimerId} == -1 ) {
  754. delete $hash->{sleeptimer};
  755. } else {
  756. harmony_sendEngineGet($hash, "gettimerinterval", "timerId=$decoded->{sleepTimerId}");
  757. }
  758. }
  759. }
  760. } elsif( $tag eq "message" ) {
  761. if( $content =~ m/type="harmony.engine\?startActivityFinished"/ ) {
  762. if( my $id = $decoded->{activityId} ) {
  763. if( harmony_activityOfId($hash, $id) ) {
  764. if( $id == -1 && $hash->{helper}{ignorePowerOff} ) {
  765. delete $hash->{helper}{ignorePowerOff};
  766. } else {
  767. harmony_updateActivity($hash, $id);
  768. }
  769. } else {
  770. $hash->{helper}{ignorePowerOff} = 1;
  771. }
  772. }
  773. } elsif( $content =~ m/type="vnd.logitech.harmony\/vnd.logitech.control.button\?pressType"/ ) {
  774. DoTrigger( $name, "vnd.logitech.control.button: $decoded->{type}" );
  775. } elsif( $content =~ m/type="automation.state\?notify"/ ) {
  776. DoTrigger( $name, "automation.state: $cdata" );
  777. } else {
  778. Log3 $name, 4, "$name: unknown message: $content";
  779. }
  780. } elsif( $tag eq "iq" ) {
  781. if( $content =~ m/errorcode='(\d*)'.*errorstring='(.*)'/ && $1 != 100 && $1 != 200 ) {
  782. Log3 $name, 2, "$name: error ($1): $2";
  783. } elsif( $content =~ m/vnd.logitech.pair/ ) {
  784. if( !$hash->{identity} && $decoded->{identity} ) {
  785. $hash->{identity} = $decoded->{identity};
  786. harmony_connect($hash);
  787. } else {
  788. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='connect.discoveryinfo?get'>format=json</oa>");
  789. #harmony_sendEngineGet($hash, "config");
  790. }
  791. } elsif( $content =~ m/\?startactivity/i ) {
  792. if( $cdata =~ m/done=(\d*):total=(\d*)(:deviceId=(\d*))?/ ) {
  793. my $done = $1;
  794. my $total = $2;
  795. my $id = $4;
  796. $id = "<unknown>" if( !defined($id) );
  797. my $label = harmony_labelOfDevice($hash,$id,$id);
  798. if( $done == $total ) {
  799. Log3 $name, 4, "$name: done starting/stopping device: $label";
  800. } elsif( $done == 1 ) {
  801. Log3 $name, 4, "$name: starting/stopping device: $label";
  802. } else {
  803. Log3 $name, 4, "$name: starting/stopping device ($done/$total): $label";
  804. }
  805. } else {
  806. Log3 $name, 3, "$name: unknown startactivity message: $content";
  807. }
  808. } elsif( $content =~ m/discoveryinfo\?get/ && $decoded ) {
  809. Log3 $name, 4, "$name: ". Dumper $decoded;
  810. $hash->{discoveryinfo} = $decoded;
  811. #$hash->{current_fw_version} = $decoded->{current_fw_version} if( defined($decoded->{current_fw_version}) );
  812. harmony_sendEngineGet($hash, "config");
  813. } elsif( $content =~ m/engine\?changeChannel/ && $decoded ) {
  814. } elsif( $content =~ m/engine\?gettimerinterval/ && $decoded ) {
  815. $hash->{sleeptimer} = FmtDateTime( gettimeofday() + $decoded->{interval} );
  816. } elsif( $content =~ m/firmware\?/ && $decoded ) {
  817. Log3 $name, 4, "$name: firmware: $cdata";
  818. if( $decoded->{status} && $decoded->{newVersion} ) {
  819. $hash->{newVersion} = $decoded->{newVersion};
  820. my $txt = $decoded->{newVersion};
  821. $txt .= ", isCritical: $decoded->{isCritical}";
  822. $txt .= ", bytes: $decoded->{totalBytes}";
  823. readingsSingleUpdate( $hash, "newVersion", $txt, 1 ) if( $txt ne ReadingsVal($hash->{NAME},"newVersion", "" ) );
  824. } else {
  825. delete $hash->{newVersion};
  826. }
  827. } elsif( $content =~ m/\?config/ && $decoded ) {
  828. $hash->{config} = $decoded;
  829. Log3 $name, 3, "$name: new config ";
  830. #Log3 $name, 5, "$name: ". Dumper $json;
  831. #my $station = $hash->{config}->{content}->{contentImageHost};
  832. #$station =~ s/{stationId}/4faa0c3b7232c50c26001b86/;
  833. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=content:://1.0/user;$station:encode=true</oa>");
  834. #foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  835. # my $content = $hash->{config}->{content}->{contentDeviceHost};
  836. # $content =~ s/{deviceProfileUri}/$device->{deviceProfileUri}/;
  837. # harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=content:://1.0/user;$content:encode=true</oa>");
  838. # last;
  839. #}
  840. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=content:://1.0/user;$hash->{config}->{content}->{householdUserProfileUri}:encode=true</oa>");
  841. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.statedigest?get' token=''>format=json</oa>");
  842. #harmony_sendEngineGet($hash, "getCurrentActivity");
  843. } elsif( $cdata =~ m/result=(.*)/ ) {
  844. my $result = $1;
  845. Log3 $name, 4, "$name: got result $1";
  846. if( $content =~ m/getCurrentActivity/ ) {
  847. harmony_updateActivity($hash, $result);
  848. } else {
  849. Log3 $name, 3, "$name: unknown result: $content";
  850. }
  851. } elsif( $content =~ m/mime='hid.report'/ ) {
  852. harmony_sendHID($hash) if( $hash->{hid} );
  853. } else {
  854. Log3 $name, 3, "$name: unknown iq: $content";
  855. Log 3, Dumper $decoded;
  856. Log 3, Dumper harmony_decode_json($decoded->{resource}) if( !$json && $decoded->{resource} && $decoded->{resource} =~ m/^{.*}$/ );
  857. }
  858. } else {
  859. Log3 $name, 3, "$name: unhandled tag: $line";
  860. }
  861. } elsif( $content =~ m/mime='hid.report'/ ) {
  862. harmony_sendHID($hash) if( $hash->{hid} );
  863. } elsif( $line =~ m/<iq id='ping-(\d+)' type='result'><\/iq>/ ) {
  864. Log3 $name, 5, "$name: got ping response $1";
  865. } elsif( $line ) {
  866. Log3 $name, 4, "$name: unknown (no cdata): $line";
  867. }
  868. } elsif( $line =~ m/^<\?xml.*id='([\w-]*).*error.*>/ ) {
  869. Log3 $name, 2, "$name: error: $1" if( $1 );
  870. Log3 $name, 4, "$name: $line";
  871. harmony_disconnect($hash);
  872. } elsif( $line =~ m/^<\?xml.*PLAIN.*>/ ) {
  873. my $identity = $hash->{identity}?$hash->{identity}:"guest";
  874. my $auth = encode_base64("\0$identity\@connect.logitech.com\0gatorade.",'');
  875. harmony_send($hash, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>$auth</auth>");
  876. $data = "";
  877. } elsif( $line =~ m/^<.*>$/ ) {
  878. Log3 $name, 4, "$name: unknown: $line";
  879. } elsif( $line ) {
  880. #Log3 $name, 5, "$name: $line";
  881. }
  882. }
  883. $hash->{helper}{PARTIAL} = $data;
  884. #Log 3, "length: ". length($hash->{helper}{PARTIAL});
  885. }
  886. sub
  887. harmony_disconnect($)
  888. {
  889. my ($hash) = @_;
  890. my $name = $hash->{NAME};
  891. RemoveInternalTimer($hash);
  892. $hash->{ConnectionState} = "Disconnected";
  893. readingsSingleUpdate( $hash, "state", $hash->{ConnectionState}, 1 ) if( $hash->{ConnectionState} ne ReadingsVal($name, "state", "" ) );
  894. return if( !$hash->{CD} );
  895. Log3 $name, 2, "$name: disconnect";
  896. close($hash->{CD}) if($hash->{CD});
  897. delete($hash->{FD});
  898. delete($hash->{CD});
  899. delete($selectlist{$name});
  900. $hash->{LAST_DISCONNECT} = FmtDateTime( gettimeofday() );
  901. }
  902. sub
  903. harmony_connect($)
  904. {
  905. my ($hash) = @_;
  906. my $name = $hash->{NAME};
  907. return if( AttrVal($hash->{NAME}, "disable", 0) );
  908. harmony_disconnect($hash);
  909. Log3 $name, 4, "$name: connect";
  910. harmony_getLoginToken($hash);
  911. if( defined($hash->{helper}{UserAuthToken}) ) {
  912. my $timeout = $hash->{TIMEOUT} ? $hash->{TIMEOUT} : 3;
  913. my $conn = IO::Socket::INET->new(PeerAddr => "$hash->{ip}:5222", Timeout => $timeout);
  914. if( $conn ) {
  915. Log3 $name, 3, "$name: connected";
  916. $hash->{ConnectionState} = "Connected";
  917. readingsSingleUpdate( $hash, "state", $hash->{ConnectionState}, 1 ) if( $hash->{ConnectionState} ne ReadingsVal($name, "state", "" ) );
  918. $hash->{LAST_CONNECT} = FmtDateTime( gettimeofday() );
  919. $hash->{FD} = $conn->fileno();
  920. $hash->{CD} = $conn; # sysread / close won't work on fileno
  921. $hash->{CONNECTS}++;
  922. $selectlist{$name} = $hash;
  923. $hash->{helper}{PARTIAL} = "";
  924. harmony_send($hash, "<stream:stream to='connect.logitech.com' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'>");
  925. } else {
  926. harmony_disconnect( $hash );
  927. InternalTimer(gettimeofday()+10, "harmony_connect", $hash, 0);
  928. }
  929. }
  930. }
  931. sub
  932. harmony_getSessionToken($)
  933. {
  934. my ($hash) = @_;
  935. my $name = $hash->{NAME};
  936. my $unique_id = '1vm7ATw/tN6HXGpQcCs/A5MkuvI';
  937. my $device = 'iOS6.0.1#iPhone';
  938. harmony_sendPair($hash, "token=$hash->{helper}{UserAuthToken}:name=$unique_id#$device");
  939. }
  940. sub
  941. harmony_send($$)
  942. {
  943. my ($hash, $data) = @_;
  944. my $name = $hash->{NAME};
  945. return undef if( !$hash->{CD} );
  946. Log3 $name, 4, "$name: send: $data";
  947. syswrite $hash->{CD}, $data;
  948. }
  949. my $id = 0;
  950. sub
  951. harmony_sendIq($$;$)
  952. {
  953. my ($hash, $xml, $type) = @_;
  954. $type = 'get' if ( !$type );
  955. ++$id;
  956. my $iq = "<iq type='$type' id='$id' from='guest'>$xml</iq>";
  957. $iq = "<iq type='$type' id='$id'>$xml</iq>";
  958. harmony_send($hash,$iq);
  959. }
  960. sub
  961. harmony_sendPair($$)
  962. {
  963. my ($hash, $payload) = @_;
  964. $payload = '' if ( !$payload );
  965. my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.pair'>$payload</oa>";
  966. harmony_sendIq($hash,$xml);
  967. }
  968. sub
  969. harmony_sendEngineGet($$;$)
  970. {
  971. my ($hash, $endpoint, $payload) = @_;
  972. $payload = '' if ( !$payload );
  973. my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.engine?$endpoint'>$payload</oa>";
  974. harmony_sendIq($hash,$xml);
  975. }
  976. sub
  977. harmony_sendHID($;$)
  978. {
  979. my ($hash, $code) = @_;
  980. if( !$code ) {
  981. return if( !$hash->{hid} );
  982. my $char = substr($hash->{hid}, 0, 1);
  983. $hash->{hid} = substr($hash->{hid}, 1);
  984. if( $char eq '\\' || ord($char) == 0xC3 ) {
  985. $char .= substr($hash->{hid}, 0, 1);
  986. $hash->{hid} = substr($hash->{hid}, 1);
  987. }
  988. $code = harmony_char2hid( $char );
  989. }
  990. my $xml = "<oa xmlns='connect.logitech.com' mime='hid.report' token=''>{'code':'$code'}</oa>";
  991. harmony_sendIq($hash,$xml);
  992. }
  993. sub
  994. harmony_sendEngineRender($$$)
  995. {
  996. my ($hash, $endpoint, $payload) = @_;
  997. #my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.engine?$endpoint' token=''>$payload</oa>";
  998. my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.engine?$endpoint'>$payload</oa>";
  999. harmony_sendIq($hash,$xml, "render");
  1000. }
  1001. sub
  1002. harmony_ping($)
  1003. {
  1004. my( $hash ) = @_;
  1005. return if( $hash->{ConnectionState} eq "Disconnected" );
  1006. ++$id;
  1007. harmony_send($hash, "<iq type='get' id='ping-$id'><ping xmlns='urn:xmpp:ping'/></iq>");
  1008. RemoveInternalTimer($hash);
  1009. InternalTimer(gettimeofday()+50, "harmony_ping", $hash, 0);
  1010. }
  1011. sub
  1012. harmony_dispatch($$$)
  1013. {
  1014. my ($param, $err, $data) = @_;
  1015. my $hash = $param->{hash};
  1016. my $name = $hash->{NAME};
  1017. if( $err ) {
  1018. Log3 $name, 2, "$name: http request failed: $err";
  1019. } elsif( $data ) {
  1020. Log3 $name, 4, "$name: $data";
  1021. if( $data !~ m/^{.*}$/ ) {
  1022. Log3 $name, 2, "$name: invalid json detected: $data";
  1023. return undef;
  1024. }
  1025. my $json = harmony_decode_json($data);
  1026. if( $param->{type} eq 'token' ) {
  1027. harmony_parseToken($hash, $json);
  1028. } elsif( $param->{type} eq 'GetJson2Uris' ) {
  1029. Log 1, Dumper $json;
  1030. }
  1031. }
  1032. }
  1033. sub
  1034. harmony_autocreate($;$)
  1035. {
  1036. my($hash, $param) = @_;
  1037. my $name = $hash->{NAME};
  1038. return if( !defined($hash->{config}) );
  1039. my $id = $param;
  1040. $id = harmony_idOfDevice($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  1041. return "unknown device $param" if( $param && !$id );
  1042. #foreach my $d (keys %defs) {
  1043. # next if($defs{$d}{TYPE} ne "autocreate");
  1044. # return undef if(AttrVal($defs{$d}{NAME},"disable",undef));
  1045. #}
  1046. my $autocreated = 0;
  1047. foreach my $device (@{$hash->{config}->{device}}) {
  1048. next if( $id && $device->{id} != $id );
  1049. if( defined($modules{$hash->{TYPE}}{defptr}{$device->{id}}) ) {
  1050. Log3 $name, 4, "$name: device '$device->{id}' already defined";
  1051. next;
  1052. }
  1053. my $devname = "harmony_". $device->{id};
  1054. my $define = "$devname harmony DEVICE $device->{id}";
  1055. Log3 $name, 3, "$name: create new device '$devname' for device '$device->{id}'";
  1056. my $cmdret = CommandDefine(undef,$define);
  1057. if($cmdret) {
  1058. Log3 $name, 1, "$name: Autocreate: An error occurred while creating device for id '$device->{id}': $cmdret";
  1059. } else {
  1060. $cmdret = CommandAttr(undef,"$devname alias $device->{label}") if( defined($device->{label}) );
  1061. $cmdret = CommandAttr(undef,"$devname event-on-change-reading .*");
  1062. $cmdret = CommandAttr(undef,"$devname room $hash->{TYPE}");
  1063. $cmdret = CommandAttr(undef,"$devname stateFormat power");
  1064. #$cmdret = CommandAttr(undef,"$devname IODev $name");
  1065. $autocreated++;
  1066. }
  1067. }
  1068. CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
  1069. return "created $autocreated devices";
  1070. }
  1071. sub
  1072. harmony_parseToken($$)
  1073. {
  1074. my($hash, $json) = @_;
  1075. my $name = $hash->{NAME};
  1076. RemoveInternalTimer($hash);
  1077. my $error = $json->{ErrorCode};
  1078. if( $error && $error != 200 ) {
  1079. Log3 $name, 2, "$name: error ($error): $json->{Message}";
  1080. $hash->{lastError} = $json->{Message};
  1081. }
  1082. my $had_token = $hash->{helper}{UserAuthToken};
  1083. $hash->{helper}{AccountId} = $json->{GetUserAuthTokenResult}->{AccountId};
  1084. $hash->{helper}{UserAuthToken} = $json->{GetUserAuthTokenResult}->{UserAuthToken};
  1085. if( $hash->{helper}{UserAuthToken} ) {
  1086. $hash->{ConnectionState} = "GotToken";
  1087. } else {
  1088. $hash->{STATE} = "Error";
  1089. $hash->{ConnectionState} = "Error";
  1090. RemoveInternalTimer($hash);
  1091. InternalTimer(gettimeofday()+60, "harmony_connect", $hash, 0);
  1092. }
  1093. }
  1094. sub
  1095. harmony_data2string($)
  1096. {
  1097. my ($data) = @_;
  1098. return "" if( !defined($data) );
  1099. return $data if( !ref($data) );
  1100. return $data if( ref($data) =~ m/JSON::..::Boolean/ );
  1101. return "[". join(',', @{$data}) ."]" if(ref($data) eq "ARRAY");
  1102. return Dumper $data;
  1103. }
  1104. sub
  1105. harmony_GetPower($$)
  1106. {
  1107. my ($hash, $activity) = @_;
  1108. my $power = "";
  1109. return $power if( !defined($activity->{fixit}) );
  1110. foreach my $id (keys %{$activity->{fixit}}) {
  1111. my $label = harmony_labelOfDevice($hash, $id);
  1112. my $state = $activity->{fixit}->{$id}->{Power};
  1113. $state = "Manual" if( !$state );
  1114. $power .= "\n\t\t\t$label: $state";
  1115. }
  1116. return $power;
  1117. }
  1118. sub
  1119. harmony_Get($$@)
  1120. {
  1121. my ($hash, $name, $cmd, @params) = @_;
  1122. my ($param) = @params;
  1123. #$cmd = lc( $cmd );
  1124. my $list = "";
  1125. if( defined($hash->{id}) ) {
  1126. if( !$hash->{hub} ) {
  1127. $hash->{hub} = harmony_hubOfDevice($hash->{id});
  1128. return "no IODev found for device $name ($hash->{id})" if( !$hash->{hub} );
  1129. }
  1130. if( $cmd eq "commands" || $cmd eq "deviceCommands" ) {
  1131. $cmd = "deviceCommands";
  1132. $param = $hash->{id};
  1133. $hash = $defs{$hash->{hub}};
  1134. } else {
  1135. $list = "commands:noArg";
  1136. return "Unknown argument $cmd, choose one of $list";
  1137. }
  1138. }
  1139. my $ret;
  1140. if( $cmd eq "activities" ) {
  1141. return "no activities found" if( !defined($hash->{config}) || !defined($hash->{config}->{activity}) );
  1142. my $ret = "";
  1143. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  1144. next if( $activity->{id} == -1 );
  1145. $ret .= "\n" if( $ret );
  1146. $ret .= sprintf( "%s\t%-24s", $activity->{id}, $activity->{label});
  1147. foreach my $param (@params) {
  1148. $ret .= "\t". harmony_data2string($activity->{$param}) if( $param && defined($activity->{$param}) );
  1149. }
  1150. if( $param && $param eq "power" ) {
  1151. my $power = harmony_GetPower($hash, $activity);
  1152. $ret .= $power if( $power );
  1153. }
  1154. }
  1155. if( my $activity = harmony_activityOfId($hash, -1) ) {
  1156. $ret .= "\n-1\t\t$activity->{label}";
  1157. if( $param && $param eq "power" ) {
  1158. my $power = harmony_GetPower($hash, $activity);
  1159. $ret .= $power if( $power );
  1160. }
  1161. }
  1162. #$ret = sprintf("%s\t\t%-24s\n", "ID", "LABEL"). $ret if( $ret );
  1163. return $ret;
  1164. } elsif( $cmd eq "devices" ) {
  1165. return "no devices found" if( !defined($hash->{config}) || !defined($hash->{config}->{device}) );
  1166. my $ret = "";
  1167. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  1168. $ret .= "\n" if( $ret );
  1169. $ret .= sprintf( "%s\t%-20s\t%-20s\t%-15s\t%-15s", $device->{id}, $device->{label}, $device->{type}, $device->{manufacturer}, $device->{model});
  1170. foreach my $param (@params) {
  1171. $ret .= "\t". harmony_data2string($device->{$param}) if( $param && defined($device->{$param}) );
  1172. }
  1173. }
  1174. #$ret = sprintf("%s\t\t%-20s\t%-20s\t%-15s\t%-15s\n", "ID", "LABEL", "TYPE", "MANUFACTURER", "MODEL"). $ret if( $ret );
  1175. return $ret;
  1176. } elsif( $cmd eq "commands" ) {
  1177. return "no commands found" if( !defined($hash->{config}) || !defined($hash->{config}->{activity}) );
  1178. my $id = $param;
  1179. $id = harmony_idOfActivity($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  1180. return "unknown activity $param" if( $param && !$id );
  1181. my $ret = "";
  1182. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  1183. next if( $activity->{id} == -1 );
  1184. next if( $id && $activity->{id} != $id );
  1185. $ret .= "$activity->{label}\n";
  1186. #$ret .= "$device->{label}\t$device->{manufacturer}\t$device->{model}\n";
  1187. foreach my $group (@{$activity->{controlGroup}}) {
  1188. $ret .= "\t$group->{name}\n";
  1189. foreach my $function (@{$group->{function}}) {
  1190. my $action = harmony_decode_json($function->{action});
  1191. $ret .= sprintf( "\t\t%-20s\t%s (%s)\n", $function->{name}, $function->{label}, harmony_labelOfDevice($hash, $action->{deviceId}, $action->{deviceId}) );
  1192. }
  1193. }
  1194. }
  1195. return $ret;
  1196. } elsif( $cmd eq "deviceCommands" ) {
  1197. return "no commands found" if( !defined($hash->{config}) || !defined($hash->{config}->{device}) );
  1198. my $id = $param;
  1199. $id = harmony_idOfDevice($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  1200. return "unknown device $param" if( $param && !$id );
  1201. my $ret = "";
  1202. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  1203. next if( $id && $device->{id} != $id );
  1204. $ret .= "$device->{label}\t$device->{manufacturer}\t$device->{model}\n";
  1205. foreach my $group (@{$device->{controlGroup}}) {
  1206. $ret .= "\t$group->{name}\n";
  1207. foreach my $function (@{$group->{function}}) {
  1208. $ret .= sprintf( "\t\t%-20s\t%s\n", $function->{name}, $function->{label} );
  1209. }
  1210. }
  1211. }
  1212. return "no commands found" if( !$ret );
  1213. return $ret;
  1214. } elsif( $cmd eq "activityDetail"
  1215. || $cmd eq "deviceDetail" ) {
  1216. return undef if( !defined($hash->{config}) );
  1217. $param = harmony_idOfActivity($hash, $param) if( $param && $param !~ m/^([\d-])+$/ && $cmd eq "activityDetail" );
  1218. $param = harmony_idOfDevice($hash, $param) if( $param && $param !~ m/^([\d-])+$/ && $cmd eq "deviceDetail" );
  1219. my $var;
  1220. $var = $hash->{config}->{activity} if( $cmd eq "activityDetail" );
  1221. $var = $hash->{config}->{device} if( $cmd eq "deviceDetail" );
  1222. if( $param ) {
  1223. foreach my $v (@{$var}) {
  1224. if( $v->{id} eq $param ) {
  1225. $var = $v;
  1226. last;
  1227. }
  1228. }
  1229. }
  1230. return Dumper $var;
  1231. } elsif( $cmd eq "configDetail" ) {
  1232. return undef if( !defined($hash->{config}) );
  1233. return Dumper $hash->{config};
  1234. } elsif( $cmd eq "currentActivity" ) {
  1235. return "unknown activity" if( !$hash->{currentActivityID} );
  1236. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  1237. return "unknown activity" if( !$activity );
  1238. return $activity->{label};
  1239. }
  1240. $list .= "activities:noArg devices:noArg";
  1241. if( $hash->{config} ) {
  1242. return undef if( !defined($hash->{config}) );
  1243. my $activities;
  1244. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  1245. next if( $activity->{id} == -1 );
  1246. $activities .= "," if( $activities );
  1247. $activities .= $activity->{label};
  1248. }
  1249. if( $activities ) {
  1250. $activities =~ s/ /./g;
  1251. $list .= " commands:,$activities";
  1252. }
  1253. my $devices;
  1254. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  1255. $devices .= "," if( $devices );
  1256. $devices .= $device->{label};
  1257. }
  1258. if( $devices ) {
  1259. $devices =~ s/ /./g;
  1260. $list .= " deviceCommands:,$devices";
  1261. }
  1262. }
  1263. if( $cmd eq 'showAccount' ) {
  1264. my $user = $hash->{helper}{username};
  1265. my $password = $hash->{helper}{password};
  1266. return 'no user set' if( !$user );
  1267. return 'no password set' if( !$password );
  1268. $user = harmony_decrypt( $user );
  1269. $password = harmony_decrypt( $password );
  1270. return "user: $user\npassword: $password";
  1271. }
  1272. $list .= " showAccount";
  1273. $list .= " currentActivity:noArg";
  1274. $list =~ s/^ //;
  1275. return "Unknown argument $cmd, choose one of $list";
  1276. }
  1277. sub
  1278. harmony_Attr($$$)
  1279. {
  1280. my ($cmd, $name, $attrName, $attrVal) = @_;
  1281. my $orig = $attrVal;
  1282. if( $attrName eq "disable" ) {
  1283. my $hash = $defs{$name};
  1284. RemoveInternalTimer($hash);
  1285. if( $cmd eq "set" && $attrVal ne "0" ) {
  1286. $attrVal = 1;
  1287. harmony_disconnect($hash);
  1288. } else {
  1289. $attr{$name}{$attrName} = 0;
  1290. harmony_connect($hash);
  1291. }
  1292. }
  1293. if( $cmd eq "set" ) {
  1294. if( !defined($orig) || $orig ne $attrVal ) {
  1295. $attr{$name}{$attrName} = $attrVal;
  1296. return $attrName ." set to ". $attrVal;
  1297. }
  1298. }
  1299. return;
  1300. }
  1301. sub
  1302. harmony_encrypt($)
  1303. {
  1304. my ($decoded) = @_;
  1305. my $key = getUniqueId();
  1306. my $encoded;
  1307. return $decoded if( $decoded =~ /^crypt:(.*)/ );
  1308. for my $char (split //, $decoded) {
  1309. my $encode = chop($key);
  1310. $encoded .= sprintf("%.2x",ord($char)^ord($encode));
  1311. $key = $encode.$key;
  1312. }
  1313. return 'crypt:'. $encoded;
  1314. }
  1315. sub
  1316. harmony_decrypt($)
  1317. {
  1318. my ($encoded) = @_;
  1319. my $key = getUniqueId();
  1320. my $decoded;
  1321. $encoded = $1 if( $encoded =~ /^crypt:(.*)/ );
  1322. for my $char (map { pack('C', hex($_)) } ($encoded =~ /(..)/g)) {
  1323. my $decode = chop($key);
  1324. $decoded .= chr(ord($char)^ord($decode));
  1325. $key = $decode.$key;
  1326. }
  1327. return $decoded;
  1328. }
  1329. 1;
  1330. =pod
  1331. =item summary module for logitech harmony hub based remots
  1332. =item summary_DE Modul für Logitech Harmony Hub basierte Fernbedienungen
  1333. =begin html
  1334. <a name="harmony"></a>
  1335. <h3>harmony</h3>
  1336. <ul>
  1337. Defines a device to integrate a Logitech Harmony Hub based remote control into fhem.<br><br>
  1338. It is possible to: start and stop activities, send ir commands to devices, send keyboard input by bluetooth and
  1339. smart keyboard usb dongles.<br><br>
  1340. You probably want to use it in conjunction with the <a href="#fakeRoku">fakeRoku</a> module.<br><br>
  1341. Notes:
  1342. <ul>
  1343. <li>JSON has to be installed on the FHEM host.</li>
  1344. <li>For hubs with firmware version 3.x.y &lt;username&gt; and &lt;password&gt; are not required as no authentication
  1345. with the logitech myharmony server is needed for the full functionality of this module.</li>
  1346. <li>For hubs with firmware version 4.x.y &lt;username&gt; and &lt;password&gt; are required for device level control.
  1347. Activit level control is (currently) still possible without authentication.</li>
  1348. <li>activity and device names can be given as id or name. names can be given as a regex and spaces in names musst be replaced by a single '.' (dot).</li>
  1349. </ul><br>
  1350. <a name="harmony_Define"></a>
  1351. <b>Define</b>
  1352. <ul>
  1353. <code>define &lt;name&gt; harmony [&lt;username&gt; &lt;password&gt;] &lt;ip&gt;</code><br>
  1354. <br>
  1355. Defines a harmony device.<br><br>
  1356. Examples:
  1357. <ul>
  1358. <code>define hub harmony 10.0.1.4</code><br>
  1359. </ul>
  1360. </ul><br>
  1361. <a name="harmony_Readings"></a>
  1362. <b>Readings</b>
  1363. <ul>
  1364. <li>currentActivity<br>
  1365. the name of the currently selected activity.</li>
  1366. <li>previousActivity<br>
  1367. the name of the previous selected activity. does not trigger an event.</li>
  1368. <li>newVersion<br>
  1369. will be set if a new firmware version is avaliable.</li>
  1370. </ul><br>
  1371. <a name="harmony_Internals"></a>
  1372. <b>Internals</b>
  1373. <ul>
  1374. <li>currentActivityID<br>
  1375. the id of the currently selected activity.</li>
  1376. <li>previousActivityID<br>
  1377. the id of the previous selected activity.</li>
  1378. <li>sleeptimer<br>
  1379. timeout for sleeptimer if any is set.</li>
  1380. </ul><br>
  1381. <a name="harmony_Set"></a>
  1382. <b>Set</b>
  1383. <ul>
  1384. <li>activity &lt;id&gt;|&ltname&gt; [&lt;channel&gt;]<br>
  1385. switch to this activit and optionally switch to &lt;channel&gt;</li>
  1386. <li>channel &lt;channel&gt;<br>
  1387. switch to &lt;channel&gt; in the current activity</li>
  1388. <li>command [&lt;id&gt;|&ltname&gt;] &lt;command&gt;<br>
  1389. send the given ir command for the current activity or for the given device</li>
  1390. <li>getConfig<br>
  1391. request the configuration from the hub</li>
  1392. <li>getCurrentActivity<br>
  1393. request the current activity from the hub</li>
  1394. <li>off<br>
  1395. switch current activity off</li>
  1396. <li>reconnect [all]<br>
  1397. close connection to the hub and reconnect, if <code>all</code> is given also reconnect to the logitech server</li>
  1398. <li>sleeptimer [&lt;timeout&gt;]<br>
  1399. &lt;timeout&gt; -> timeout in minutes<br>
  1400. -1 -> timer off<br>
  1401. default -> 60 minutes</li>
  1402. <li>sync<br>
  1403. syncs the hub to the myHarmony config</li>
  1404. <li>hidDevice [&lt;id&gt;|&ltname&gt;]<br>
  1405. sets the target device for keyboard commands, if no device is given -> set the target to the
  1406. default device for the current activity.</li>
  1407. <li>text &lt;text&gt;<br>
  1408. sends &lt;text&gt; by bluetooth/smart keaboard dongle. a-z ,A-Z ,0-9, \n, \e, \t and space are currently possible</li>
  1409. <li>cursor &lt;direction&gt;<br>
  1410. moves the cursor by bluetooth/smart keaboard dongle. &lt;direction&gt; can be one of: up, down, left, right, pageUp, pageDown, home, end.</li>
  1411. <li>special &lt;key&gt;<br>
  1412. sends special key by bluetooth/smart keaboard dongle. &lt;key&gt; can be one of: previousTrack, nextTrack, stop, playPause, volumeUp, volumeDown, mute.</li>
  1413. <li>autocreate [&lt;id&gt;|&ltname&gt;]<br>
  1414. creates a fhem device for a single/all device(s) in the harmony hub. if activities are startet the state
  1415. of these devices will be updatet with the power state defined in these activites.</li>
  1416. <li>update<br>
  1417. triggers a firmware update. only available if a new firmware is available.</li>
  1418. </ul>
  1419. The command, hidDevice, text, cursor and special commmands are also available for the autocreated devices. The &lt;id&gt;|&ltname&gt; paramter hast to be omitted.<br><br>
  1420. <a name="harmony_Get"></a>
  1421. <b>Get</b>
  1422. <ul>
  1423. <li>activites [&lt;param&gt;]<br>
  1424. lists all activities<br>
  1425. parm = power -> list power state for each device in activity</li>
  1426. <li>devices [&lt;param&gt;]<br>
  1427. lists all devices</li>
  1428. <li>commands [&lt;id&gt;|&ltname&gt;]<br>
  1429. lists the commands for the specified activity or for all activities</li>
  1430. <li>deviceCommands [&lt;id&gt;|&ltname&gt;]<br>
  1431. lists the commands for the specified device or for all devices</li>
  1432. <li>activityDetail [&lt;id&gt;|&ltname&gt;]</li>
  1433. <li>deviceDetail [&lt;id&gt;|&ltname&gt;]</li>
  1434. <li>configDetail</li>
  1435. <li>currentActivity<br>
  1436. returns the current activity name</li>
  1437. <li>showAccount<br>
  1438. display obfuscated user and password in cleartext</li>
  1439. </ul>
  1440. The commands commmand is also available for the autocreated devices. The &lt;id&gt;|&ltname&gt; paramter hast to be omitted.<br><br>
  1441. <a name="harmony_Attr"></a>
  1442. <b>Attributes</b>
  1443. <ul>
  1444. <li>disable<br>
  1445. 1 -> disconnect from the hub</li>
  1446. </ul>
  1447. </ul>
  1448. =end html
  1449. =cut