70_PHTV.pm 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746
  1. ###############################################################################
  2. # $Id: 70_PHTV.pm 14400 2017-05-28 11:10:33Z loredo $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Data::Dumper;
  7. use Time::HiRes qw(gettimeofday);
  8. use HttpUtils;
  9. use Color;
  10. use Encode;
  11. # initialize ##################################################################
  12. sub PHTV_Initialize($) {
  13. my ($hash) = @_;
  14. Log3 $hash, 5, "PHTV_Initialize: Entering";
  15. $hash->{DefFn} = "PHTV_Define";
  16. $hash->{UndefFn} = "PHTV_Undefine";
  17. $hash->{SetFn} = "PHTV_Set";
  18. $hash->{GetFn} = "PHTV_Get";
  19. $hash->{NotifyFn} = "PHTV_Notify";
  20. $hash->{AttrList} =
  21. "disable:0,1 disabledForIntervals do_not_notify:1,0 timeout sequentialQuery:0,1 drippyFactor:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 inputs ambiHueLeft ambiHueRight ambiHueTop ambiHueBottom ambiHueLatency:150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000 jsversion:1,5,6 macaddr:textField model wakeupCmd:textField channelsMax:slider,30,1,200 httpLoglevel:1,2,3,4,5 sslVersion device_id auth_key "
  22. . $readingFnAttributes;
  23. $data{RC_layout}{PHTV_SVG} = "PHTV_RClayout_SVG";
  24. $data{RC_layout}{PHTV} = "PHTV_RClayout";
  25. $data{RC_makenotify}{PHTV} = "PHTV_RCmakenotify";
  26. # 98_powerMap.pm support
  27. $hash->{powerMap} = {
  28. model => {
  29. '55PFL8008S/12' => {
  30. rname_E => 'energy',
  31. rname_P => 'consumption',
  32. map => {
  33. stateAV => {
  34. absent => 0,
  35. off => 0.1,
  36. '*' => 90,
  37. },
  38. },
  39. },
  40. },
  41. };
  42. FHEM_colorpickerInit();
  43. }
  44. # regular Fn ##################################################################
  45. sub PHTV_Define($$) {
  46. my ( $hash, $def ) = @_;
  47. my @a = split( "[ \t][ \t]*", $def );
  48. my $name = $hash->{NAME};
  49. Log3 $name, 5, "PHTV $name: called function PHTV_Define()";
  50. eval {
  51. require JSON;
  52. import JSON qw( decode_json encode_json );
  53. };
  54. return "Please install Perl JSON to use module PHTV"
  55. if ($@);
  56. if ( int(@a) < 3 ) {
  57. my $msg =
  58. "Wrong syntax: define <name> PHTV <ip-or-hostname> [<poll-interval>]";
  59. Log3 $name, 4, $msg;
  60. return $msg;
  61. }
  62. $hash->{NOTIFYDEV} = "global";
  63. my $address = $a[2];
  64. $hash->{helper}{ADDRESS} = $address;
  65. # use interval of 45sec if not defined
  66. my $interval = $a[3] || 45;
  67. $hash->{INTERVAL} = $interval;
  68. readingsSingleUpdate( $hash, "ambiHue", "off", 0 )
  69. if ( ReadingsVal( $name, "ambiHue", "" ) ne "off" );
  70. $hash->{model} = ReadingsVal( $name, "model", undef )
  71. if ( ReadingsVal( $name, "model", undef ) );
  72. $hash->{swversion} = ReadingsVal( $name, "softwareversion", undef )
  73. if ( ReadingsVal( $name, "softwareversion", undef ) );
  74. # set default settings on first define
  75. if ( $init_done && !defined( $hash->{OLDDEF} ) ) {
  76. fhem 'attr ' . $name . ' webCmd volume:input:rgb';
  77. fhem 'attr ' . $name
  78. . ' devStateIcon on:rc_GREEN:off off:rc_YELLOW:on absent:rc_STOP:on';
  79. fhem 'attr ' . $name . ' icon it_television';
  80. PHTV_GetStatus($hash);
  81. }
  82. return;
  83. }
  84. sub PHTV_Undefine($$) {
  85. my ( $hash, $arg ) = @_;
  86. my $name = $hash->{NAME};
  87. Log3 $name, 5, "PHTV $name: called function PHTV_Undefine()";
  88. # Stop the internal GetStatus-Loop and exit
  89. RemoveInternalTimer($hash);
  90. return;
  91. }
  92. sub PHTV_Set($@);
  93. sub PHTV_Set($@) {
  94. my ( $hash, @a ) = @_;
  95. my $name = $hash->{NAME};
  96. my $state = ReadingsVal( $name, "state", "Initialized" );
  97. my $channel = ReadingsVal( $name, "channel", "" );
  98. my $channels = "";
  99. my $inputs_txt = "";
  100. $hash->{helper}{lastInput} = ReadingsVal( $name, "input", "-" );
  101. my $input = $hash->{helper}{lastInput};
  102. Log3 $name, 5, "PHTV $name: called function PHTV_Set()";
  103. return "No Argument given" unless ( defined( $a[1] ) );
  104. # depending on current FHEMWEB instance's allowedCommands,
  105. # restrict set commands if there is "set-user" in it
  106. my $adminMode = 1;
  107. my $FWallowedCommands = 0;
  108. $FWallowedCommands = AttrVal( $FW_wname, "allowedCommands", 0 )
  109. if ( defined($FW_wname) );
  110. if ( $FWallowedCommands && $FWallowedCommands =~ m/\bset-user\b/ ) {
  111. $adminMode = 0;
  112. return "Forbidden command: set " . $a[1]
  113. if ( lc( $a[1] ) eq "statusrequest" );
  114. }
  115. # Input alias handling
  116. if ( AttrVal( $name, "inputs", "" ) ne "" ) {
  117. my @inputs = split( ':', AttrVal( $name, "inputs", ":" ) );
  118. $inputs_txt = "-," if ( $state ne "on" );
  119. if (@inputs) {
  120. foreach (@inputs) {
  121. if (m/[^,\s]+(,[^,\s]+)+/) {
  122. my @input_names = split( ',', $_ );
  123. $inputs_txt .= $input_names[1] . ",";
  124. $input_names[1] =~ s/\s/_/g;
  125. $hash->{helper}{device}{inputAliases}{ $input_names[0] } =
  126. $input_names[1];
  127. $hash->{helper}{device}{inputNames}{ $input_names[1] } =
  128. $input_names[0];
  129. }
  130. else {
  131. $inputs_txt .= $_ . ",";
  132. }
  133. }
  134. }
  135. $inputs_txt =~ s/\s/_/g;
  136. $inputs_txt = substr( $inputs_txt, 0, -1 );
  137. }
  138. # load channel list
  139. if ( defined( $hash->{helper}{device}{channelPreset} )
  140. && ref( $hash->{helper}{device}{channelPreset} ) eq "HASH" )
  141. {
  142. my $i = 1;
  143. my $count = scalar( keys %{ $hash->{helper}{device}{channelPreset} } );
  144. my $channelsMax = AttrVal( $name, "channelsMax", "80" );
  145. $count = $channelsMax if ( $count > $channelsMax );
  146. while ( $i <= $count ) {
  147. if ( defined( $hash->{helper}{device}{channelPreset}{$i}{name} )
  148. && $hash->{helper}{device}{channelPreset}{$i}{name} ne "" )
  149. {
  150. $channels .=
  151. $hash->{helper}{device}{channelPreset}{$i}{name} . ",";
  152. }
  153. $i++;
  154. }
  155. }
  156. if ( $channel ne ""
  157. && $channels !~ /$channel/ )
  158. {
  159. $channels = $channel . "," . $channels . ",";
  160. }
  161. chop($channels) if ( $channels ne "" );
  162. # create inputList reading for frontends
  163. readingsSingleUpdate( $hash, "inputList", $inputs_txt, 1 )
  164. if ( ReadingsVal( $name, "inputList", "-" ) ne $inputs_txt );
  165. # create channelList reading for frontends
  166. readingsSingleUpdate( $hash, "channelList", $channels, 1 )
  167. if ( ReadingsVal( $name, "channelList", "-" ) ne $channels );
  168. my $usage =
  169. "Unknown argument "
  170. . $a[1]
  171. . ", choose one of toggle:noArg on:noArg off:noArg play:noArg pause:noArg stop:noArg record:noArg volume:slider,1,1,100 volumeUp:noArg volumeDown:noArg channelUp:noArg channelDown:noArg remoteControl ambiHue:off,on ambiMode:internal,manual,expert ambiPreset:rainbow,rainbow-pastel rgb:colorpicker,rgb hue:slider,0,1,65534 sat:slider,0,1,255 pct:slider,0,1,100 bri:slider,0,1,255";
  172. $usage .=
  173. " volumeStraight:slider,"
  174. . $hash->{helper}{audio}{min} . ",1,"
  175. . $hash->{helper}{audio}{max}
  176. if ( defined( $hash->{helper}{audio}{min} )
  177. && defined( $hash->{helper}{audio}{max} ) );
  178. $usage .= " mute:-,on,off"
  179. if ( ReadingsVal( $name, "mute", "" ) eq "-" );
  180. $usage .= " mute:on,off"
  181. if ( ReadingsVal( $name, "mute", "" ) ne "-" );
  182. $usage .= " input:" . $inputs_txt if ( $inputs_txt ne "" );
  183. $usage .= " channel:$channels" if ( $channels ne "" );
  184. if ($adminMode) {
  185. $usage .= " statusRequest:noArg";
  186. }
  187. $usage = "" if ( $state eq "Initialized" );
  188. $usage = "pin" if ( $state =~ /^pairing.*/ );
  189. my $cmd = '';
  190. my $result;
  191. # pairing grant / PIN
  192. if ( lc( $a[1] ) eq "pin" ) {
  193. return "Missing PIN code" unless ( defined( $a[2] ) );
  194. return "Not in pairing mode"
  195. unless ( defined( $hash->{pairing} )
  196. && defined( $hash->{pairing}{auth_key} )
  197. && defined( $hash->{pairing}{timestamp} )
  198. && defined( $hash->{pairing}{request} )
  199. && defined( $hash->{pairing}{request}{device} ) );
  200. readingsSingleUpdate( $hash, "state", "pairing-grant", 1 );
  201. $hash->{pairing}{grant} = {
  202. auth => {
  203. auth_AppId => 1,
  204. pin => trim( $a[2] ),
  205. auth_timestamp => $hash->{pairing}{timestamp},
  206. auth_signature => PHTV_createAuthSignature(
  207. $hash->{pairing}{timestamp},
  208. $a[2],
  209. "ZmVay1EQVFOaZhwQ4Kv81ypLAZNczV9sG4KkseXWn1NEk6cXmPKO/MCa9sryslvLCFMnNe4Z4CPXzToowvhHvA=="
  210. ),
  211. },
  212. device => $hash->{pairing}{request}{device},
  213. };
  214. return "Unable to sign pairing confirmation."
  215. . " Please install Digest::SHA first."
  216. unless ( defined( $hash->{pairing}{grant}{auth}{auth_signature} ) );
  217. PHTV_SendCommand( $hash, "pair/grant", $hash->{pairing}{grant} );
  218. }
  219. # statusRequest
  220. elsif ( lc( $a[1] ) eq "statusrequest" ) {
  221. Log3 $name, 3, "PHTV set $name " . $a[1];
  222. delete $hash->{helper}{device}
  223. if ( defined( $hash->{helper}{device} ) );
  224. delete $hash->{helper}{supportedAPIcmds}
  225. if ( defined( $hash->{helper}{supportedAPIcmds} ) );
  226. PHTV_GetStatus($hash);
  227. }
  228. # toggle
  229. elsif ( lc( $a[1] ) eq "toggle" ) {
  230. Log3 $name, 3, "PHTV set $name " . $a[1];
  231. if ( ReadingsVal( $name, "state", "off" ) ne "on" ) {
  232. return PHTV_Set( $hash, $name, "on" );
  233. }
  234. else {
  235. return PHTV_Set( $hash, $name, "off" );
  236. }
  237. }
  238. # on
  239. elsif ( lc( $a[1] ) eq "on" ) {
  240. if ( ReadingsVal( $name, "state", "absent" ) eq "absent" ) {
  241. Log3 $name, 3, "PHTV set $name " . $a[1] . " (wakeup)";
  242. my $wakeupCmd = AttrVal( $name, "wakeupCmd", "" );
  243. my $macAddr = AttrVal( $name, "macaddr", "" );
  244. if ( $wakeupCmd ne "" ) {
  245. $wakeupCmd =~ s/\$DEVICE/$name/g;
  246. $wakeupCmd =~ s/\$MACADDR/$macAddr/g;
  247. if ( $wakeupCmd =~ s/^[ \t]*\{|\}[ \t]*$//g ) {
  248. Log3 $name, 4,
  249. "PHTV executing wake-up command (Perl): $wakeupCmd";
  250. $result = eval $wakeupCmd;
  251. }
  252. else {
  253. Log3 $name, 4,
  254. "PHTV executing wake-up command (fhem): $wakeupCmd";
  255. $result = fhem $wakeupCmd;
  256. }
  257. }
  258. elsif ( $macAddr ne "" && $macAddr ne "-" ) {
  259. $hash->{helper}{wakeup} = 1;
  260. PHTV_wake($hash);
  261. RemoveInternalTimer($hash);
  262. InternalTimer( gettimeofday() + 35, "PHTV_GetStatus", $hash,
  263. 0 );
  264. return "wake-up command sent";
  265. }
  266. else {
  267. return
  268. "Attribute macaddr not set. Device needs to be reachable to turn it on.";
  269. }
  270. }
  271. elsif ( ReadingsVal( $name, "state", "off" ) eq "off" ) {
  272. Log3 $name, 3, "PHTV set $name " . $a[1];
  273. $cmd = PHTV_GetRemotecontrolCommand("STANDBY");
  274. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"',
  275. "on" );
  276. }
  277. }
  278. # off
  279. elsif ( lc( $a[1] ) eq "off" ) {
  280. Log3 $name, 3, "PHTV set $name " . $a[1];
  281. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  282. $cmd = PHTV_GetRemotecontrolCommand("STANDBY");
  283. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"',
  284. "off" );
  285. }
  286. }
  287. # ambiHue
  288. elsif ( lc( $a[1] ) eq "ambihue" ) {
  289. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  290. return "No argument given" unless ( defined( $a[2] ) );
  291. return "Device does not seem to support Ambilight"
  292. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  293. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  294. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  295. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  296. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  297. if ( lc( $a[2] ) eq "on" ) {
  298. return
  299. "No configuration found. Please set ambiHue attributes first."
  300. unless ( AttrVal( $name, "ambiHueLeft", undef )
  301. || AttrVal( $name, "ambiHueRight", undef )
  302. || AttrVal( $name, "ambiHueTop", undef )
  303. || AttrVal( $name, "ambiHueBottom", undef ) );
  304. # enable internal Ambilight color
  305. PHTV_SendCommand( $hash, "ambilight/mode",
  306. '"current": "internal"', "internal" )
  307. if ( ReadingsVal( $name, "ambiMode", "internal" ) ne
  308. "internal" );
  309. PHTV_SendCommand( $hash, "ambilight/processed", undef, "init" );
  310. }
  311. elsif ( lc( $a[2] ) eq "off" ) {
  312. readingsSingleUpdate( $hash, "ambiHue", $a[2], 1 )
  313. if ( ReadingsVal( $name, "ambiHue", "off" ) ne $a[2] );
  314. }
  315. else {
  316. return "Unknown argument given";
  317. }
  318. }
  319. else {
  320. return "Device needs to be ON to turn on Ambilight+Hue.";
  321. }
  322. }
  323. # ambiMode
  324. elsif ( lc( $a[1] ) eq "ambimode" ) {
  325. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  326. return "No argument given" unless ( defined( $a[2] ) );
  327. return "Device does not seem to support Ambilight"
  328. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  329. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  330. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  331. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  332. if ( ReadingsVal( $name, "state", "absent" ) ne "absent" ) {
  333. if ( lc( $a[2] ) eq "internal"
  334. || lc( $a[2] ) eq "manual"
  335. || lc( $a[2] ) eq "expert" )
  336. {
  337. PHTV_SendCommand( $hash, "ambilight/mode",
  338. '"current": "' . $a[2] . '"', $a[2] );
  339. readingsSingleUpdate( $hash, "rgb", "000000", 1 )
  340. if ( lc( $a[2] ) eq "internal" );
  341. }
  342. else {
  343. return
  344. "Unknown argument given, choose one of internal manual expert";
  345. }
  346. }
  347. else {
  348. return "Device needs to be reachable to control Ambilight mode.";
  349. }
  350. }
  351. # ambiPreset
  352. elsif ( lc( $a[1] ) eq "ambipreset" ) {
  353. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  354. return "No argument given" unless ( defined( $a[2] ) );
  355. return "Device does not seem to support Ambilight"
  356. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  357. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  358. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  359. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  360. if ( ReadingsVal( $name, "state", "absent" ) ne "absent" ) {
  361. if ( ReadingsVal( $name, "ambiLEDLayers", undef ) ) {
  362. my $json;
  363. # rainbow
  364. if ( lc( $a[2] ) eq "rainbow" ) {
  365. my $layer = ( $a[3] ) ? $a[3] : 1;
  366. return "Layer $layer is not numeric"
  367. if ( !PHTV_isinteger($layer) );
  368. return "Layer $layer is not existing"
  369. if (
  370. $layer > ReadingsVal( $name, "ambiLEDLayers", undef ) );
  371. while (
  372. $layer <= ReadingsVal( $name, "ambiLEDLayers", undef ) )
  373. {
  374. my $rgb;
  375. foreach my $side ( 'Left', 'Top', 'Right', 'Bottom' ) {
  376. my $ambiLED = "ambiLED$side";
  377. my $side = lc($side);
  378. my $l = "layer" . $layer;
  379. if ( ReadingsVal( $name, $ambiLED, 0 ) > 0 ) {
  380. $rgb = { "r" => 255, "g" => 0, "b" => 0 }
  381. if ( $side eq "left"
  382. || $side eq "right" );
  383. # run clockwise for left and top
  384. if ( $side eq "left" || $side eq "top" ) {
  385. my $led = 0;
  386. while ( $led <=
  387. ReadingsVal( $name, $ambiLED, 0 ) - 1 )
  388. {
  389. $json->{$l}{$side}{$led}{r} =
  390. $rgb->{r};
  391. $json->{$l}{$side}{$led}{g} =
  392. $rgb->{g};
  393. $json->{$l}{$side}{$led}{b} =
  394. $rgb->{b};
  395. if ( $rgb->{r} == 255 ) {
  396. $rgb = {
  397. "r" => 0,
  398. "g" => 255,
  399. "b" => 0
  400. };
  401. }
  402. elsif ( $rgb->{g} == 255 ) {
  403. $rgb = {
  404. "r" => 0,
  405. "g" => 0,
  406. "b" => 255
  407. };
  408. }
  409. elsif ( $rgb->{b} == 255 ) {
  410. $rgb = {
  411. "r" => 255,
  412. "g" => 0,
  413. "b" => 0
  414. };
  415. }
  416. $led++;
  417. }
  418. }
  419. # run anti-clockwise for right and bottom
  420. elsif ($side eq "right"
  421. || $side eq "bottom" )
  422. {
  423. my $led =
  424. ReadingsVal( $name, $ambiLED, 0 ) - 1;
  425. while ( $led >= 0 ) {
  426. $json->{$l}{$side}{$led}{r} =
  427. $rgb->{r};
  428. $json->{$l}{$side}{$led}{g} =
  429. $rgb->{g};
  430. $json->{$l}{$side}{$led}{b} =
  431. $rgb->{b};
  432. if ( $rgb->{r} == 255 ) {
  433. $rgb = {
  434. "r" => 0,
  435. "g" => 255,
  436. "b" => 0
  437. };
  438. }
  439. elsif ( $rgb->{g} == 255 ) {
  440. $rgb = {
  441. "r" => 0,
  442. "g" => 0,
  443. "b" => 255
  444. };
  445. }
  446. elsif ( $rgb->{b} == 255 ) {
  447. $rgb = {
  448. "r" => 255,
  449. "g" => 0,
  450. "b" => 0
  451. };
  452. }
  453. $led--;
  454. }
  455. }
  456. }
  457. }
  458. last if ( defined( $a[3] ) );
  459. $layer++;
  460. }
  461. # enable manual Ambilight color
  462. PHTV_SendCommand( $hash, "ambilight/mode",
  463. '"current": "manual"', "manual" )
  464. if ( ReadingsVal( $name, "ambiMode", "manual" ) ne
  465. "manual" );
  466. }
  467. # rainbow-pastel
  468. elsif ( lc( $a[2] ) eq "rainbow-pastel" ) {
  469. my $layer = ( $a[3] ) ? $a[3] : 1;
  470. return "Layer $layer is not numeric"
  471. if ( !PHTV_isinteger($layer) );
  472. return "Layer $layer is not existing"
  473. if ( $layer > ReadingsVal( $name, "ambiLEDLayers", 0 ) );
  474. PHTV_Set( $hash, $name, "ambiPreset", "rainbow" );
  475. # enable manual Ambilight color
  476. PHTV_SendCommand( $hash, "ambilight/mode",
  477. '"current": "expert"',
  478. "expert", 0.5 )
  479. if ( ReadingsVal( $name, "ambiMode", "expert" ) ne
  480. "expert" );
  481. }
  482. # unknown preset
  483. else {
  484. return "Unknown preset, choose one of rainbow";
  485. }
  486. PHTV_SendCommand( $hash, "ambilight/cached", $json );
  487. }
  488. else {
  489. return "Devices does not seem to support Ambilight.";
  490. }
  491. }
  492. else {
  493. return "Device needs to be reachable to control Ambilight mode.";
  494. }
  495. }
  496. # rgb
  497. elsif ( lc( $a[1] ) eq "rgb" ) {
  498. Log3 $name, 4, "PHTV set $name " . $a[1] . " " . $a[2];
  499. return "No argument given" unless ( defined( $a[2] ) );
  500. return "Device does not seem to support Ambilight"
  501. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  502. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  503. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  504. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  505. if ( ReadingsVal( $name, "state", "absent" ) ne "absent" ) {
  506. # set all LEDs at once
  507. if ( uc( $a[2] ) =~ /^(..)(..)(..)$/ ) {
  508. my $json;
  509. my $hsb;
  510. my $hue;
  511. my $sat;
  512. my $bri;
  513. my $pct;
  514. my ( $r, $g, $b ) = ( hex($1), hex($2), hex($3) );
  515. my $rgbsum = $r + $g + $b;
  516. $json .= '"r": ' . $r . ',';
  517. $json .= '"g": ' . $g . ',';
  518. $json .= '"b": ' . $b;
  519. $hsb = PHTV_rgb2hsb( $r, $g, $b );
  520. $hue = $hsb->{h};
  521. $sat = $hsb->{s};
  522. $bri = $hsb->{b};
  523. $pct = PHTV_bri2pct($bri);
  524. PHTV_SendCommand( $hash, "ambilight/cached", $json,
  525. uc( $a[2] ) );
  526. # enable manual Ambilight color if RGB!=000000
  527. PHTV_SendCommand( $hash, "ambilight/mode",
  528. '"current": "manual"', "manual" )
  529. if (
  530. ReadingsVal( $name, "ambiMode", "internal" ) eq "internal"
  531. && $rgbsum > 0 );
  532. # disable manual Ambilight color if RGB=000000
  533. PHTV_SendCommand( $hash, "ambilight/mode",
  534. '"current": "internal"', "internal" )
  535. if (
  536. ReadingsVal( $name, "ambiMode", "internal" ) ne "internal"
  537. && $rgbsum == 0 );
  538. readingsBeginUpdate($hash);
  539. readingsBulkUpdateIfChanged( $hash, "pct", $pct );
  540. readingsBulkUpdateIfChanged( $hash, "level", $pct . " %" );
  541. readingsBulkUpdateIfChanged( $hash, "hue", $hue );
  542. readingsBulkUpdateIfChanged( $hash, "sat", $sat );
  543. readingsBulkUpdateIfChanged( $hash, "bri", $bri );
  544. readingsBulkUpdateIfChanged( $hash, "rgb", uc( $a[2] ) );
  545. readingsEndUpdate( $hash, 1 );
  546. }
  547. # direct control per LED
  548. elsif ( uc( $a[2] ) =~ /^L[1-9].*/ ) {
  549. my $json;
  550. my $rgbsum = 0;
  551. my $i = 2;
  552. while ( exists( $a[$i] ) ) {
  553. my ( $layer, $side, $led, $rgb );
  554. my ( $addr, $hex ) = split( ':', $a[$i] );
  555. # calculate LED address
  556. $layer = "layer" . substr( $addr, 1, 1 )
  557. if ( length($addr) > 1
  558. && PHTV_isinteger( substr( $addr, 1, 1 ) ) );
  559. if ( length($addr) > 2 ) {
  560. $side = "left" if ( substr( $addr, 2, 1 ) eq "L" );
  561. $side = "top" if ( substr( $addr, 2, 1 ) eq "T" );
  562. $side = "right" if ( substr( $addr, 2, 1 ) eq "R" );
  563. $side = "bottom"
  564. if ( substr( $addr, 2, 1 ) eq "B" );
  565. }
  566. $led = substr( $addr, 3 )
  567. if ( length($addr) > 3
  568. && PHTV_isinteger( substr( $addr, 3 ) ) );
  569. # get desired color
  570. if ( defined($hex) ) {
  571. if ( $hex =~ /^(..)(..)(..)$/ ) {
  572. $rgb = PHTV_hex2rgb($hex);
  573. }
  574. else {
  575. return
  576. "Color "
  577. . $hex
  578. . " for address "
  579. . $addr
  580. . " is not in HEX format";
  581. }
  582. }
  583. else {
  584. return
  585. "Please add color in HEX format for address $addr";
  586. }
  587. # update json hash
  588. if ( defined( $rgb->{r} )
  589. && defined( $rgb->{g} )
  590. && defined( $rgb->{b} ) )
  591. {
  592. $rgbsum += $rgb->{r} + $rgb->{g} + $rgb->{b};
  593. if ( defined($led)
  594. && defined($side)
  595. && defined($layer) )
  596. {
  597. $json->{$layer}{$side}{$led}{r} = $rgb->{r};
  598. $json->{$layer}{$side}{$led}{g} = $rgb->{g};
  599. $json->{$layer}{$side}{$led}{b} = $rgb->{b};
  600. }
  601. elsif ( defined($side) && defined($layer) ) {
  602. $json->{$layer}{$side}{r} = $rgb->{r};
  603. $json->{$layer}{$side}{g} = $rgb->{g};
  604. $json->{$layer}{$side}{b} = $rgb->{b};
  605. }
  606. elsif ( defined($layer) ) {
  607. $json->{$layer}{r} = $rgb->{r};
  608. $json->{$layer}{g} = $rgb->{g};
  609. $json->{$layer}{b} = $rgb->{b};
  610. }
  611. else {
  612. return "Invalid LED address format " . $addr;
  613. }
  614. }
  615. $i++;
  616. }
  617. PHTV_SendCommand( $hash, "ambilight/cached", $json );
  618. # enable manual Ambilight color if RGB!=000000
  619. PHTV_SendCommand( $hash, "ambilight/mode",
  620. '"current": "manual"', "manual" )
  621. if (
  622. ReadingsVal( $name, "ambiMode", "internal" ) eq "internal"
  623. && $rgbsum > 0 );
  624. }
  625. else {
  626. return "Invalid RGB code " . $a[2];
  627. }
  628. }
  629. else {
  630. return "Device needs to be reachable to set Ambilight color.";
  631. }
  632. }
  633. # hue
  634. elsif ( lc( $a[1] ) eq "hue" ) {
  635. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  636. return "No argument given" unless ( defined( $a[2] ) );
  637. return "Device does not seem to support Ambilight"
  638. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  639. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  640. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  641. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  642. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  643. if ( ReadingsVal( $name, "rgb", "" ) ne "" ) {
  644. my $hsb;
  645. my $hex;
  646. if ( $a[2] =~ m/^\d+$/ && $a[2] >= 0 && $a[2] <= 65535 ) {
  647. $hsb = PHTV_hex2hsb( ReadingsVal( $name, "rgb", "" ) );
  648. $hex = PHTV_hsb2hex( $a[2], $hsb->{s}, $hsb->{b} );
  649. Log3 $name, 4,
  650. "PHTV $name hue - old: "
  651. . ReadingsVal( $name, "rgb", "" )
  652. . " new: $hex(h="
  653. . $a[2] . " s="
  654. . $hsb->{s} . " b="
  655. . $hsb->{b};
  656. return PHTV_Set( $hash, $name, "rgb", $hex );
  657. }
  658. else {
  659. return
  660. "Argument does not seem to be a valid integer between 0 and 100";
  661. }
  662. }
  663. }
  664. else {
  665. return "Device needs to be ON to set Ambilight color.";
  666. }
  667. }
  668. # sat
  669. elsif ( lc( $a[1] ) eq "sat" ) {
  670. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  671. return "No argument given" unless ( defined( $a[2] ) );
  672. return "Device does not seem to support Ambilight"
  673. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  674. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  675. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  676. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  677. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  678. if ( ReadingsVal( $name, "rgb", "" ) ne "" ) {
  679. my $hsb;
  680. my $hex;
  681. if ( $a[2] =~ m/^\d+$/ && $a[2] >= 0 && $a[2] <= 255 ) {
  682. $hsb = PHTV_hex2hsb( ReadingsVal( $name, "rgb", "" ) );
  683. $hex = PHTV_hsb2hex( $hsb->{h}, $a[2], $hsb->{b} );
  684. Log3 $name, 4,
  685. "PHTV $name sat - old: "
  686. . ReadingsVal( $name, "rgb", "" )
  687. . " new: $hex(h="
  688. . $hsb->{h} . " s="
  689. . $a[2] . " b="
  690. . $hsb->{b};
  691. return PHTV_Set( $hash, $name, "rgb", $hex );
  692. }
  693. else {
  694. return
  695. "Argument does not seem to be a valid integer between 0 and 100";
  696. }
  697. }
  698. }
  699. else {
  700. return "Device needs to be ON to set Ambilight color.";
  701. }
  702. }
  703. # bri
  704. elsif ( lc( $a[1] ) eq "bri" ) {
  705. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  706. return "No argument given" unless ( defined( $a[2] ) );
  707. return "Device does not seem to support Ambilight"
  708. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  709. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  710. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  711. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  712. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  713. if ( ReadingsVal( $name, "rgb", "" ) ne "" ) {
  714. my $hsb;
  715. my $hex;
  716. if ( $a[2] =~ m/^\d+$/ && $a[2] >= 0 && $a[2] <= 255 ) {
  717. $hsb = PHTV_hex2hsb( ReadingsVal( $name, "rgb", "" ) );
  718. $hex = PHTV_hsb2hex( $hsb->{h}, $hsb->{s}, $a[2] );
  719. Log3 $name, 4,
  720. "PHTV $name bri - old: "
  721. . ReadingsVal( $name, "rgb", "" )
  722. . " new: $hex(h="
  723. . $hsb->{h} . " s="
  724. . $hsb->{s} . " b="
  725. . $a[2] . ")";
  726. return PHTV_Set( $hash, $name, "rgb", $hex );
  727. }
  728. else {
  729. return
  730. "Argument does not seem to be a valid integer between 0 and 100";
  731. }
  732. }
  733. }
  734. else {
  735. return "Device needs to be ON to set Ambilight color.";
  736. }
  737. }
  738. # pct
  739. elsif ( lc( $a[1] ) eq "pct" ) {
  740. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  741. return "No argument given" unless ( defined( $a[2] ) );
  742. return "Device does not seem to support Ambilight"
  743. if ( ReadingsVal( $name, "ambiLEDBottom", 0 ) == 0
  744. && ReadingsVal( $name, "ambiLEDLeft", 0 ) == 0
  745. && ReadingsVal( $name, "ambiLEDRight", 0 ) == 0
  746. && ReadingsVal( $name, "ambiLEDTop", 0 ) == 0 );
  747. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  748. if ( ReadingsVal( $name, "rgb", "" ) ne "" ) {
  749. my $hsb;
  750. my $bri;
  751. my $hex;
  752. if ( $a[2] =~ m/^\d+$/ && $a[2] >= 0 && $a[2] <= 100 ) {
  753. $hsb = PHTV_hex2hsb( ReadingsVal( $name, "rgb", "" ) );
  754. $bri = PHTV_pct2bri( $a[2] );
  755. $hex = PHTV_hsb2hex( $hsb->{h}, $hsb->{s}, $bri );
  756. Log3 $name, 4,
  757. "PHTV $name pct - old: "
  758. . ReadingsVal( $name, "rgb", "" )
  759. . " new: $hex(h="
  760. . $hsb->{h} . " s="
  761. . $hsb->{s}
  762. . " b=$bri)";
  763. return PHTV_Set( $hash, $name, "rgb", $hex );
  764. }
  765. else {
  766. return
  767. "Argument does not seem to be a valid integer between 0 and 100";
  768. }
  769. }
  770. }
  771. else {
  772. return "Device needs to be ON to set Ambilight color.";
  773. }
  774. }
  775. # volume
  776. elsif ( lc( $a[1] ) eq "volume" ) {
  777. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  778. return "No argument given" unless ( defined( $a[2] ) );
  779. my $vol;
  780. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  781. if ( $a[2] =~ m/^\d+$/ && $a[2] >= 1 && $a[2] <= 100 ) {
  782. if ( defined( $hash->{helper}{audio}{min} )
  783. && defined( $hash->{helper}{audio}{max} ) )
  784. {
  785. $vol = int(
  786. ( $a[2] / 100 * $hash->{helper}{audio}{max} ) + 0.5 );
  787. }
  788. else {
  789. $vol = $a[2];
  790. }
  791. $cmd = '"current": ' . $vol;
  792. }
  793. else {
  794. return
  795. "Argument does not seem to be a valid integer between 1 and 100";
  796. }
  797. PHTV_SendCommand( $hash, "audio/volume", $cmd );
  798. readingsBeginUpdate($hash);
  799. readingsBulkUpdateIfChanged( $hash, "volume", $a[2] );
  800. readingsBulkUpdateIfChanged( $hash, "volumeStraight", $vol )
  801. if ( defined($vol) );
  802. readingsEndUpdate( $hash, 1 );
  803. }
  804. else {
  805. return "Device needs to be ON to adjust volume.";
  806. }
  807. }
  808. # volumeStraight
  809. elsif ( lc( $a[1] ) eq "volumestraight" ) {
  810. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  811. return "No argument given" unless ( defined( $a[2] ) );
  812. my $vol;
  813. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  814. if ( $a[2] =~ m/^\d+$/
  815. && $a[2] >= $hash->{helper}{audio}{min}
  816. && $a[2] <= $hash->{helper}{audio}{max} )
  817. {
  818. $vol =
  819. int( ( $a[2] / $hash->{helper}{audio}{max} * 100 ) + 0.5 );
  820. $cmd = '"current": ' . $a[2];
  821. }
  822. else {
  823. return
  824. "Argument does not seem to be a valid integer between "
  825. . $hash->{helper}{audio}{min} . " and "
  826. . $hash->{helper}{audio}{max};
  827. }
  828. PHTV_SendCommand( $hash, "audio/volume", $cmd );
  829. readingsBeginUpdate($hash);
  830. readingsBulkUpdateIfChanged( $hash, "volume", $vol );
  831. readingsBulkUpdateIfChanged( $hash, "volumeStraight", $a[2] );
  832. readingsEndUpdate( $hash, 1 );
  833. }
  834. else {
  835. return "Device needs to be ON to adjust volume.";
  836. }
  837. }
  838. # volumeUp/volumeDown
  839. elsif ( lc( $a[1] ) =~ /^(volumeup|volumedown)$/ ) {
  840. Log3 $name, 3, "PHTV set $name " . $a[1];
  841. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  842. if ( lc( $a[1] ) eq "volumeup" ) {
  843. $cmd = PHTV_GetRemotecontrolCommand("VOLUP");
  844. }
  845. else {
  846. $cmd = PHTV_GetRemotecontrolCommand("VOLDOWN");
  847. }
  848. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"',
  849. "volume" );
  850. }
  851. else {
  852. return "Device needs to be ON to adjust volume.";
  853. }
  854. }
  855. # mute
  856. elsif ( lc( $a[1] ) eq "mute" || lc( $a[1] ) eq "mutet" ) {
  857. if ( defined( $a[2] ) ) {
  858. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  859. }
  860. else {
  861. Log3 $name, 3, "PHTV set $name " . $a[1];
  862. }
  863. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  864. if ( !defined( $a[2] ) || $a[2] eq "toggle" ) {
  865. if ( ReadingsVal( $name, "mute", "" ) eq "off" ) {
  866. $cmd = '"muted": true';
  867. readingsSingleUpdate( $hash, "mute", "on", 1 );
  868. }
  869. elsif ( ReadingsVal( $name, "mute", "" ) eq "on" ) {
  870. $cmd = '"muted": false';
  871. readingsSingleUpdate( $hash, "mute", "off", 1 );
  872. }
  873. }
  874. elsif ( lc( $a[2] ) eq "off" ) {
  875. if ( ReadingsVal( $name, "mute", "" ) ne "off" ) {
  876. $cmd = '"muted": false';
  877. readingsSingleUpdate( $hash, "mute", "off", 1 );
  878. }
  879. }
  880. elsif ( lc( $a[2] ) eq "on" ) {
  881. if ( ReadingsVal( $name, "mute", "" ) ne "on" ) {
  882. $cmd = '"muted": true';
  883. readingsSingleUpdate( $hash, "mute", "on", 1 );
  884. }
  885. }
  886. else {
  887. return "Unknown argument " . $a[2];
  888. }
  889. $result = PHTV_SendCommand( $hash, "audio/volume", $cmd )
  890. if ( $cmd ne "" );
  891. }
  892. else {
  893. return "Device needs to be ON to mute/unmute audio.";
  894. }
  895. }
  896. # remoteControl
  897. elsif ( lc( $a[1] ) eq "remotecontrol" ) {
  898. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  899. if ( ReadingsVal( $name, "state", "absent" ) ne "absent" ) {
  900. unless ( defined( $a[2] ) ) {
  901. my $commandKeys = "";
  902. for (
  903. sort keys %{
  904. PHTV_GetRemotecontrolCommand(
  905. "GetRemotecontrolCommands")
  906. }
  907. )
  908. {
  909. $commandKeys = $commandKeys . " " . $_;
  910. }
  911. return "No argument given, choose one of" . $commandKeys;
  912. }
  913. $cmd = PHTV_GetRemotecontrolCommand( uc( $a[2] ) );
  914. if ( uc( $a[2] ) eq "MUTE" ) {
  915. PHTV_Set( $hash, $name, "mute" );
  916. }
  917. elsif ( uc( $a[2] ) eq "CHANUP" ) {
  918. PHTV_Set( $hash, $name, "channelUp" );
  919. }
  920. elsif ( uc( $a[2] ) eq "CHANDOWN" ) {
  921. PHTV_Set( $hash, $name, "channelDown" );
  922. }
  923. elsif ( $cmd ne "" ) {
  924. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"' );
  925. }
  926. else {
  927. my $commandKeys = "";
  928. for (
  929. sort keys %{
  930. PHTV_GetRemotecontrolCommand(
  931. "GetRemotecontrolCommands")
  932. }
  933. )
  934. {
  935. $commandKeys = $commandKeys . " " . $_;
  936. }
  937. return
  938. "Unknown argument "
  939. . $a[2]
  940. . ", choose one of"
  941. . $commandKeys;
  942. }
  943. }
  944. else {
  945. return "Device needs to be reachable to be controlled remotely.";
  946. }
  947. }
  948. # channel
  949. elsif ( lc( $a[1] ) eq "channel" ) {
  950. if ( defined( $a[2] )
  951. && ReadingsVal( $name, "presence", "absent" ) eq "present"
  952. && ReadingsVal( $name, "state", "off" ) ne "on" )
  953. {
  954. Log3 $name, 4, "PHTV $name: indirect switching request to ON";
  955. PHTV_Set( $hash, $name, "on" );
  956. }
  957. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  958. return
  959. "No argument given, choose one of channel presetNumber channelName "
  960. unless ( defined( $a[2] ) );
  961. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  962. my $channelName = $a[2];
  963. if (
  964. defined( $hash->{helper}{device}{channelID}{$channelName}{id} )
  965. )
  966. {
  967. $cmd = $hash->{helper}{device}{channelID}{$channelName}{id};
  968. readingsSingleUpdate( $hash, "channel", $channelName, 1 )
  969. if ( ReadingsVal( $name, "channel", "" ) ne $channelName );
  970. }
  971. elsif (
  972. $channelName =~ /^(\d+):(.*):$/
  973. && defined(
  974. $hash->{helper}{device}{channelPreset}{$channelName}{id}
  975. )
  976. )
  977. {
  978. $cmd =
  979. $hash->{helper}{device}{channelPreset}{$channelName}{id};
  980. }
  981. else {
  982. return "Argument " . $channelName
  983. . " is not a valid integer between 0 and 9999 or servicereference is invalid";
  984. }
  985. PHTV_SendCommand( $hash, "channels/current",
  986. '"id": "' . $cmd . '"', $cmd );
  987. }
  988. else {
  989. return
  990. "Device needs to be reachable to switch to a specific channel.";
  991. }
  992. }
  993. # channelUp/channelDown
  994. elsif ( lc( $a[1] ) =~ /^(channelup|channeldown)$/ ) {
  995. Log3 $name, 3, "PHTV set $name " . $a[1];
  996. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  997. if ( lc( $a[1] ) eq "channelup" ) {
  998. $cmd = PHTV_GetRemotecontrolCommand("CHANUP");
  999. }
  1000. else {
  1001. $cmd = PHTV_GetRemotecontrolCommand("CHANDOWN");
  1002. }
  1003. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"',
  1004. "channel" );
  1005. }
  1006. else {
  1007. return "Device needs to be ON to switch channel.";
  1008. }
  1009. }
  1010. # input
  1011. elsif ( lc( $a[1] ) eq "input" ) {
  1012. if ( defined( $a[2] )
  1013. && ReadingsVal( $name, "presence", "absent" ) eq "present"
  1014. && ReadingsVal( $name, "state", "off" ) ne "on" )
  1015. {
  1016. Log3 $name, 4, "PHTV $name: indirect switching request to ON";
  1017. PHTV_Set( $hash, $name, "on" );
  1018. }
  1019. return "No 2nd argument given" unless ( defined( $a[2] ) );
  1020. Log3 $name, 3, "PHTV set $name " . $a[1] . " " . $a[2];
  1021. # Alias handling
  1022. $a[2] = $hash->{helper}{device}{inputNames}{ $a[2] }
  1023. if ( defined( $hash->{helper}{device}{inputNames}{ $a[2] } ) );
  1024. # Resolve input ID name
  1025. my $input_id;
  1026. if ( defined( $hash->{helper}{device}{sourceID}{ $a[2] } ) ) {
  1027. $input_id = $hash->{helper}{device}{sourceID}{ $a[2] };
  1028. }
  1029. elsif ( defined( $hash->{helper}{device}{sourceName}{ $a[2] } ) ) {
  1030. $input_id = $a[2];
  1031. }
  1032. else {
  1033. return "Unknown source input '" . $a[2] . "' on that device.";
  1034. }
  1035. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  1036. PHTV_SendCommand( $hash, "sources/current",
  1037. '"id": "' . $input_id . '"', $input_id );
  1038. readingsSingleUpdate( $hash, "input", $a[2], 1 )
  1039. if ( ReadingsVal( $name, "input", "" ) ne $a[2] );
  1040. }
  1041. else {
  1042. return "Device needs to be reachable to switch input.";
  1043. }
  1044. }
  1045. # play / pause
  1046. elsif ( lc( $a[1] ) =~ /^(play|pause)$/ ) {
  1047. Log3 $name, 3, "PHTV set $name " . $a[1];
  1048. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  1049. $cmd = PHTV_GetRemotecontrolCommand("PLAYPAUSE");
  1050. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"' );
  1051. }
  1052. else {
  1053. return "Device needs to be ON to play or pause video.";
  1054. }
  1055. }
  1056. # stop
  1057. elsif ( lc( $a[1] ) eq "stop" ) {
  1058. Log3 $name, 3, "PHTV set $name " . $a[1];
  1059. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  1060. $cmd = PHTV_GetRemotecontrolCommand("STOP");
  1061. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"' );
  1062. }
  1063. else {
  1064. return "Device needs to be ON to stop video.";
  1065. }
  1066. }
  1067. # record
  1068. elsif ( lc( $a[1] ) eq "record" ) {
  1069. Log3 $name, 3, "PHTV set $name " . $a[1];
  1070. if ( ReadingsVal( $name, "state", "off" ) eq "on" ) {
  1071. $cmd = PHTV_GetRemotecontrolCommand("RECORD");
  1072. PHTV_SendCommand( $hash, "input/key", '"key": "' . $cmd . '"' );
  1073. }
  1074. else {
  1075. return "Device needs to be ON to start instant recording.";
  1076. }
  1077. }
  1078. # return usage hint
  1079. else {
  1080. return $usage;
  1081. }
  1082. return;
  1083. }
  1084. sub PHTV_Get($@) {
  1085. my ( $hash, @a ) = @_;
  1086. my $name = $hash->{NAME};
  1087. my $state = ReadingsVal( $name, "state", "Initialized" );
  1088. my $what;
  1089. Log3 $name, 5, "PHTV $name: called function PHTV_Get()";
  1090. return "argument is missing" if ( int(@a) < 2 );
  1091. return if ( $state =~ /^(pairing.*|initialized)$/i );
  1092. $what = $a[1];
  1093. if ( $what =~ /^(power|input|volume|mute|rgb)$/ ) {
  1094. return ReadingsVal( $name, $what, "no such reading: $what" );
  1095. }
  1096. else {
  1097. return
  1098. "Unknown argument $what, choose one of power:noArg input:noArg volume:noArg mute:noArg rgb:noArg ";
  1099. }
  1100. }
  1101. sub PHTV_Notify($$) {
  1102. my ( $hash, $dev_hash ) = @_;
  1103. my $name = $hash->{NAME};
  1104. my $dev = $dev_hash->{NAME};
  1105. my $TYPE = $hash->{TYPE};
  1106. return
  1107. if (
  1108. !$init_done
  1109. or IsDisabled($name)
  1110. or IsDisabled($dev)
  1111. or $name eq $dev # do not process own events
  1112. or ( !$modules{ $defs{$dev}{TYPE} }{$TYPE}
  1113. and !$defs{$dev}{$TYPE}
  1114. and $dev ne "global" )
  1115. );
  1116. my $events = deviceEvents( $dev_hash, 1 );
  1117. return unless ($events);
  1118. Log3 $name, 5, "$TYPE: Entering PHTV_Notify() for $dev";
  1119. # global events
  1120. if ( $dev eq "global" ) {
  1121. foreach my $event ( @{$events} ) {
  1122. next unless ( defined($event) );
  1123. # initialize
  1124. if ( $event =~ /^(INITIALIZED|MODIFIED)(?:\s+(.+))?$/ ) {
  1125. PHTV_GetStatus($hash)
  1126. if ( $1 eq "INITIALIZED" || $2 && $2 eq $name );
  1127. }
  1128. }
  1129. return;
  1130. }
  1131. return undef;
  1132. }
  1133. # module Fn ####################################################################
  1134. sub PHTV_GetStatus($;$) {
  1135. my ( $hash, $update ) = @_;
  1136. my $name = $hash->{NAME};
  1137. my $interval = $hash->{INTERVAL};
  1138. my $presence = ReadingsVal( $name, "presence", "absent" );
  1139. my $sequential = AttrVal( $name, "sequentialQuery", 0 );
  1140. my $querySent = 0;
  1141. Log3 $name, 5, "PHTV $name: called function PHTV_GetStatus()";
  1142. $interval = $interval * 1.6
  1143. if ( ReadingsVal( $name, "ambiHue", "off" ) eq "on" );
  1144. RemoveInternalTimer($hash);
  1145. InternalTimer( gettimeofday() + $interval, "PHTV_GetStatus", $hash, 0 );
  1146. return
  1147. if ( IsDisabled($name) );
  1148. # try to fetch only some information to check device availability
  1149. unless ($update) {
  1150. PHTV_SendCommand( $hash, "audio/volume" ) if ( $presence eq "present" );
  1151. PHTV_SendCommand( $hash, "system" ) if ( $presence eq "absent" );
  1152. # in case we should query the device gently, mark we already sent a query
  1153. $querySent = 1 if $sequential;
  1154. $hash->{helper}{sequentialQueryCounter} = 1 if $sequential;
  1155. }
  1156. # fetch other info if device is on
  1157. if ( !$querySent
  1158. && ( ReadingsVal( $name, "state", "off" ) eq "on" || $update ) )
  1159. {
  1160. # Read device info every 15 minutes only
  1161. if (
  1162. !$querySent
  1163. && $presence eq "present"
  1164. && (
  1165. !defined( $hash->{helper}{lastFullUpdate} )
  1166. || ( !$update
  1167. && $hash->{helper}{lastFullUpdate} + 900 le time() )
  1168. )
  1169. )
  1170. {
  1171. PHTV_SendCommand( $hash, "system" );
  1172. PHTV_SendCommand( $hash, "ambilight/topology" );
  1173. $querySent = 1 if $sequential;
  1174. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1175. # Update state
  1176. $hash->{helper}{lastFullUpdate} = time();
  1177. }
  1178. # read audio volume
  1179. if ( !$querySent && $update && $presence eq "absent" ) {
  1180. PHTV_SendCommand( $hash, "audio/volume" );
  1181. $querySent = 1 if $sequential;
  1182. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1183. }
  1184. # read ambilight details
  1185. if ( !$querySent ) {
  1186. # read ambilight mode
  1187. PHTV_SendCommand( $hash, "ambilight/mode" );
  1188. $querySent = 1 if $sequential;
  1189. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1190. # read ambilight RGB value
  1191. PHTV_SendCommand( $hash, "ambilight/cached" )
  1192. if ( ReadingsVal( $name, "ambiMode", "internal" ) ne "internal" );
  1193. }
  1194. # read all sources if not existing
  1195. if (
  1196. !$querySent
  1197. && ( !defined( $hash->{helper}{device}{sourceName} )
  1198. || !defined( $hash->{helper}{device}{sourceID} ) )
  1199. )
  1200. {
  1201. PHTV_SendCommand( $hash, "sources" );
  1202. $querySent = 1 if $sequential;
  1203. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1204. }
  1205. # otherwise read current source
  1206. elsif ( !$querySent ) {
  1207. PHTV_SendCommand( $hash, "sources/current" );
  1208. $querySent = 1 if $sequential;
  1209. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1210. }
  1211. # read all channels if not existing
  1212. if (
  1213. !$querySent
  1214. && ( !defined( $hash->{helper}{device}{channelName} )
  1215. || !defined( $hash->{helper}{device}{channelID} ) )
  1216. )
  1217. {
  1218. PHTV_SendCommand( $hash, "channels" );
  1219. $querySent = 1 if $sequential;
  1220. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1221. }
  1222. # otherwise read current channel
  1223. elsif ( !$querySent ) {
  1224. PHTV_SendCommand( $hash, "channels/current" );
  1225. $querySent = 1 if $sequential;
  1226. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1227. }
  1228. }
  1229. # Input alias handling
  1230. #
  1231. if ( AttrVal( $name, "inputs", "" ) ne "" ) {
  1232. my @inputs = split( ':', AttrVal( $name, "inputs", ":" ) );
  1233. if (@inputs) {
  1234. foreach (@inputs) {
  1235. if (m/[^,\s]+(,[^,\s]+)+/) {
  1236. my @input_names = split( ',', $_ );
  1237. $input_names[1] =~ s/\s/_/g;
  1238. $hash->{helper}{device}{inputAliases}{ $input_names[0] } =
  1239. $input_names[1];
  1240. $hash->{helper}{device}{inputNames}{ $input_names[1] } =
  1241. $input_names[0];
  1242. }
  1243. }
  1244. }
  1245. }
  1246. return;
  1247. }
  1248. sub PHTV_SendCommandDelayed($) {
  1249. my ($par) = @_;
  1250. Log3 $par->{hash}->{NAME}, 5,
  1251. "PHTV "
  1252. . $par->{hash}->{NAME}
  1253. . ": called function PHTV_SendCommandDelayed()";
  1254. PHTV_SendCommand( $par->{hash}, $par->{service}, $par->{cmd},
  1255. $par->{type} );
  1256. }
  1257. sub PHTV_SendCommand($$;$$$) {
  1258. my ( $hash, $service, $cmd, $type, $delay ) = @_;
  1259. my $name = $hash->{NAME};
  1260. my $address = $hash->{helper}{ADDRESS};
  1261. my $protoV = AttrVal( $name, "jsversion", 1 );
  1262. my $device_id = AttrVal( $name, "device_id", undef );
  1263. my $auth_key = AttrVal( $name, "auth_key", undef );
  1264. my $timestamp = gettimeofday();
  1265. my $data;
  1266. my $timeout;
  1267. if ( defined($delay) && $delay > 0 ) {
  1268. my %par = (
  1269. hash => $hash,
  1270. service => $service,
  1271. cmd => $cmd,
  1272. type => $type
  1273. );
  1274. InternalTimer( gettimeofday() + $delay,
  1275. "PHTV_SendCommandDelayed", \%par, 0 );
  1276. return;
  1277. }
  1278. if ( defined($cmd) && ref($cmd) eq "HASH" ) {
  1279. $data = encode_json($cmd);
  1280. }
  1281. elsif ( defined($cmd) && $cmd !~ /^{/ ) {
  1282. $data = "{ " . $cmd . " }";
  1283. }
  1284. Log3 $name, 5, "PHTV $name: called function PHTV_SendCommand()";
  1285. my $URL;
  1286. my $response;
  1287. my $return;
  1288. my $auth;
  1289. if ( defined( $hash->{helper}{supportedAPIcmds}{$service} )
  1290. && $hash->{helper}{supportedAPIcmds}{$service} == 0 )
  1291. {
  1292. Log3 $name, 5,
  1293. "PHTV $name: API command '" . $service . "' not supported by device.";
  1294. return;
  1295. }
  1296. if ( !defined($data) || ref($cmd) eq "HASH" && $data eq "" ) {
  1297. Log3 $name, 4, "PHTV $name: REQ $service";
  1298. }
  1299. else {
  1300. Log3 $name, 4, "PHTV $name: REQ $service/" . urlDecode($data);
  1301. }
  1302. # add missing port if required
  1303. if ( $address !~ m/^.+:[0-9]+$/ ) {
  1304. $address .= ":1925" if ( $protoV <= 5 );
  1305. $address .= ":1926" if ( $protoV > 5 );
  1306. }
  1307. # special authentication handling during pairing
  1308. $auth_key = undef if ( $service =~ /^(pair\/.+|system)/i );
  1309. $auth_key = $hash->{pairing}{auth_key}
  1310. if ( $service eq "pair/grant"
  1311. && defined( $hash->{pairing} )
  1312. && defined( $hash->{pairing}{auth_key} ) );
  1313. $auth = "$device_id:$auth_key" if ( $device_id && $auth_key );
  1314. $URL = "http://";
  1315. $URL = "https://" if ( $protoV > 5 || $address =~ m/^.+:1926$/ );
  1316. $URL .= "$auth@" if ($auth);
  1317. $URL .= $address . "/" . $protoV . "/" . $service;
  1318. $timeout = AttrVal( $name, "httpTimeout", AttrVal( $name, "timeout", 7 ) );
  1319. $timeout = 7 unless ( $timeout =~ /^\d+$/ );
  1320. # send request via HTTP-POST method
  1321. Log3 $name, 5, "PHTV $name: GET " . $URL . " (" . urlDecode($data) . ")"
  1322. if ( defined($data) && ref($cmd) ne "HASH" );
  1323. Log3 $name, 5, "PHTV $name: GET " . $URL . " (#HASH)"
  1324. if ( defined($data) && ref($cmd) eq "HASH" );
  1325. Log3 $name, 5, "PHTV $name: GET " . $URL
  1326. unless ( defined($data) );
  1327. HttpUtils_NonblockingGet(
  1328. {
  1329. url => $URL,
  1330. hideauth => 1,
  1331. auth => $auth,
  1332. digest => 1,
  1333. timeout => $timeout,
  1334. data => $data,
  1335. hash => $hash,
  1336. service => $service,
  1337. cmd => $cmd,
  1338. type => $type,
  1339. timestamp => $timestamp,
  1340. httpversion => "1.1",
  1341. callback => \&PHTV_ReceiveCommand,
  1342. loglevel => AttrVal( $name, "httpLoglevel", 4 ),
  1343. header => {
  1344. Agent => 'FHEM-PHTV/1.0.0',
  1345. 'User-Agent' => 'FHEM-PHTV/1.0.0',
  1346. Accept => 'application/json;charset=UTF-8',
  1347. 'Accept-Charset' => 'UTF-8',
  1348. },
  1349. sslargs => {
  1350. SSL_verify_mode => 'SSL_VERIFY_NONE',
  1351. },
  1352. }
  1353. );
  1354. return;
  1355. }
  1356. sub PHTV_ReceiveCommand($$$) {
  1357. my ( $param, $err, $data ) = @_;
  1358. my $hash = $param->{hash};
  1359. my $name = $hash->{NAME};
  1360. my $service = $param->{service};
  1361. my $cmd = $param->{cmd};
  1362. my $code = $param->{code} ? $param->{code} : 0;
  1363. my $sequential = AttrVal( $name, "sequentialQuery", 0 );
  1364. my $protoV = AttrVal( $name, "jsversion", 1 );
  1365. my $device_id = AttrVal( $name, "device_id", undef );
  1366. my $state = ReadingsVal( $name, "state", "" );
  1367. my $newstate = "absent";
  1368. my $type = $param->{type} ? $param->{type} : "";
  1369. my $return;
  1370. Log3 $name, 5, "PHTV $name: called function PHTV_ReceiveCommand()";
  1371. readingsBeginUpdate($hash);
  1372. # pairing request reply
  1373. if ( $service eq "pair/request" ) {
  1374. my $errtype = "TIMEOUT";
  1375. $errtype = "ERROR/$code" if ($code);
  1376. my $log;
  1377. my $loglevel = 4;
  1378. if ( $data && $data =~ m/^\s*([{\[][\s\S]+[}\]])\s*$/i ) {
  1379. $return = decode_json( Encode::encode_utf8($1) ) if ($1);
  1380. if ( ref($return) eq "HASH"
  1381. && $return->{timestamp}
  1382. && $return->{auth_key}
  1383. && $return->{timeout}
  1384. && $return->{error_id}
  1385. && $return->{error_id} =~ m/^SUCCESS$/i )
  1386. {
  1387. $log = "$code - Pairing enabled";
  1388. $loglevel = 3;
  1389. readingsBulkUpdate( $hash, "state", "pairing" );
  1390. $hash->{PAIRING_BEGIN} = time();
  1391. $hash->{PAIRING_END} =
  1392. $hash->{PAIRING_BEGIN} + $return->{timeout};
  1393. $hash->{pairing}{auth_key} = $return->{auth_key};
  1394. $hash->{pairing}{timeout} = $return->{timeout};
  1395. $hash->{pairing}{timestamp} = $return->{timestamp};
  1396. }
  1397. else {
  1398. $log = "$errtype - Pairing request failed";
  1399. $log .= "\n $err" if ( $code && $err );
  1400. $log .= "\n Data:\n$err" if ( $code && $data );
  1401. readingsBulkUpdate( $hash, "state", "pairing request failed" );
  1402. }
  1403. }
  1404. else {
  1405. $log = "$errtype - Pairing not supported";
  1406. $log .= "\n $err" if ( $code && $err );
  1407. $log .= "\n Data:\n$err" if ( $code && $data );
  1408. readingsBulkUpdate( $hash, "state", "pairing not supported" );
  1409. }
  1410. Log3 $name, $loglevel, "PHTV $name: $log";
  1411. readingsEndUpdate( $hash, 1 );
  1412. return;
  1413. }
  1414. # pairing grant reply
  1415. elsif ( $service eq "pair/grant" ) {
  1416. my $errtype = "TIMEOUT";
  1417. $errtype = "ERROR/$code" if ($code);
  1418. my $log;
  1419. my $loglevel = 4;
  1420. my $interval = 10;
  1421. if ( $data && $data =~ m/^\s*([{\[][\s\S]+[}\]])\s*$/i ) {
  1422. $return = decode_json( Encode::encode_utf8($1) ) if ($1);
  1423. if ( ref($return) eq "HASH"
  1424. && $return->{error_id}
  1425. && $return->{error_id} =~ m/^SUCCESS$/i )
  1426. {
  1427. $log = "$code - Pairing successful";
  1428. $loglevel = 3;
  1429. readingsBulkUpdate( $hash, "state", "paired" );
  1430. fhem 'attr '
  1431. . $name
  1432. . ' auth_key '
  1433. . $hash->{pairing}{auth_key};
  1434. $interval = 3;
  1435. }
  1436. else {
  1437. $log = "$errtype - Pairing failed";
  1438. $log .= "\n $err" if ( $code && $err );
  1439. $log .= "\n Data:\n$err" if ( $code && $data );
  1440. readingsBulkUpdate( $hash, "state", "pairing failed" );
  1441. }
  1442. }
  1443. else {
  1444. $log = "$errtype - Pairing grant not supported";
  1445. $log .= "\n $err" if ( $code && $err );
  1446. $log .= "\n Data:\n$err" if ( $code && $data );
  1447. readingsBulkUpdate( $hash, "state", "pairing grant not supported" );
  1448. }
  1449. Log3 $name, $loglevel, "PHTV $name: $log";
  1450. delete $hash->{pairing};
  1451. delete $hash->{PAIRING_BEGIN};
  1452. delete $hash->{PAIRING_END};
  1453. RemoveInternalTimer($hash);
  1454. InternalTimer( gettimeofday() + $interval, "PHTV_GetStatus", $hash, 0 );
  1455. readingsEndUpdate( $hash, 1 );
  1456. return;
  1457. }
  1458. # authorization/pairing needed
  1459. elsif ( $code == 401 ) {
  1460. if ( defined( $hash->{pairing} )
  1461. && defined( $hash->{PAIRING_END} )
  1462. && $hash->{PAIRING_END} < time() )
  1463. {
  1464. readingsEndUpdate( $hash, 1 );
  1465. return;
  1466. }
  1467. readingsBulkUpdateIfChanged( $hash, "presence", "present" );
  1468. readingsBulkUpdateIfChanged( $hash, "power", "on" );
  1469. readingsBulkUpdate( $hash, "state", "pairing-request" );
  1470. fhem 'attr ' . $name . ' jsversion 6'
  1471. unless ( $protoV > 1 );
  1472. unless ($device_id) {
  1473. $device_id = PHTV_createDeviceId();
  1474. fhem 'attr ' . $name . ' device_id ' . $device_id;
  1475. }
  1476. delete $hash->{pairing} if ( defined( $hash->{pairing} ) );
  1477. delete $hash->{PAIRING_BEGIN} if ( defined( $hash->{PAIRING_BEGIN} ) );
  1478. delete $hash->{PAIRING_END} if ( defined( $hash->{PAIRING_END} ) );
  1479. $hash->{pairing}{request} = {
  1480. device => {
  1481. device_name => 'fhem',
  1482. device_os => 'Android',
  1483. app_name => 'FHEM PHTV',
  1484. type => 'native',
  1485. app_id => 'org.fhem.PHTV',
  1486. id => $device_id,
  1487. },
  1488. scope => [ "read", "write", "control" ],
  1489. };
  1490. PHTV_SendCommand( $hash, "pair/request", $hash->{pairing}{request} );
  1491. readingsEndUpdate( $hash, 1 );
  1492. return;
  1493. }
  1494. # device error
  1495. elsif ( $err && ( !$code || $code ne "200" ) ) {
  1496. my $errtype = "TIMEOUT";
  1497. $errtype = "ERROR/$code" if ($code);
  1498. if ( !defined($cmd) || ref($cmd) eq "HASH" || $cmd eq "" ) {
  1499. Log3 $name, 4, "PHTV $name: RCV $errtype $service" . "\n $err";
  1500. }
  1501. else {
  1502. Log3 $name, 4,
  1503. "PHTV $name: RCV $errtype $service/"
  1504. . urlDecode($cmd)
  1505. . "\n $err";
  1506. }
  1507. # device is not reachable
  1508. if ( $service eq "audio/volume"
  1509. || $service eq "system"
  1510. || ( $service eq "input/key" && $type eq "off" ) )
  1511. {
  1512. $newstate = "off" if ($code);
  1513. readingsBulkUpdateIfChanged( $hash, "presence", "present" )
  1514. if ($code);
  1515. readingsBulkUpdateIfChanged( $hash, "presence", "absent" )
  1516. unless ($code);
  1517. }
  1518. # device behaves naughty
  1519. elsif ( $code || ( $data && $data ne "" ) ) {
  1520. $newstate = "on";
  1521. readingsBulkUpdateIfChanged( $hash, "presence", "present" );
  1522. # because it does not seem to support the command
  1523. unless ( defined( $hash->{helper}{supportedAPIcmds}{$service} ) ) {
  1524. $hash->{helper}{supportedAPIcmds}{$service} = 0;
  1525. Log3 $name, 4,
  1526. "PHTV $name: API command '"
  1527. . $service
  1528. . "' not supported by device.";
  1529. }
  1530. }
  1531. }
  1532. # data received
  1533. elsif ($data) {
  1534. readingsBulkUpdateIfChanged( $hash, "presence", "present" );
  1535. if ( !defined($cmd) || ref($cmd) eq "HASH" | $cmd eq "" ) {
  1536. Log3 $name, 4, "PHTV $name: RCV $service";
  1537. }
  1538. else {
  1539. Log3 $name, 4, "PHTV $name: RCV $service/" . urlDecode($cmd);
  1540. }
  1541. if ( $data =~
  1542. m/^\s*(([{\[][\s\S]+[}\]])|(<html>\s*<head>\s*<title>\s*Ok\s*<\/title>\s*<\/head>\s*<body>\s*Ok\s*<\/body>\s*<\/html>))\s*$/i
  1543. )
  1544. {
  1545. $return = decode_json( Encode::encode_utf8($2) ) if ($2);
  1546. $return = "ok" if ($3);
  1547. if ( !defined($cmd) || ref($cmd) eq "HASH" || $cmd eq "" ) {
  1548. Log3 $name, 5, "PHTV $name: RES $service\n" . $data;
  1549. }
  1550. else {
  1551. Log3 $name, 5,
  1552. "PHTV $name: RES $service/" . urlDecode($cmd) . "\n" . $data;
  1553. }
  1554. $hash->{helper}{supportedAPIcmds}{$service} = 1
  1555. unless ( defined( $hash->{helper}{supportedAPIcmds}{$service} )
  1556. || $service =~ /^channels\/.*/
  1557. || $service =~ /^channellists\/.*/ );
  1558. }
  1559. elsif ( $data ne "" ) {
  1560. if ( !defined($cmd) || ref($cmd) eq "HASH" || $cmd eq "" ) {
  1561. Log3 $name, 5, "PHTV $name: RES ERROR/$code $service\n" . $data;
  1562. }
  1563. else {
  1564. Log3 $name, 5,
  1565. "PHTV $name: RES ERROR/$code $service/"
  1566. . urlDecode($cmd) . "\n"
  1567. . $data;
  1568. }
  1569. unless ( defined( $hash->{helper}{supportedAPIcmds}{$service} ) ) {
  1570. $hash->{helper}{supportedAPIcmds}{$service} = 0;
  1571. Log3 $name, 4,
  1572. "PHTV $name: API command '"
  1573. . $service
  1574. . "' not supported by device.";
  1575. }
  1576. return undef;
  1577. }
  1578. #######################
  1579. # process return data
  1580. #
  1581. $newstate = "on";
  1582. if ( ref($return) eq "HASH" ) {
  1583. for ( keys %{$return} ) {
  1584. next unless ( $_ =~ /^(.*)_encrypted$/ );
  1585. my $r = $1;
  1586. $return->{$r} = $return->{$_};
  1587. }
  1588. }
  1589. # audio/volume
  1590. if ( $service eq "audio/volume" ) {
  1591. if ( ref($return) eq "HASH" ) {
  1592. # calculate volume
  1593. my $vol = ( $return->{current} ) ? $return->{current} : 0;
  1594. if ( defined( $return->{min} )
  1595. && defined( $return->{max} ) )
  1596. {
  1597. $hash->{helper}{audio}{min} = $return->{min};
  1598. $hash->{helper}{audio}{max} = $return->{max};
  1599. $vol =
  1600. int(
  1601. ( $return->{current} / $return->{max} * 100 ) + 0.5 );
  1602. }
  1603. # volume
  1604. readingsBulkUpdateIfChanged( $hash, "volume", $vol );
  1605. # volumeStraight
  1606. if ( defined( $return->{current} ) ) {
  1607. readingsBulkUpdateIfChanged( $hash, "volumeStraight",
  1608. $return->{current} );
  1609. }
  1610. if ( defined( $return->{muted} ) ) {
  1611. if ( $return->{muted} eq "false" ) {
  1612. readingsBulkUpdateIfChanged( $hash, "mute", "off" );
  1613. }
  1614. elsif ( $return->{muted} eq "true" ) {
  1615. readingsBulkUpdateIfChanged( $hash, "mute", "on" );
  1616. }
  1617. }
  1618. # send on command in case device came up and
  1619. # macaddr attribute is set
  1620. if ( $newstate eq "on"
  1621. && $newstate ne $state
  1622. && defined( $hash->{helper}{wakeup} ) )
  1623. {
  1624. Log3 $name, 4,
  1625. "PHTV $name: Wakeup successful, turning device on";
  1626. delete $hash->{helper}{wakeup};
  1627. $cmd = PHTV_GetRemotecontrolCommand("STANDBY");
  1628. PHTV_SendCommand( $hash, "input/key",
  1629. '"key": "' . $cmd . '"', "on" );
  1630. }
  1631. # trigger query cascade in case the device
  1632. # just came up or sequential query is enabled
  1633. if ( $sequential
  1634. || ( $newstate eq "on" && $newstate ne $state ) )
  1635. {
  1636. # reset API command monitoring
  1637. delete $hash->{helper}{supportedAPIcmds}
  1638. if ( defined( $hash->{helper}{supportedAPIcmds} ) );
  1639. # add some delay if the device just came up
  1640. # and user set attribut for lazy devices
  1641. if ( $newstate eq "on"
  1642. && $newstate ne $state
  1643. && AttrVal( $name, "drippyFactor", -1 ) ge 0 )
  1644. {
  1645. RemoveInternalTimer($hash);
  1646. InternalTimer(
  1647. gettimeofday() +
  1648. AttrVal( $name, "drippyFactor", 0 ),
  1649. "PHTV_GetStatus", $hash, 1
  1650. );
  1651. }
  1652. else {
  1653. PHTV_GetStatus( $hash, 1 );
  1654. }
  1655. }
  1656. }
  1657. }
  1658. # system
  1659. elsif ( $service eq "system" ) {
  1660. ######### 2013 device
  1661. # {
  1662. # "menulanguage": "English",
  1663. # "name": "Loredos TV",
  1664. # "country": "Germany",
  1665. # "serialnumber": "ZH1D1319003420",
  1666. # "softwareversion": "QF2EU-0.173.65.0",
  1667. # "model": "55PFL8008S/12"
  1668. # }
  1669. ######### 2016 device
  1670. # {
  1671. # "menulanguage" : "German", "name" : "NN", "country" : "Germany",
  1672. # "serialnumber_encrypted"
  1673. # : "9Ujg7qk0jWyPF7opK7RsswTePRBq6dZTCFwUIgF\/Q94=\n",
  1674. # "softwareversion_encrypted"
  1675. # : "mCOnRshweMpSjXKxwNxJ7h+5VzkcYTUeqY70o6lZyWE=\n",
  1676. # "model_encrypted" : "DWxvf4moWe49A7uQOri7\/LcObzUIvy9HNxMoMMp1gdE=\n",
  1677. # "deviceid_encrypted" : "rkhmRw5JDPqaZ9h2yQoOdrj\/j26uyo288XumhKsG9Ck=\n",
  1678. # "nettvversion" : "6.0.2", "epgsource" : "one",
  1679. # "api_version" : { "Major" : 6, "Minor" : 2, "Patch" : 0 },
  1680. # "featuring"
  1681. # : {
  1682. # "jsonfeatures"
  1683. # : {
  1684. # "editfavorites" : [ "TVChannels", "SatChannels" ],
  1685. # "recordings" : [ "List", "Schedule", "Manage" ],
  1686. # "ambilight" : [ "LoungeLight", "Hue", "Ambilight" ],
  1687. # "menuitems" : ["Setup_Menu"],
  1688. # "textentry"
  1689. # : [
  1690. # "context_based", "initial_string_available",
  1691. # "editor_info_available"
  1692. # ],
  1693. # "applications" : [ "TV_Apps", "TV_Games", "TV_Settings" ],
  1694. # "pointer" : ["not_available"],
  1695. # "inputkey" : ["key"],
  1696. # "activities" : ["intent"],
  1697. # "channels" : ["preset_string"],
  1698. # "mappings" : ["server_mapping"]
  1699. # },
  1700. # "systemfeatures"
  1701. # : {
  1702. # "tvtype" : "consumer",
  1703. # "content" : [ "dmr", "dms_tad" ],
  1704. # "tvsearch" : "intent",
  1705. # "pairing_type" : "digest_auth_pairing",
  1706. # "secured_transport" : "true"
  1707. # }
  1708. # }
  1709. # }
  1710. if ( ref($return) eq "HASH" ) {
  1711. # language
  1712. if ( defined( $return->{menulanguage} ) ) {
  1713. readingsBulkUpdateIfChanged( $hash, "language",
  1714. $return->{menulanguage} );
  1715. }
  1716. # name
  1717. if ( defined( $return->{name} ) ) {
  1718. readingsBulkUpdateIfChanged( $hash, "systemname",
  1719. $return->{name} );
  1720. }
  1721. # country
  1722. if ( defined( $return->{country} ) ) {
  1723. readingsBulkUpdateIfChanged( $hash, "country",
  1724. $return->{country} );
  1725. }
  1726. # serialnumber
  1727. if ( defined( $return->{serialnumber} ) ) {
  1728. readingsBulkUpdateIfChanged( $hash, "serialnumber",
  1729. $return->{serialnumber} );
  1730. }
  1731. # softwareversion
  1732. if ( defined( $return->{softwareversion} ) ) {
  1733. readingsBulkUpdateIfChanged( $hash, "softwareversion",
  1734. $return->{softwareversion} );
  1735. $hash->{swversion} = $return->{softwareversion};
  1736. }
  1737. # model
  1738. if ( defined( $return->{model} ) ) {
  1739. readingsBulkUpdateIfChanged( $hash, "model",
  1740. uc( $return->{model} ) );
  1741. $hash->{model} = uc( $return->{model} );
  1742. $attr{$name}{model} = uc( $return->{model} );
  1743. }
  1744. # epgsource
  1745. if ( defined( $return->{epgsource} ) ) {
  1746. readingsBulkUpdateIfChanged( $hash, "epgsource",
  1747. $return->{epgsource} );
  1748. }
  1749. # nettvversion
  1750. if ( defined( $return->{nettvversion} ) ) {
  1751. readingsBulkUpdateIfChanged( $hash, "nettvversion",
  1752. $return->{nettvversion} );
  1753. }
  1754. # api_version
  1755. if ( defined( $return->{api_version} )
  1756. && defined( $return->{api_version}{Major} )
  1757. && defined( $return->{api_version}{Minor} )
  1758. && defined( $return->{api_version}{Patch} ) )
  1759. {
  1760. $hash->{api_version} = $return->{api_version};
  1761. readingsBulkUpdateIfChanged( $hash, "api_version",
  1762. $return->{api_version}{Major} . "."
  1763. . $return->{api_version}{Minor} . "."
  1764. . $return->{api_version}{Patch} );
  1765. fhem 'attr '
  1766. . $name
  1767. . ' jsversion '
  1768. . $return->{api_version}{Major}
  1769. if ( $protoV ne $return->{api_version}{Major} );
  1770. }
  1771. # featuring
  1772. if ( defined( $return->{featuring} ) ) {
  1773. $hash->{featuring} = $return->{featuring};
  1774. }
  1775. }
  1776. # continue query cascade in case sequential query is enabled
  1777. PHTV_GetStatus( $hash, 1 )
  1778. if ( $sequential || $state ne $newstate );
  1779. }
  1780. # sources
  1781. elsif ( $service eq "sources" ) {
  1782. if ( ref($return) eq "HASH" ) {
  1783. # Safe input names
  1784. my $inputs;
  1785. foreach my $input (
  1786. sort
  1787. keys %{$return}
  1788. )
  1789. {
  1790. my $input_name = $return->{$input}{name};
  1791. $input_name =~ s/\s/_/g;
  1792. $hash->{helper}{device}{sourceName}{$input} =
  1793. $input_name;
  1794. $hash->{helper}{device}{sourceID}{$input_name} = $input;
  1795. $inputs .= $input_name . ":";
  1796. }
  1797. unless ( defined( AttrVal( $name, "inputs", undef ) ) ) {
  1798. $inputs = substr( $inputs, 0, -1 );
  1799. $attr{$name}{inputs} = $inputs;
  1800. }
  1801. PHTV_SendCommand( $hash, "sources/current" );
  1802. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1803. }
  1804. }
  1805. # sources/current
  1806. elsif ( $service eq "sources/current" ) {
  1807. if ( ref($return) eq "HASH" ) {
  1808. if ( defined( $return->{id} ) ) {
  1809. $return->{id} =~ s/^\s+//;
  1810. $return->{id} =~ s/\s+$//;
  1811. $cmd = (
  1812. $hash->{helper}{device}{sourceName}{ $return->{id} }
  1813. ? $hash->{helper}{device}{sourceName}{ $return->{id} }
  1814. : "-"
  1815. );
  1816. }
  1817. else {
  1818. $cmd = "-";
  1819. }
  1820. # Alias handling
  1821. $cmd = $hash->{helper}{device}{inputAliases}{$cmd}
  1822. if ( defined( $hash->{helper}{device}{inputAliases}{$cmd} ) );
  1823. readingsBulkUpdateIfChanged( $hash, "input", $cmd );
  1824. }
  1825. elsif ( $return eq "ok" ) {
  1826. $cmd =
  1827. ( $hash->{helper}{device}{sourceName}{$type} )
  1828. ? $hash->{helper}{device}{sourceName}{$type}
  1829. : $type;
  1830. # Alias handling
  1831. $cmd = $hash->{helper}{device}{inputAliases}{$cmd}
  1832. if ( defined( $hash->{helper}{device}{inputAliases}{$cmd} ) );
  1833. readingsBulkUpdateIfChanged( $hash, "input", $cmd );
  1834. }
  1835. # SEQUENTIAL QUERY CASCADE - next: channels
  1836. # read all channels if not existing
  1837. if (
  1838. $sequential
  1839. && ( !defined( $hash->{helper}{device}{channelName} )
  1840. || !defined( $hash->{helper}{device}{channelID} ) )
  1841. )
  1842. {
  1843. PHTV_SendCommand( $hash, "channels" );
  1844. $hash->{helper}{sequentialQueryCounter}++;
  1845. }
  1846. # otherwise read current channel
  1847. elsif ($sequential) {
  1848. PHTV_SendCommand( $hash, "channels/current" );
  1849. $hash->{helper}{sequentialQueryCounter}++;
  1850. }
  1851. }
  1852. # channels
  1853. elsif ( $service eq "channels" ) {
  1854. if ( ref($return) eq "HASH" ) {
  1855. # Safe channel names
  1856. foreach my $channel (
  1857. sort
  1858. keys %{$return}
  1859. )
  1860. {
  1861. my $channel_name = $return->{$channel}{name};
  1862. $channel_name =~ s/^\s+//;
  1863. $channel_name =~ s/\s+$//;
  1864. $channel_name =~ s/\s/_/g;
  1865. $channel_name =~ s/,/./g;
  1866. $channel_name =~ s///g;
  1867. if ( $channel_name ne "" ) {
  1868. $hash->{helper}{device}{channelName}{$channel}{name} =
  1869. $channel_name;
  1870. $hash->{helper}{device}{channelName}{$channel}{preset}
  1871. = $return->{$channel}{preset};
  1872. $hash->{helper}{device}{channelID}{$channel_name}{id} =
  1873. $channel;
  1874. $hash->{helper}{device}{channelID}{$channel_name}
  1875. {preset} = $return->{$channel}{preset};
  1876. $hash->{helper}{device}{channelPreset}
  1877. { $return->{$channel}{preset} }{id} = $channel;
  1878. $hash->{helper}{device}{channelPreset}
  1879. { $return->{$channel}{preset} }{name} = $channel_name;
  1880. }
  1881. }
  1882. PHTV_SendCommand( $hash, "channels/current" );
  1883. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  1884. }
  1885. }
  1886. # channels/current
  1887. elsif ( $service eq "channels/current" ) {
  1888. if ( ref($return) eq "HASH" ) {
  1889. if ( defined( $return->{id} ) ) {
  1890. $return->{id} =~ s/^\s+//;
  1891. $return->{id} =~ s/\s+$//;
  1892. $cmd =
  1893. ( $hash->{helper}{device}{channelName}
  1894. { $return->{id} }{name} )
  1895. ? $hash->{helper}{device}{channelName}
  1896. { $return->{id} }{name}
  1897. : "-";
  1898. }
  1899. else {
  1900. $cmd = "-";
  1901. }
  1902. readingsBulkUpdateIfChanged( $hash, "channel", $cmd );
  1903. # read channel details if type is known
  1904. if ( defined( $return->{id} ) && $return->{id} ne "" ) {
  1905. PHTV_SendCommand( $hash, "channels/" . $return->{id} );
  1906. $hash->{helper}{sequentialQueryCounter}++
  1907. if $sequential;
  1908. }
  1909. # read all channellists if not existing
  1910. elsif ( !defined( $hash->{helper}{device}{channellists} ) ) {
  1911. PHTV_SendCommand( $hash, "channellists" );
  1912. $hash->{helper}{sequentialQueryCounter}++
  1913. if $sequential;
  1914. }
  1915. }
  1916. elsif ( $return eq "ok" ) {
  1917. $cmd =
  1918. ( $hash->{helper}{device}{channelName}{$type} )
  1919. ? $hash->{helper}{device}{channelName}{$type}{name}
  1920. : $type;
  1921. readingsBulkUpdateIfChanged( $hash, "channel", $cmd );
  1922. # read channel details if type is known
  1923. if ( defined($type) && $type ne "" ) {
  1924. PHTV_SendCommand( $hash, "channels/" . $type );
  1925. $hash->{helper}{sequentialQueryCounter}++
  1926. if $sequential;
  1927. }
  1928. # read all channellists if not existing
  1929. elsif ( !defined( $hash->{helper}{device}{channellists} ) ) {
  1930. PHTV_SendCommand( $hash, "channellists" );
  1931. $hash->{helper}{sequentialQueryCounter}++
  1932. if $sequential;
  1933. }
  1934. }
  1935. }
  1936. # channels/id
  1937. elsif ( $service =~ /^channels\/.*/ ) {
  1938. if ( ref($return) eq "HASH" ) {
  1939. # currentMedia
  1940. if ( defined( $return->{preset} ) ) {
  1941. readingsBulkUpdateIfChanged( $hash, "currentMedia",
  1942. $return->{preset} );
  1943. }
  1944. else {
  1945. readingsBulkUpdateIfChanged( $hash, "currentMedia", "-" );
  1946. }
  1947. # servicename
  1948. if ( defined( $return->{name} ) ) {
  1949. readingsBulkUpdateIfChanged( $hash, "servicename",
  1950. $return->{name} );
  1951. }
  1952. else {
  1953. readingsBulkUpdateIfChanged( $hash, "servicename", "-" );
  1954. }
  1955. # frequency
  1956. if ( defined( $return->{frequency} ) ) {
  1957. readingsBulkUpdateIfChanged( $hash, "frequency",
  1958. $return->{frequency} );
  1959. }
  1960. else {
  1961. readingsBulkUpdateIfChanged( $hash, "frequency", "-" );
  1962. }
  1963. # onid
  1964. if ( defined( $return->{onid} ) ) {
  1965. readingsBulkUpdateIfChanged( $hash, "onid",
  1966. $return->{onid} );
  1967. }
  1968. else {
  1969. readingsBulkUpdateIfChanged( $hash, "onid", "-" );
  1970. }
  1971. # tsid
  1972. if ( defined( $return->{tsid} ) ) {
  1973. readingsBulkUpdateIfChanged( $hash, "tsid",
  1974. $return->{tsid} );
  1975. }
  1976. else {
  1977. readingsBulkUpdateIfChanged( $hash, "tsid", "-" );
  1978. }
  1979. # sid
  1980. if ( defined( $return->{sid} ) ) {
  1981. readingsBulkUpdateIfChanged( $hash, "sid", $return->{sid} );
  1982. }
  1983. else {
  1984. readingsBulkUpdateIfChanged( $hash, "sid", "-" );
  1985. }
  1986. # receiveMode
  1987. if ( defined( $return->{analog} )
  1988. && defined( $return->{digital} ) )
  1989. {
  1990. my $receiveMode =
  1991. ( $return->{analog} eq "false" )
  1992. ? $return->{digital}
  1993. : "analog";
  1994. readingsBulkUpdateIfChanged( $hash, "receiveMode",
  1995. $receiveMode );
  1996. }
  1997. else {
  1998. readingsBulkUpdateIfChanged( $hash, "receiveMode", "-" );
  1999. }
  2000. }
  2001. # read all channellists if not existing
  2002. unless ( defined( $hash->{helper}{device}{channellists} ) ) {
  2003. PHTV_SendCommand( $hash, "channellists" );
  2004. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  2005. }
  2006. }
  2007. # channellists
  2008. elsif ( $service eq "channellists" ) {
  2009. if ( ref($return) eq "HASH" ) {
  2010. # request each lists content
  2011. foreach my $item (
  2012. sort
  2013. keys %{$return}
  2014. )
  2015. {
  2016. PHTV_SendCommand( $hash, "channellists/$item", undef,
  2017. $item );
  2018. }
  2019. $hash->{helper}{sequentialQueryCounter}++ if $sequential;
  2020. }
  2021. }
  2022. # channellists/id
  2023. elsif ( $service =~ /^channellists\/.*/ ) {
  2024. if ( ref($return) eq "ARRAY" ) {
  2025. $hash->{helper}{device}{channellists}{$type} = $return;
  2026. }
  2027. }
  2028. # input/key
  2029. elsif ( $service eq "input/key" ) {
  2030. if ( ref($return) ne "HASH" && $return eq "ok" ) {
  2031. # toggle standby
  2032. if ( defined($type) && $type eq "off" ) {
  2033. $newstate = "off";
  2034. }
  2035. # toggle standby
  2036. elsif ( defined($type) && $type eq "on" ) {
  2037. $newstate = "on";
  2038. }
  2039. # volumeUp volumeDown
  2040. elsif ( defined($type) && $type eq "volume" ) {
  2041. PHTV_SendCommand( $hash, "audio/volume" );
  2042. }
  2043. # channelUp channelDown
  2044. elsif ( defined($type) && $type eq "channel" ) {
  2045. PHTV_SendCommand( $hash, "channels/current" );
  2046. }
  2047. }
  2048. }
  2049. # ambilight/topology
  2050. elsif ( $service eq "ambilight/topology" ) {
  2051. if ( ref($return) eq "HASH" ) {
  2052. if ( defined( $return->{layers} ) ) {
  2053. readingsBulkUpdateIfChanged( $hash, "ambiLEDLayers",
  2054. $return->{layers} );
  2055. }
  2056. if ( defined( $return->{left} ) ) {
  2057. readingsBulkUpdateIfChanged( $hash, "ambiLEDLeft",
  2058. $return->{left} );
  2059. }
  2060. if ( defined( $return->{top} ) ) {
  2061. readingsBulkUpdateIfChanged( $hash, "ambiLEDTop",
  2062. $return->{top} );
  2063. }
  2064. if ( defined( $return->{right} ) ) {
  2065. readingsBulkUpdateIfChanged( $hash, "ambiLEDRight",
  2066. $return->{right} );
  2067. }
  2068. if ( defined( $return->{bottom} ) ) {
  2069. readingsBulkUpdateIfChanged( $hash, "ambiLEDBottom",
  2070. $return->{bottom} );
  2071. }
  2072. }
  2073. }
  2074. # ambilight/mode
  2075. elsif ( $service eq "ambilight/mode" ) {
  2076. if ( ref($return) eq "HASH" ) {
  2077. if ( defined( $return->{current} ) ) {
  2078. readingsBulkUpdateIfChanged( $hash, "ambiMode",
  2079. $return->{current} );
  2080. }
  2081. }
  2082. elsif ( $return eq "ok" ) {
  2083. readingsBulkUpdateIfChanged( $hash, "ambiMode", $type );
  2084. }
  2085. # SEQUENTIAL QUERY CASCADE - next: sources
  2086. # read all sources if not existing
  2087. if (
  2088. $sequential
  2089. && ( !defined( $hash->{helper}{device}{sourceName} )
  2090. || !defined( $hash->{helper}{device}{sourceID} ) )
  2091. )
  2092. {
  2093. PHTV_SendCommand( $hash, "sources" );
  2094. $hash->{helper}{sequentialQueryCounter}++;
  2095. }
  2096. # otherwise read current source
  2097. elsif ($sequential) {
  2098. PHTV_SendCommand( $hash, "sources/current" );
  2099. $hash->{helper}{sequentialQueryCounter}++;
  2100. }
  2101. }
  2102. # ambilight/cached (rgb)
  2103. elsif ( $service eq "ambilight/cached" ) {
  2104. if ( ref($return) eq "HASH" ) {
  2105. my $hexsum = "";
  2106. foreach my $layer ( keys %{$return} ) {
  2107. foreach my $side ( keys %{ $return->{$layer} } ) {
  2108. foreach my $led ( keys %{ $return->{$layer}{$side} } ) {
  2109. my $hex = "";
  2110. my $l = $layer;
  2111. my $s = $side;
  2112. $l =~ s/layer/L/;
  2113. $s =~ s/left/L/ if ( $side eq "left" );
  2114. $s =~ s/top/T/ if ( $side eq "top" );
  2115. $s =~ s/right/R/ if ( $side eq "right" );
  2116. $s =~ s/bottom/B/ if ( $side eq "bottom" );
  2117. my $readingname = "rgb_" . $l . $s . $led;
  2118. $hex = PHTV_rgb2hex(
  2119. $return->{$layer}{$side}{$led}{r},
  2120. $return->{$layer}{$side}{$led}{g},
  2121. $return->{$layer}{$side}{$led}{b}
  2122. );
  2123. $hexsum = $hex if ( $hexsum eq "" );
  2124. $hexsum = "diff" if ( $hexsum ne $hex );
  2125. readingsBulkUpdateIfChanged( $hash,
  2126. $readingname, $hex );
  2127. }
  2128. }
  2129. }
  2130. if ( $hexsum ne "diff" ) {
  2131. my $hsb = PHTV_hex2hsb($hexsum);
  2132. my $hue = $hsb->{h};
  2133. my $sat = $hsb->{s};
  2134. my $bri = $hsb->{b};
  2135. my $pct = PHTV_bri2pct($bri);
  2136. readingsBulkUpdateIfChanged( $hash, "rgb", $hexsum );
  2137. readingsBulkUpdateIfChanged( $hash, "hue", $hue );
  2138. readingsBulkUpdateIfChanged( $hash, "sat", $sat );
  2139. readingsBulkUpdateIfChanged( $hash, "bri", $bri );
  2140. readingsBulkUpdateIfChanged( $hash, "pct", $pct );
  2141. readingsBulkUpdateIfChanged( $hash, "level", $pct . " %" );
  2142. }
  2143. }
  2144. elsif ( $return eq "ok" ) {
  2145. if ( $type =~ /^(..)(..)(..)$/
  2146. && ReadingsVal( $name, "ambiLEDLayers", undef ) )
  2147. {
  2148. my $hsb = PHTV_hex2hsb($type);
  2149. my $hue = $hsb->{h};
  2150. my $sat = $hsb->{s};
  2151. my $bri = $hsb->{b};
  2152. my $pct = PHTV_bri2pct($bri);
  2153. readingsBulkUpdateIfChanged( $hash, "rgb", $type );
  2154. readingsBulkUpdateIfChanged( $hash, "hue", $hue );
  2155. readingsBulkUpdateIfChanged( $hash, "sat", $sat );
  2156. readingsBulkUpdateIfChanged( $hash, "bri", $bri );
  2157. readingsBulkUpdateIfChanged( $hash, "pct", $pct );
  2158. readingsBulkUpdateIfChanged( $hash, "level", $pct . " %" );
  2159. if ( ReadingsVal( $name, "ambiLEDLayers", undef ) ) {
  2160. my $layer = 1;
  2161. while ( $layer <=
  2162. ReadingsVal( $name, "ambiLEDLayers", undef ) )
  2163. {
  2164. foreach
  2165. my $side ( 'Left', 'Top', 'Right', 'Bottom' )
  2166. {
  2167. my $ambiLED = "ambiLED$side";
  2168. my $side = lc($side);
  2169. my $l = "L" . $layer;
  2170. my $s = $side;
  2171. $s =~ s/left/L/ if ( $side eq "left" );
  2172. $s =~ s/top/T/ if ( $side eq "top" );
  2173. $s =~ s/right/R/ if ( $side eq "right" );
  2174. $s =~ s/bottom/B/ if ( $side eq "bottom" );
  2175. if ( ReadingsVal( $name, $ambiLED, 0 ) > 0 ) {
  2176. my $led = 0;
  2177. while ( $led <=
  2178. ReadingsVal( $name, $ambiLED, 0 ) - 1 )
  2179. {
  2180. my $readingname =
  2181. "rgb_" . $l . $s . $led;
  2182. readingsBulkUpdateIfChanged( $hash,
  2183. $readingname, $type );
  2184. $led++;
  2185. }
  2186. }
  2187. }
  2188. $layer++;
  2189. }
  2190. }
  2191. }
  2192. }
  2193. }
  2194. # ambilight/processed (ambiHue)
  2195. elsif ( $service eq "ambilight/processed" ) {
  2196. if ( ref($return) eq "HASH" ) {
  2197. readingsBulkUpdate( $hash, "ambiHue", "on" )
  2198. if ( $type eq "init" );
  2199. # run ambiHue
  2200. if (
  2201. (
  2202. ReadingsVal( $name, "ambiHue", "off" ) eq "on"
  2203. || $type eq "init"
  2204. )
  2205. && ( defined( AttrVal( $name, "ambiHueLeft", undef ) )
  2206. || defined( AttrVal( $name, "ambiHueRight", undef ) )
  2207. || defined( AttrVal( $name, "ambiHueTop", undef ) )
  2208. || defined( AttrVal( $name, "ambiHueBottom", undef ) ) )
  2209. )
  2210. {
  2211. my $transitiontime =
  2212. int(
  2213. AttrVal( $name, "ambiHueLatency", 300 ) / 100 + 0.5 );
  2214. $transitiontime = 3 if ( $transitiontime < 3 );
  2215. foreach my $side ( 'Left', 'Top', 'Right', 'Bottom' ) {
  2216. my $ambiHue = "ambiHue$side";
  2217. my $ambiLED = "ambiLED$side";
  2218. my $s = lc($side);
  2219. # $ambiHue
  2220. if ( AttrVal( $name, $ambiHue, "" ) ne ""
  2221. && defined( $return->{layer1}->{$s} )
  2222. && ref( $return->{layer1}->{$s} ) eq "HASH"
  2223. && ReadingsVal( $name, $ambiLED, 0 ) > 0 )
  2224. {
  2225. my @devices =
  2226. split( " ", AttrVal( $name, $ambiHue, "" ) );
  2227. Log3 $name, 5,
  2228. "PHTV $name: processing devices from attribute $ambiHue";
  2229. foreach my $devled (@devices) {
  2230. my ( $dev, $led, $sat, $bri ) =
  2231. split( /:/, $devled );
  2232. my @leds;
  2233. my $logtext =
  2234. "PHTV $name: processing $ambiHue -> $devled -> dev=$dev";
  2235. $logtext .= " led=$led"
  2236. if ( defined($led) );
  2237. $logtext .= " sat=$sat"
  2238. if ( defined($sat) );
  2239. $logtext .= " bri=$bri"
  2240. if ( defined($bri) );
  2241. Log3 $name, 5, $logtext;
  2242. # next for if HUE device is not ready
  2243. if ( !IsDevice( $dev, "HUEDevice" )
  2244. || ReadingsVal( $dev, "reachable", 0 ) ne
  2245. "1" )
  2246. {
  2247. Log3 $name, 5,
  2248. "PHTV $name: $devled seems to be unreachable, skipping it";
  2249. next;
  2250. }
  2251. # determine reference LEDs
  2252. if ( !defined($led) || $led eq "" ) {
  2253. my $led_middle = int(
  2254. ReadingsVal( $name, $ambiLED, 0 ) / 2 +
  2255. 0.5 ) - 1;
  2256. # take the middle LED and
  2257. # one left and right each
  2258. push(
  2259. @leds,
  2260. (
  2261. $led_middle,
  2262. $led_middle - 1,
  2263. $led_middle + 1
  2264. )
  2265. );
  2266. }
  2267. # user named reference LED(s)
  2268. else {
  2269. my ( $ledB, $ledE ) =
  2270. split( /-/, $led );
  2271. $ledB -= 1;
  2272. $ledE -= 1
  2273. if ( defined($ledE) && $ledE ne "" );
  2274. if ( !defined($ledE) || $ledE eq "" ) {
  2275. push( @leds, ($ledB) );
  2276. }
  2277. else {
  2278. my $i = $ledB;
  2279. while ( $i <= $ledE ) {
  2280. push( @leds, ($i) );
  2281. $i++;
  2282. }
  2283. }
  2284. }
  2285. # get current RGB values
  2286. my $Hsum = 0;
  2287. my $Ssum = 0;
  2288. my $Bsum = 0;
  2289. my $countLEDs = 0;
  2290. foreach my $l (@leds) {
  2291. if (
  2292. defined(
  2293. $return->{layer1}->{$s}->{$l}
  2294. )
  2295. )
  2296. {
  2297. Log3 $name, 5,
  2298. "PHTV $name: $devled - getting color from LED $l";
  2299. my $hsb = PHTV_rgb2hsb(
  2300. $return->{layer1}->{$s}->{$l}->{r},
  2301. $return->{layer1}->{$s}->{$l}->{g},
  2302. $return->{layer1}->{$s}->{$l}->{b}
  2303. );
  2304. # only consider color if:
  2305. # - hue color delta <4000
  2306. # - sat&bri >5
  2307. # to avoid huge color skips between LEDs
  2308. if (
  2309. (
  2310. $countLEDs > 0 && abs(
  2311. $Hsum / $countLEDs -
  2312. $hsb->{h}
  2313. ) < 4000
  2314. )
  2315. || $countLEDs == 0
  2316. )
  2317. {
  2318. if (
  2319. (
  2320. $hsb->{s} > 5
  2321. && $hsb->{b} > 5
  2322. )
  2323. || $countLEDs == 0
  2324. )
  2325. {
  2326. Log3 $name, 5,
  2327. "PHTV $name: $devled - LED $l added to sum of $countLEDs";
  2328. $Hsum += $hsb->{h};
  2329. $Ssum += $hsb->{s};
  2330. $Bsum += $hsb->{b};
  2331. $countLEDs++;
  2332. }
  2333. }
  2334. }
  2335. }
  2336. # consider user defined values
  2337. my $satF =
  2338. ( $sat && $sat > 0 && $sat < 100 )
  2339. ? $sat / 100
  2340. : 1;
  2341. my $briF =
  2342. ( $bri && $bri > 0 && $bri < 100 )
  2343. ? $bri / 100
  2344. : 1;
  2345. my ( $hDec, $sDec, $bDec, $h, $s, $b );
  2346. if ( $countLEDs > 0 ) {
  2347. $hDec =
  2348. int( $Hsum / $countLEDs / 256 + 0.5 );
  2349. $sDec =
  2350. int( $Ssum / $countLEDs * $satF + 0.5 );
  2351. $bDec =
  2352. int( $Bsum / $countLEDs * $briF + 0.5 );
  2353. # keep bri=1 if user calc value
  2354. # would be below
  2355. $bDec = 1 if ( $briF < 1 && $bDec < 1 );
  2356. $h = sprintf( "%02x", $hDec );
  2357. $s = sprintf( "%02x", $sDec );
  2358. $b = sprintf( "%02x", $bDec );
  2359. }
  2360. else {
  2361. $hDec = 0;
  2362. $sDec = 0;
  2363. $bDec = 0;
  2364. $h = "00";
  2365. $s = "00";
  2366. $b = "00";
  2367. }
  2368. # temp. disable event triggers for HUEDevice
  2369. unless (
  2370. AttrVal( $dev, "event-on-change-reading",
  2371. "" ) eq "none"
  2372. )
  2373. {
  2374. $attr{$dev}{"event-on-change-reading"} =
  2375. "none";
  2376. }
  2377. # Update color only if there is a
  2378. #significant difference
  2379. my (
  2380. $hMin, $hMax, $hDiff, $sMin, $sMax,
  2381. $sDiff, $bMin, $bMax, $bDiff
  2382. );
  2383. if (
  2384. defined(
  2385. $hash->{helper}{ambiHueColor}{$side}
  2386. )
  2387. )
  2388. {
  2389. $hMin = PHTV_min(
  2390. $hash->{helper}{ambiHueColor}{$side}{h},
  2391. $hDec
  2392. );
  2393. $hMax = PHTV_max(
  2394. $hash->{helper}{ambiHueColor}{$side}{h},
  2395. $hDec
  2396. );
  2397. $hDiff = $hMax - $hMin;
  2398. $sMin = PHTV_min(
  2399. $hash->{helper}{ambiHueColor}{$side}{s},
  2400. $sDec
  2401. );
  2402. $sMax = PHTV_max(
  2403. $hash->{helper}{ambiHueColor}{$side}{s},
  2404. $sDec
  2405. );
  2406. $sDiff = $sMax - $sMin;
  2407. $bMin = PHTV_min(
  2408. $hash->{helper}{ambiHueColor}{$side}{b},
  2409. $bDec
  2410. );
  2411. $bMax = PHTV_max(
  2412. $hash->{helper}{ambiHueColor}{$side}{b},
  2413. $bDec
  2414. );
  2415. $bDiff = $bMax - $bMin;
  2416. }
  2417. if (
  2418. ( $hDec == 0 && $sDec == 0 && $bDec == 0 )
  2419. || ( !defined($hDiff)
  2420. && !defined($sDiff)
  2421. && !defined($bDiff) )
  2422. || $hDiff >= 200
  2423. || $sDiff > 3
  2424. || $bDiff > 2
  2425. )
  2426. {
  2427. Log3 $name, 4,
  2428. "PHTV $name: color changed hDiff=$hDiff sDiff=$sDiff bDiff=$bDiff"
  2429. if ( $hDiff && $sDiff && $bDiff );
  2430. $hash->{helper}{ambiHueColor}{$side}{h} =
  2431. $hDec;
  2432. $hash->{helper}{ambiHueColor}{$side}{s} =
  2433. $sDec;
  2434. $hash->{helper}{ambiHueColor}{$side}{b} =
  2435. $bDec;
  2436. # switch HUE bulb to color
  2437. if ( $b ne "00" ) {
  2438. Log3 $name, 4,
  2439. "PHTV $name: set $dev transitiontime $transitiontime : noUpdate : hsv $h$s$b";
  2440. fhem(
  2441. "set $dev transitiontime $transitiontime : noUpdate : hsv $h$s$b"
  2442. );
  2443. }
  2444. # switch HUE bulb off if brightness is 0
  2445. else {
  2446. Log3 $name, 4,
  2447. "PHTV $name: set $dev transitiontime 5 : noUpdate : off (reason: brightness=$b)";
  2448. fhem(
  2449. "set $dev transitiontime 5 : noUpdate : off"
  2450. );
  2451. }
  2452. }
  2453. }
  2454. }
  2455. }
  2456. my $duration = gettimeofday() - $param->{timestamp};
  2457. my $minLatency =
  2458. AttrVal( $name, "ambiHueLatency", 200 ) / 1000;
  2459. my $waittime = $minLatency - $duration;
  2460. # latency compensation
  2461. if ( $waittime > 0 ) {
  2462. $hash->{helper}{ambiHueDelay} =
  2463. int( ( $duration + $waittime ) * 1000 + 0.5 );
  2464. }
  2465. else {
  2466. $hash->{helper}{ambiHueDelay} =
  2467. int( $duration * 1000 + 0.5 );
  2468. }
  2469. PHTV_SendCommand( $hash, "ambilight/processed", undef,
  2470. undef, $waittime );
  2471. }
  2472. # cleanup after stopping ambiHue
  2473. elsif (
  2474. ReadingsVal( $name, "ambiHue", "off" ) eq "off"
  2475. || ( !defined( AttrVal( $name, "ambiHueLeft", undef ) )
  2476. && !defined( AttrVal( $name, "ambiHueRight", undef ) )
  2477. && !defined( AttrVal( $name, "ambiHueTop", undef ) )
  2478. && !defined( AttrVal( $name, "ambiHueBottom", undef ) )
  2479. )
  2480. )
  2481. {
  2482. delete $hash->{helper}{ambiHueDelay};
  2483. delete $hash->{helper}{ambiHueColor};
  2484. readingsBulkUpdateIfChanged( $hash, "ambiHue", "off" );
  2485. # ambiHueLeft
  2486. if ( AttrVal( $name, "ambiHueLeft", "" ) ne "" ) {
  2487. my @devices =
  2488. split( " ", AttrVal( $name, "ambiHueLeft", "" ) );
  2489. foreach (@devices) {
  2490. my ( $dev, $led ) = split( /:/, $_ );
  2491. $attr{$dev}{"event-on-change-reading"} = ".*";
  2492. fhem(
  2493. "set $dev transitiontime 10 : noUpdate : hsv 000020"
  2494. );
  2495. }
  2496. }
  2497. # ambiHueTop
  2498. if ( AttrVal( $name, "ambiHueTop", "" ) ne "" ) {
  2499. my @devices =
  2500. split( " ", AttrVal( $name, "ambiHueTop", "" ) );
  2501. foreach (@devices) {
  2502. my ( $dev, $led ) = split( /:/, $_ );
  2503. $attr{$dev}{"event-on-change-reading"} = ".*";
  2504. fhem(
  2505. "set $dev transitiontime 10 : noUpdate : hsv 000020"
  2506. );
  2507. }
  2508. }
  2509. # ambiHueRight
  2510. if ( AttrVal( $name, "ambiHueRight", "" ) ne "" ) {
  2511. my @devices =
  2512. split( " ", AttrVal( $name, "ambiHueRight", "" ) );
  2513. foreach (@devices) {
  2514. my ( $dev, $led ) = split( /:/, $_ );
  2515. $attr{$dev}{"event-on-change-reading"} = ".*";
  2516. fhem(
  2517. "set $dev transitiontime 10 : noUpdate : hsv 000020"
  2518. );
  2519. }
  2520. }
  2521. # ambiHueBottom
  2522. if ( AttrVal( $name, "ambiHueBottom", "" ) ne "" ) {
  2523. my @devices =
  2524. split( " ", $attr{$name}{ambiHueBottom} );
  2525. foreach (@devices) {
  2526. my ( $dev, $led ) = split( /:/, $_ );
  2527. $attr{$dev}{"event-on-change-reading"} = ".*";
  2528. fhem(
  2529. "set $dev transitiontime 10 : noUpdate : hsv 000020"
  2530. );
  2531. }
  2532. }
  2533. }
  2534. }
  2535. }
  2536. # all other command results
  2537. else {
  2538. Log3 $name, 2,
  2539. "PHTV $name: ERROR: method to handle response of $service not implemented";
  2540. }
  2541. }
  2542. # Set reading for power
  2543. #
  2544. my $readingPower = "off";
  2545. $readingPower = "on"
  2546. if ( $newstate eq "on" );
  2547. readingsBulkUpdateIfChanged( $hash, "power", $readingPower );
  2548. # Set reading for state
  2549. #
  2550. readingsBulkUpdateIfChanged( $hash, "state", $newstate );
  2551. # Set reading for stateAV
  2552. my $stateAV = PHTV_GetStateAV($hash);
  2553. readingsBulkUpdateIfChanged( $hash, "stateAV", $stateAV );
  2554. # Set PHTV online-only readings to "-" in case box is in
  2555. # offline or in standby mode
  2556. if ( $newstate eq "off"
  2557. || $newstate eq "absent"
  2558. || $newstate eq "undefined" )
  2559. {
  2560. foreach (
  2561. 'mute', 'volume', 'volumeStraight',
  2562. 'input', 'channel', 'currentMedia',
  2563. 'servicename', 'frequency', 'onid',
  2564. 'tsid', 'sid', 'receiveMode',
  2565. )
  2566. {
  2567. readingsBulkUpdateIfChanged( $hash, $_, "-" );
  2568. }
  2569. readingsBulkUpdateIfChanged( $hash, "ambiHue", "off" );
  2570. readingsBulkUpdateIfChanged( $hash, "ambiMode", "internal" );
  2571. readingsBulkUpdateIfChanged( $hash, "rgb", "000000" );
  2572. readingsBulkUpdateIfChanged( $hash, "hue", "0" );
  2573. readingsBulkUpdateIfChanged( $hash, "sat", "0" );
  2574. readingsBulkUpdateIfChanged( $hash, "bri", "0" );
  2575. readingsBulkUpdateIfChanged( $hash, "pct", "0" );
  2576. readingsBulkUpdateIfChanged( $hash, "level", "0 %" );
  2577. if ( ReadingsVal( $name, "ambiLEDLayers", undef ) ) {
  2578. my $layer = 1;
  2579. while ( $layer <= ReadingsVal( $name, "ambiLEDLayers", undef ) ) {
  2580. foreach my $side ( 'Left', 'Top', 'Right', 'Bottom' ) {
  2581. my $ambiLED = "ambiLED$side";
  2582. my $side = lc($side);
  2583. my $l = "L" . $layer;
  2584. my $s = $side;
  2585. $s =~ s/left/L/ if ( $side eq "left" );
  2586. $s =~ s/top/T/ if ( $side eq "top" );
  2587. $s =~ s/right/R/ if ( $side eq "right" );
  2588. $s =~ s/bottom/B/ if ( $side eq "bottom" );
  2589. if ( ReadingsVal( $name, $ambiLED, 0 ) > 0 ) {
  2590. my $led = 0;
  2591. while ( $led <= ReadingsVal( $name, $ambiLED, 0 ) - 1 )
  2592. {
  2593. my $readingname = "rgb_" . $l . $s . $led;
  2594. readingsBulkUpdateIfChanged( $hash,
  2595. $readingname, "000000" );
  2596. $led++;
  2597. }
  2598. }
  2599. }
  2600. $layer++;
  2601. }
  2602. }
  2603. }
  2604. readingsEndUpdate( $hash, 1 );
  2605. Log3 $name, 4,
  2606. "PHTV $name: sequentialQuery - finished round "
  2607. . $hash->{helper}{sequentialQueryCounter}
  2608. if $sequential;
  2609. return;
  2610. }
  2611. sub PHTV_GetStateAV($) {
  2612. my ($hash) = @_;
  2613. my $name = $hash->{NAME};
  2614. if ( ReadingsVal( $name, "presence", "absent" ) eq "absent" ) {
  2615. return "absent";
  2616. }
  2617. elsif ( ReadingsVal( $name, "power", "off" ) eq "off" ) {
  2618. return "off";
  2619. }
  2620. elsif ( ReadingsVal( $name, "mute", "off" ) eq "on" ) {
  2621. return "muted";
  2622. }
  2623. elsif ( ReadingsVal( $name, "playStatus", "stopped" ) ne "stopped" ) {
  2624. return ReadingsVal( $name, "playStatus", "stopped" );
  2625. }
  2626. else {
  2627. return ReadingsVal( $name, "power", "off" );
  2628. }
  2629. }
  2630. sub PHTV_wake ($) {
  2631. my ($hash) = @_;
  2632. my $name = $hash->{NAME};
  2633. my $mac_addr = AttrVal( $name, "macaddr", "-" );
  2634. my $address = '255.255.255.255';
  2635. my $port = 9;
  2636. if ( $mac_addr ne "-" ) {
  2637. my $sock = new IO::Socket::INET( Proto => 'udp' )
  2638. or die "socket : $!";
  2639. die "Can't create WOL socket" if ( !$sock );
  2640. my $ip_addr = inet_aton($address);
  2641. my $sock_addr = sockaddr_in( $port, $ip_addr );
  2642. $mac_addr =~ s/://g;
  2643. my $packet =
  2644. pack( 'C6H*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, $mac_addr x 16 );
  2645. setsockopt( $sock, SOL_SOCKET, SO_BROADCAST, 1 )
  2646. or die "setsockopt : $!";
  2647. Log3 $name, 4,
  2648. "PHTV $name: Waking up by sending Wake-On-Lan magic package to "
  2649. . $mac_addr
  2650. . " at IP "
  2651. . $address
  2652. . " port "
  2653. . $port;
  2654. send( $sock, $packet, 0, $sock_addr ) or die "send : $!";
  2655. close($sock);
  2656. }
  2657. else {
  2658. Log3 $name, 3, "PHTV $name: Attribute macaddr not set.";
  2659. }
  2660. return 1;
  2661. }
  2662. sub PHTV_RCmakenotify($$) {
  2663. my ( $nam, $ndev ) = @_;
  2664. my $nname = "notify_$nam";
  2665. fhem( "define $nname notify $nam set $ndev remoteControl " . '$EVENT', 1 );
  2666. Log3 undef, 2, "[remotecontrol:PHTV] Notify created: $nname";
  2667. return "Notify created by PHTV: $nname";
  2668. }
  2669. sub PHTV_RClayout_SVG() {
  2670. my @row;
  2671. $row[0] = ":rc_BLANK.svg,:rc_BLANK.svg,POWER:rc_POWER.svg";
  2672. $row[1] = ":rc_BLANK.svg,:rc_BLANK.svg,:rc_BLANK.svg";
  2673. $row[2] = "1:rc_1.svg,2:rc_2.svg,3:rc_3.svg";
  2674. $row[3] = "4:rc_4.svg,5:rc_5.svg,6:rc_6.svg";
  2675. $row[4] = "7:rc_7.svg,8:rc_8.svg,9:rc_9.svg";
  2676. $row[5] = "LEFTBRACE:rc_PREVIOUS.svg,0:rc_0.svg,RIGHTBRACE:rc_NEXT.svg";
  2677. $row[6] =
  2678. "RED:rc_RED.svg,GREEN:rc_GREEN.svg,YELLOW:rc_YELLOW.svg,BLUE:rc_BLUE.svg";
  2679. $row[7] = ":rc_BLANK.svg,:rc_BLANK.svg,:rc_BLANK.svg";
  2680. $row[8] = "INFO:rc_INFO.svg,UP:rc_UP.svg,MENU:rc_MENU.svg";
  2681. $row[9] = "LEFT:rc_LEFT.svg,OK:rc_OK.svg,RIGHT:rc_RIGHT.svg";
  2682. $row[10] = "AUDIO:rc_AUDIO.svg,DOWN:rc_DOWN.svg,VIDEO:rc_VIDEO.svg";
  2683. $row[11] = ":rc_BLANK.svg,EXIT:rc_EXIT.svg,:rc_BLANK.svg";
  2684. $row[12] = "VOLUP:rc_VOLPLUS.svg,:rc_BLANK.svg,CHANNELUP:rc_UP.svg";
  2685. $row[13] =
  2686. "VOLDOWN:rc_VOLMINUS.svg,MUTE:rc_MUTE.svg,CHANNELDOWN:rc_DOWN.svg";
  2687. $row[14] = ":rc_BLANK.svg,:rc_BLANK.svg,:rc_BLANK.svg";
  2688. $row[15] =
  2689. "REWIND:rc_REW.svg,PLAY:rc_PLAY.svg,STOP:rc_STOP.svg,FASTFORWARD:rc_FF.svg";
  2690. $row[16] =
  2691. "TV:rc_TV.svg,RADIO:rc_RADIO.svg,TEXT:rc_TEXT.svg,RECORD:rc_REC.svg";
  2692. $row[17] = "attr rc_iconpath icons";
  2693. $row[18] = "attr rc_iconprefix rc_";
  2694. return @row;
  2695. }
  2696. sub PHTV_RClayout() {
  2697. my @row;
  2698. $row[0] = ":blank,:blank,POWER:POWEROFF";
  2699. $row[1] = ":blank,:blank,:blank";
  2700. $row[2] = "1,2,3";
  2701. $row[3] = "4,5,6";
  2702. $row[4] = "7,8,9";
  2703. $row[5] = "LEFTBRACE:LEFT2,0:0,RIGHTBRACE:RIGHT2";
  2704. $row[6] = "RED,GREEN,YELLOW,BLUE";
  2705. $row[7] = ":blank,:blank,:blank";
  2706. $row[8] = "INFO,UP,MENU";
  2707. $row[9] = "LEFT,OK,RIGHT";
  2708. $row[10] = "AUDIO,DOWN,VIDEO";
  2709. $row[11] = ":blank,EXIT,:blank";
  2710. $row[12] = "VOLUP:VOLUP,:blank,CHANNELUP:CHUP2";
  2711. $row[13] = "VOLDOWN:VOLDOWN,MUTE,CHANNELDOWN:CHDOWN2";
  2712. $row[14] = ":blank,:blank,:blank";
  2713. $row[15] = "REWIND,PLAY,STOP,FASTFORWARD:FF";
  2714. $row[16] = "TV,RADIO,TEXT,RECORD:REC";
  2715. $row[17] = "attr rc_iconpath icons/remotecontrol";
  2716. $row[18] = "attr rc_iconprefix black_btn_";
  2717. return @row;
  2718. }
  2719. sub PHTV_GetRemotecontrolCommand($) {
  2720. my ($command) = @_;
  2721. my $commands = {
  2722. 'POWER' => "Standby",
  2723. 'STANDBY' => "Standby",
  2724. 'BACK' => "Back",
  2725. 'EXIT' => "Back",
  2726. 'ESC' => "Back",
  2727. 'FIND' => "Find",
  2728. 'RED' => "RedColour",
  2729. 'GREEN' => "GreenColour",
  2730. 'YELLOW' => "YellowColour",
  2731. 'BLUE' => "BlueColour",
  2732. 'HOME' => "Home",
  2733. 'MENU' => "Home",
  2734. 'VOLUP' => "VolumeUp",
  2735. 'VOLUMEUP' => "VolumeUp",
  2736. 'VOLDOWN' => "VolumeDown",
  2737. 'VOLUMEDOWN' => "VolumeDown",
  2738. 'MUTE' => "Mute",
  2739. 'OPTIONS' => "Options",
  2740. 'DOT' => "Dot",
  2741. '0' => "Digit0",
  2742. '1' => "Digit1",
  2743. '2' => "Digit2",
  2744. '3' => "Digit3",
  2745. '4' => "Digit4",
  2746. '5' => "Digit5",
  2747. '6' => "Digit6",
  2748. '7' => "Digit7",
  2749. '8' => "Digit8",
  2750. '9' => "Digit9",
  2751. 'INFO' => "Info",
  2752. 'UP' => "CursorUp",
  2753. 'DOWN' => "CursorDown",
  2754. 'LEFT' => "CursorLeft",
  2755. 'RIGHT' => "CursorRight",
  2756. 'OK' => "Confirm",
  2757. 'ENTER' => "Confirm",
  2758. 'NEXT' => "Next",
  2759. 'PREVIOUS' => "Previous",
  2760. 'ADJUST' => "Adjust",
  2761. 'TV' => "WatchTV",
  2762. 'MODE' => "Viewmode",
  2763. 'TEXT' => "Teletext",
  2764. 'SUBTITLE' => "Subtitle",
  2765. 'CHANUP' => "ChannelStepUp",
  2766. 'CHANNELUP' => "ChannelStepUp",
  2767. 'CHANDOWN' => "ChannelStepDown",
  2768. 'CHANNELDOWN' => "ChannelStepDown",
  2769. 'SOURCE' => "Source",
  2770. 'AMBI' => "AmbilightOnOff",
  2771. 'PLAYPAUSE' => "PlayPause",
  2772. 'FORWARD' => "FastForward",
  2773. 'STOP' => "Stop",
  2774. 'REWIND' => "Rewind",
  2775. 'RECORD' => "Record",
  2776. 'ONLINE' => "Online",
  2777. };
  2778. if ( defined( $commands->{$command} ) ) {
  2779. return $commands->{$command};
  2780. }
  2781. elsif ( $command eq "GetRemotecontrolCommands" ) {
  2782. return $commands;
  2783. }
  2784. else {
  2785. return "";
  2786. }
  2787. }
  2788. sub PHTV_isinteger {
  2789. defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
  2790. }
  2791. sub PHTV_bri2pct($) {
  2792. my ($bri) = @_;
  2793. return 0 if ( $bri <= 0 );
  2794. return int( $bri / 255 * 100 + 0.5 );
  2795. }
  2796. sub PHTV_pct2bri($) {
  2797. my ($pct) = @_;
  2798. return 0 if ( $pct <= 0 );
  2799. return int( $pct / 100 * 255 + 0.5 );
  2800. }
  2801. sub PHTV_hex2rgb($) {
  2802. my ($hex) = @_;
  2803. if ( uc($hex) =~ /^(..)(..)(..)$/ ) {
  2804. my ( $r, $g, $b ) = ( hex($1), hex($2), hex($3) );
  2805. my $return = { "r" => $r, "g" => $g, "b" => $b };
  2806. Log3 undef, 5,
  2807. "PHTV hex2rgb: $hex > "
  2808. . $return->{r} . " "
  2809. . $return->{g} . " "
  2810. . $return->{b};
  2811. return $return;
  2812. }
  2813. }
  2814. sub PHTV_rgb2hex($$$) {
  2815. my ( $r, $g, $b ) = @_;
  2816. my $return = sprintf( "%2.2X%2.2X%2.2X", $r, $g, $b );
  2817. Log3 undef, 5, "PHTV rgb2hex: $r $g $b > $return";
  2818. return uc($return);
  2819. }
  2820. sub PHTV_hex2hsb($;$) {
  2821. my ( $hex, $type ) = @_;
  2822. $type = lc($type) if ( defined( ($type) && $type ne "" ) );
  2823. my $rgb = PHTV_hex2rgb($hex);
  2824. my $return = PHTV_rgb2hsb( $rgb->{r}, $rgb->{g}, $rgb->{b} );
  2825. if ( defined($type) ) {
  2826. return $return->{h} if ( $type eq "h" );
  2827. return $return->{s} if ( $type eq "s" );
  2828. return $return->{b} if ( $type eq "b" );
  2829. }
  2830. else {
  2831. return $return;
  2832. }
  2833. }
  2834. sub PHTV_hsb2hex($$$) {
  2835. my ( $h, $s, $b ) = @_;
  2836. my $rgb = PHTV_hsb2rgb( $h, $s, $b );
  2837. return PHTV_rgb2hex( $rgb->{r}, $rgb->{g}, $rgb->{b} );
  2838. }
  2839. sub PHTV_rgb2hsb ($$$) {
  2840. my ( $r, $g, $b ) = @_;
  2841. my $r2 = $r / 255.0;
  2842. my $g2 = $g / 255.0;
  2843. my $b2 = $b / 255.0;
  2844. my $hsv = PHTV_rgb2hsv( $r2, $g2, $b2 );
  2845. my $h = int( $hsv->{h} * 65535 );
  2846. my $s = int( $hsv->{s} * 255 );
  2847. my $bri = int( $hsv->{v} * 255 );
  2848. Log3 undef, 5, "PHTV rgb2hsb: $r $g $b > $h $s $bri";
  2849. return {
  2850. "h" => $h,
  2851. "s" => $s,
  2852. "b" => $bri
  2853. };
  2854. }
  2855. sub PHTV_hsb2rgb ($$$) {
  2856. my ( $h, $s, $bri ) = @_;
  2857. my $h2 = $h / 65535.0;
  2858. my $s2 = $s / 255.0;
  2859. my $bri2 = $bri / 255.0;
  2860. my $rgb = PHTV_hsv2rgb( $h2, $s2, $bri2 );
  2861. my $r = int( $rgb->{r} * 255 );
  2862. my $g = int( $rgb->{g} * 255 );
  2863. my $b = int( $rgb->{b} * 255 );
  2864. Log3 undef, 5, "PHTV hsb2rgb: $h $s $bri > $r $g $b";
  2865. return {
  2866. "r" => $r,
  2867. "g" => $g,
  2868. "b" => $b
  2869. };
  2870. }
  2871. sub PHTV_rgb2hsv($$$) {
  2872. my ( $r, $g, $b ) = @_;
  2873. my ( $M, $m, $c, $h, $s, $v );
  2874. $M = PHTV_max( $r, $g, $b );
  2875. $m = PHTV_min( $r, $g, $b );
  2876. $c = $M - $m;
  2877. if ( $c == 0 ) {
  2878. $h = 0;
  2879. }
  2880. elsif ( $M == $r ) {
  2881. $h = ( 60 * ( ( $g - $b ) / $c ) % 360 ) / 360;
  2882. }
  2883. elsif ( $M == $g ) {
  2884. $h = ( 60 * ( ( $b - $r ) / $c ) + 120 ) / 360;
  2885. }
  2886. elsif ( $M == $b ) {
  2887. $h = ( 60 * ( ( $r - $g ) / $c ) + 240 ) / 360;
  2888. }
  2889. if ( $h < 0 ) {
  2890. $h = $h + 1;
  2891. }
  2892. if ( $M == 0 ) {
  2893. $s = 0;
  2894. }
  2895. else {
  2896. $s = $c / $M;
  2897. }
  2898. $v = $M;
  2899. Log3 undef, 5, "PHTV rgb2hsv: $r $g $b > $h $s $v";
  2900. return {
  2901. "h" => $h,
  2902. "s" => $s,
  2903. "v" => $v
  2904. };
  2905. }
  2906. sub PHTV_hsv2rgb($$$) {
  2907. my ( $h, $s, $v ) = @_;
  2908. my $r = 0.0;
  2909. my $g = 0.0;
  2910. my $b = 0.0;
  2911. if ( $s == 0 ) {
  2912. $r = $v;
  2913. $g = $v;
  2914. $b = $v;
  2915. }
  2916. else {
  2917. my $i = int( $h * 6.0 );
  2918. my $f = ( $h * 6.0 ) - $i;
  2919. my $p = $v * ( 1.0 - $s );
  2920. my $q = $v * ( 1.0 - $s * $f );
  2921. my $t = $v * ( 1.0 - $s * ( 1.0 - $f ) );
  2922. $i = $i % 6;
  2923. if ( $i == 0 ) {
  2924. $r = $v;
  2925. $g = $t;
  2926. $b = $p;
  2927. }
  2928. elsif ( $i == 1 ) {
  2929. $r = $q;
  2930. $g = $v;
  2931. $b = $p;
  2932. }
  2933. elsif ( $i == 2 ) {
  2934. $r = $p;
  2935. $g = $v;
  2936. $b = $t;
  2937. }
  2938. elsif ( $i == 3 ) {
  2939. $r = $p;
  2940. $g = $q;
  2941. $b = $v;
  2942. }
  2943. elsif ( $i == 4 ) {
  2944. $r = $t;
  2945. $g = $p;
  2946. $b = $v;
  2947. }
  2948. elsif ( $i == 5 ) {
  2949. $r = $v;
  2950. $g = $p;
  2951. $b = $q;
  2952. }
  2953. }
  2954. Log3 undef, 5, "PHTV hsv2rgb: $h $s $v > $r $g $b";
  2955. return {
  2956. "r" => $r,
  2957. "g" => $g,
  2958. "b" => $b
  2959. };
  2960. }
  2961. sub PHTV_max {
  2962. my ( $max, @vars ) = @_;
  2963. for (@vars) {
  2964. $max = $_
  2965. if $_ > $max;
  2966. }
  2967. return $max;
  2968. }
  2969. sub PHTV_min {
  2970. my ( $min, @vars ) = @_;
  2971. for (@vars) {
  2972. $min = $_ if $_ < $min;
  2973. }
  2974. return $min;
  2975. }
  2976. sub PHTV_createDeviceId() {
  2977. my $deviceid;
  2978. my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 );
  2979. $deviceid .= $chars[ rand @chars ] for 1 .. 16;
  2980. return $deviceid;
  2981. }
  2982. sub PHTV_createAuthSignature($$$) {
  2983. my ( $timestamp, $pin, $secretkey ) = @_;
  2984. my $base64 = 0;
  2985. my $authsignature;
  2986. if ( $secretkey =~ m/.*=+$/ ) {
  2987. $secretkey = decode_base64($secretkey);
  2988. $base64 = 1;
  2989. }
  2990. eval {
  2991. require Digest::SHA;
  2992. import Digest::SHA qw( hmac_sha1_hex );
  2993. $authsignature = hmac_sha1_hex( $timestamp . trim($pin), $secretkey );
  2994. };
  2995. return
  2996. if ($@);
  2997. while ( length($authsignature) % 4 ) {
  2998. $authsignature .= '=';
  2999. }
  3000. return trim( encode_base64($authsignature) ) if ($base64);
  3001. return $authsignature;
  3002. }
  3003. 1;
  3004. =pod
  3005. =item device
  3006. =item summary control for Philips TV devices and their Ambilight via network connection
  3007. =item summary_DE Steuerung von Philips TV Ger&auml;ten und Ambilight &uuml;ber das Netzwerk
  3008. =begin html
  3009. <a name="PHTV"></a>
  3010. <h3>PHTV</h3>
  3011. <ul>
  3012. <a name="PHTVdefine"></a>
  3013. <b>Define</b>
  3014. <ul>
  3015. <code>define &lt;name&gt; PHTV &lt;ip-address-or-hostname&gt; [&lt;poll-interval&gt;]</code>
  3016. <br><br>
  3017. This module controls Philips TV devices and their Ambilight via network connection.<br><br>
  3018. Defining a PHTV device will schedule an internal task (interval can be set
  3019. with optional parameter &lt;poll-interval&gt; in seconds, if not set, the value is 45
  3020. seconds), which periodically reads the status of the device and triggers notify/filelog commands.<br><br>
  3021. Example:<br>
  3022. <ul><code>
  3023. define PhilipsTV PHTV 192.168.0.10
  3024. <br><br>
  3025. # With custom interval of 20 seconds<br>
  3026. define PhilipsTV PHTV 192.168.0.10 20
  3027. </code></ul>
  3028. <br>
  3029. <br>
  3030. <i>Note:</i> Some older devices might need to have the API activated first. If you get no response from your
  3031. device, try to input "5646877223" on the remote while watching TV (which spells jointspace on the digits).
  3032. A popup might appear stating the API was successfully enabled.
  3033. </ul>
  3034. <br>
  3035. <br>
  3036. <a name="PHTVset"></a>
  3037. <b>Set </b>
  3038. <ul>
  3039. <code>set &lt;name&gt; &lt;command&gt; [&lt;parameter&gt;]</code>
  3040. <br><br>
  3041. Currently, the following commands are defined.<br>
  3042. <ul>
  3043. <li><b>on</b> &nbsp;&nbsp;-&nbsp;&nbsp; powers on the device and send a WoL magic package if needed</li>
  3044. <li><b>off</b> &nbsp;&nbsp;-&nbsp;&nbsp; turns the device in standby mode</li>
  3045. <li><b>toggle</b> &nbsp;&nbsp;-&nbsp;&nbsp; switch between on and off</li>
  3046. <li><b>channel</b> channel,0...999,sRef &nbsp;&nbsp;-&nbsp;&nbsp; zap to specific channel or service reference</li>
  3047. <li><b>channelUp</b> &nbsp;&nbsp;-&nbsp;&nbsp; zap to next channel</li>
  3048. <li><b>channelDown</b> &nbsp;&nbsp;-&nbsp;&nbsp; zap to previous channel</li>
  3049. <li><b>volume</b> 0...100 &nbsp;&nbsp;-&nbsp;&nbsp; set the volume level in percentage</li>
  3050. <li><b>volumeStraight</b> 1...60 &nbsp;&nbsp;-&nbsp;&nbsp; set the volume level in device specific range</li>
  3051. <li><b>volumeUp</b> &nbsp;&nbsp;-&nbsp;&nbsp; increases the volume level</li>
  3052. <li><b>volumeDown</b> &nbsp;&nbsp;-&nbsp;&nbsp; decreases the volume level</li>
  3053. <li><b>mute</b> on,off,toggle &nbsp;&nbsp;-&nbsp;&nbsp; controls volume mute</li>
  3054. <li><b>input</b> ... &nbsp;&nbsp;-&nbsp;&nbsp; switches between inputs</li>
  3055. <li><b>statusRequest</b> &nbsp;&nbsp;-&nbsp;&nbsp; requests the current status of the device</li>
  3056. <li><b>remoteControl</b> UP,DOWN,... &nbsp;&nbsp;-&nbsp;&nbsp; sends remote control commands; see remoteControl help</li>
  3057. <li><b>ambiHue</b> on,off &nbsp;&nbsp;-&nbsp;&nbsp; activates/disables Ambilight+Hue function</li>
  3058. <li><b>ambiMode</b> internal,manual,expert &nbsp;&nbsp;-&nbsp;&nbsp; set source register for Ambilight</li>
  3059. <li><b>ambiPreset</b> &nbsp;&nbsp;-&nbsp;&nbsp; set Ambilight to predefined state</li>
  3060. <li><b>rgb</b> HEX,LED address &nbsp;&nbsp;-&nbsp;&nbsp; set an RGB value for Ambilight</li>
  3061. <li><b>hue</b> 0-65534 &nbsp;&nbsp;-&nbsp;&nbsp; set the color hue value Ambilight</li>
  3062. <li><b>sat</b> 0-255 &nbsp;&nbsp;-&nbsp;&nbsp; set the saturation value for Ambilight</li>
  3063. <li><b>bri</b> 0-255 &nbsp;&nbsp;-&nbsp;&nbsp; set the brightness value for Ambilight</li>
  3064. <li><b>play</b> &nbsp;&nbsp;-&nbsp;&nbsp; starts/resumes playback</li>
  3065. <li><b>pause</b> &nbsp;&nbsp;-&nbsp;&nbsp; starts/resumes playback</li>
  3066. <li><b>stop</b> &nbsp;&nbsp;-&nbsp;&nbsp; stops current playback</li>
  3067. <li><b>record</b> &nbsp;&nbsp;-&nbsp;&nbsp; starts recording of current channel</li>
  3068. </ul>
  3069. </ul>
  3070. <ul>
  3071. <u>Note:</u> If you would like to restrict access to admin set-commands (-> statusRequest) you may set your FHEMWEB instance's attribute allowedCommands like 'set,set-user'.
  3072. The string 'set-user' will ensure only non-admin set-commands can be executed when accessing FHEM using this FHEMWEB instance.
  3073. </ul>
  3074. <br>
  3075. <br>
  3076. <ul>
  3077. <u>Advanced Ambilight Control</u><br>
  3078. <br>
  3079. <ul>
  3080. If you would like to specificly control color for individual sides or even individual LEDs, you may use special addressing to be used with set command 'rgb':<br>
  3081. <br><br>
  3082. LED addressing format:<br>
  3083. <code>&lt;Layer&gt;&lt;Side&gt;&lt;LED number&gt;</code>
  3084. <br><br>
  3085. <u>Examples:</u><br>
  3086. <ul>
  3087. <code># set LED 0 on left side within layer 1 to color RED<br>
  3088. set PhilipsTV rgb L1L0:FF0000
  3089. <br><br>
  3090. # set LED 0, 2 and 4 on left side within layer 1 to color RED<br>
  3091. set PhilipsTV rgb L1L0:FF0000 L1L2:FF0000 L1L4:FF0000
  3092. <br><br>
  3093. # set complete right side within layer 1 to color GREEN<br>
  3094. set PhilipsTV rgb L1R:00FF00
  3095. <br><br>
  3096. # set complete layer 1 to color BLUE
  3097. set PhilipsTV rgb L1:0000FF</code>
  3098. </ul><br>
  3099. </ul>
  3100. </ul>
  3101. <br>
  3102. <br>
  3103. <br>
  3104. <br>
  3105. <ul>
  3106. <u>Advanced Ambilight+HUE Control</u><br>
  3107. <br>
  3108. <ul>
  3109. Linking to your HUE devices within attributes ambiHueLeft, ambiHueTop, ambiHueRight and ambiHueBottom uses some defaults to calculate the actual color.<br>
  3110. More than one HUE device may be added using blank.<br>
  3111. The following settings can be fine tuned for each HUE device:<br>
  3112. <br>
  3113. <li>LED(s) to be used as color source<br>
  3114. either 1 single LED or a few in a raw like 2-4. Defaults to use the middle LED and it's left and right partners. Counter starts at 1. See readings ambiLED* for how many LED's your TV has.</li>
  3115. <li>saturation in percent of the original value (1-99, default=100)</li>
  3116. <li>brightness in percent of the original value (1-99, default=100)</li>
  3117. <br><br>
  3118. Use the following addressing format for fine tuning:<br>
  3119. <code>devicename:&lt;LEDs$gt;:&lt;saturation$gt;:&lt;brightness$gt;</code>
  3120. <br><br>
  3121. <u>Examples:</u><br>
  3122. <ul>
  3123. <code># to push color from top to 2 HUE devices<br>
  3124. attr PhilipsTV ambiHueTop HUEDevice0 HUEDevice1
  3125. <br><br>
  3126. # to use only LED 4 from the top as source<br>
  3127. attr PhilipsTV ambiHueTop HUEDevice0:4
  3128. <br><br>
  3129. # to use a combination of LED's 1+2 as source<br>
  3130. attr PhilipsTV ambiHueTop HUEDevice0:1-2
  3131. <br><br>
  3132. # to use LED's 1+2 and only 90% of their saturation<br>
  3133. attr PhilipsTV ambiHueTop HUEDevice0:1-2:90
  3134. <br><br>
  3135. # to use LED's 1+2 and only 50% of their brightness<br>
  3136. attr PhilipsTV ambiHueTop HUEDevice0:1-2::50
  3137. <br><br>
  3138. # to use LED's 1+2, 90% saturation and 50% brightness<br>
  3139. attr PhilipsTV ambiHueTop HUEDevice0:1-2:90:50
  3140. <br><br>
  3141. # to use default LED settings but only adjust their brightness to 50%<br>
  3142. attr PhilipsTV ambiHueTop HUEDevice0:::50</code>
  3143. </ul><br>
  3144. </ul>
  3145. </ul>
  3146. <br>
  3147. <br>
  3148. <a name="PHTVget"></a>
  3149. <b>Get</b>
  3150. <ul>
  3151. <code>get &lt;name&gt; &lt;what&gt;</code>
  3152. <br><br>
  3153. Currently, the following commands are defined:<br><br>
  3154. <ul><code>channel<br>
  3155. mute<br>
  3156. power<br>
  3157. input<br>
  3158. volume<br>
  3159. rgb<br>
  3160. </code></ul>
  3161. </ul>
  3162. <br>
  3163. <br>
  3164. <a name="PHTVattr"></a>
  3165. <b>Attributes</b><br>
  3166. <ul><ul>
  3167. <li><b>ambiHueLeft</b> - HUE devices that should get the color from left Ambilight.</li>
  3168. <li><b>ambiHueTop</b> - HUE devices that should get the color from top Ambilight.</li>
  3169. <li><b>ambiHueRight</b> - HUE devices that should get the color from right Ambilight.</li>
  3170. <li><b>ambiHueBottom</b> - HUE devices that should get the color from bottom Ambilight.</li>
  3171. <li><b>ambiHueLatency</b> - Controls the update interval for HUE devices in milliseconds; defaults to 200 ms.</li>
  3172. <li><b>channelsMax</b> - Maximum amount of channels shown in FHEMWEB. Defaults to 80.</li>
  3173. <li><b>disable</b> - Disable polling (true/false)</li>
  3174. <li><b>drippyFactor</b> - Adds some delay in seconds after low-performance devices came up to allow more time to become responsive (default=0)</li>
  3175. <li><b>inputs</b> - Presents the inputs read from device. Inputs can be renamed by adding <code>,NewName</code> right after the original name.</li>
  3176. <li><b>jsversion</b> - JointSpace protocol version; e.g. pre2014 devices use 1, 2014 devices use 5 and >=2015 devices use 6. defaults to 1</li>
  3177. <li><b>sequentialQuery</b> - avoid parallel queries for low-performance devices</li>
  3178. <li><b>timeout</b> - Set different polling timeout in seconds (default=7)</li>
  3179. </ul></ul>
  3180. <br>
  3181. <br>
  3182. <br>
  3183. <b>Generated Readings/Events:</b><br>
  3184. <ul><ul>
  3185. <li><b>ambiHue</b> - Ambilight+Hue status</li>
  3186. <li><b>ambiLEDBottom</b> - Number of LEDs of bottom Ambilight</li>
  3187. <li><b>ambiLEDLayers</b> - Number of physical LED layers</li>
  3188. <li><b>ambiLEDLeft</b> - Number of LEDs of left Ambilight</li>
  3189. <li><b>ambiLEDRight</b> - Number of LEDs of right Ambilight</li>
  3190. <li><b>ambiLEDTop</b> - Number of LEDs of top Ambilight</li>
  3191. <li><b>ambiMode</b> - current Ambilight color source</li>
  3192. <li><b>channel</b> - Shows the service name of current channel; part of FHEM-4-AV-Devices compatibility</li>
  3193. <li><b>country</b> - Set country</li>
  3194. <li><b>currentMedia</b> - The preset number of this channel; part of FHEM-4-AV-Devices compatibility</li>
  3195. <li><b>frequency</b> - Shows current channels frequency</li>
  3196. <li><b>input</b> - Shows currently used input; part of FHEM-4-AV-Devices compatibility</li>
  3197. <li><b>language</b> - Set menu language</li>
  3198. <li><b>model</b> - Device model</li>
  3199. <li><b>mute</b> - Reports the mute status of the device (can be "on" or "off")</li>
  3200. <li><b>onid</b> - The ON ID</li>
  3201. <li><b>power</b> - Reports the power status of the device (can be "on" or "off")</li>
  3202. <li><b>presence</b> - Reports the presence status of the receiver (can be "absent" or "present"). In case of an absent device, control is basically limited to turn it on again. This will only work if the device supports Wake-On-LAN packages, otherwise command "on" will have no effect.</li>
  3203. <li><b>receiveMode</b> - Receiving mode (analog or DVB)</li>
  3204. <li><b>rgb</b> - Current Ambilight color if ambiMode is not set to internal and all LEDs have the same color</li>
  3205. <li><b>rgb_X</b> - Current Ambilight color of a specific LED if ambiMode is not set to internal</li>
  3206. <li><b>serialnumber</b> - Device serial number</li>
  3207. <li><b>servicename</b> - Name for current channel</li>
  3208. <li><b>sid</b> - The S-ID</li>
  3209. <li><b>state</b> - Reports current power state and an absence of the device (can be "on", "off" or "absent")</li>
  3210. <li><b>systemname</b> - Device system name</li>
  3211. <li><b>tsid</b> - The TS ID</li>
  3212. <li><b>volume</b> - Reports current volume level of the receiver in percentage values (between 0 and 100 %)</li>
  3213. <li><b>volumeStraight</b> - Reports current volume level of the receiver in device specific range</li>
  3214. </ul></ul>
  3215. </ul>
  3216. =end html
  3217. =begin html_DE
  3218. <a name="PHTV"></a>
  3219. <h3>PHTV</h3>
  3220. <ul>
  3221. Eine deutsche Version der Dokumentation ist derzeit nicht vorhanden.
  3222. Die englische Version ist hier zu finden:
  3223. </ul>
  3224. <ul>
  3225. <a href='http://fhem.de/commandref.html#PHTV'>PHTV</a>
  3226. </ul>
  3227. =end html_DE
  3228. =cut