31_HUEDevice.pm 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. # $Id: 31_HUEDevice.pm 17671 2018-11-04 11:36:40Z justme1968 $
  2. # "Hue Personal Wireless Lighting" is a trademark owned by Koninklijke Philips Electronics N.V.,
  3. # see www.meethue.com for more information.
  4. # I am in no way affiliated with the Philips organization.
  5. package main;
  6. use strict;
  7. use warnings;
  8. use Color;
  9. use POSIX;
  10. use JSON;
  11. use SetExtensions;
  12. #require "30_HUEBridge.pm";
  13. #require "$attr{global}{modpath}/FHEM/30_HUEBridge.pm";
  14. use vars qw(%FW_webArgs); # all arguments specified in the GET
  15. my %hueModels = (
  16. LCT001 => {name => 'Hue Bulb' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  17. gamut => 'B', icon => 'hue_filled_white_and_color_e27_b22', },
  18. LCT002 => {name => 'Hue Spot BR30' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  19. gamut => 'B', icon => 'hue_filled_br30.svg', },
  20. LCT003 => {name => 'Hue Spot GU10' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  21. gamut => 'B', icon => 'hue_filled_gu10_par16', },
  22. LCT007 => {name => 'Hue Bulb V2' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  23. gamut => 'B', icon => 'hue_filled_white_and_color_e27_b22', },
  24. LCT010 => {name => 'Hue Bulb V3' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  25. gamut => 'C', icon => 'hue_filled_white_and_color_e27_b22', },
  26. LCT011 => {name => 'Hue BR30' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  27. gamut => 'C', icon => 'hue_filled_br30.svg', },
  28. LCT012 => {name => 'Hue color candle' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  29. gamut => 'C', },
  30. LCT014 => {name => 'Hue Bulb V3' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  31. gamut => 'C', icon => 'hue_filled_white_and_color_e27_b22', },
  32. LLC001 => {name => 'Living Colors G2' ,type => 'Color light' ,subType => 'colordimmer',
  33. gamut => 'A', icon => 'hue_filled_iris', },
  34. LLC005 => {name => 'Living Colors Bloom' ,type => 'Color light' ,subType => 'colordimmer',
  35. gamut => 'A', icon => 'hue_filled_bloom', },
  36. LLC006 => {name => 'Living Colors Gen3 Iris' ,type => 'Color light' ,subType => 'colordimmer',
  37. gamut => 'A', icon => 'hue_filled_iris', },
  38. LLC007 => {name => 'Living Colors Gen3 Bloom' ,type => 'Color light' ,subType => 'colordimmer',
  39. gamut => 'A', icon => 'hue_filled_bloom', },
  40. LLC010 => {name => 'Hue Living Colors Iris' ,type => 'Color light' ,subType => 'colordimmer',
  41. gamut => 'A', icon => 'hue_filled_iris', },
  42. LLC011 => {name => 'Hue Living Colors Bloom' ,type => 'Color light' ,subType => 'colordimmer',
  43. gamut => 'A', icon => 'hue_filled_bloom', },
  44. LLC012 => {name => 'Hue Living Colors Bloom' ,type => 'Color light' ,subType => 'colordimmer',
  45. gamut => 'A', icon => 'hue_filled_bloom', },
  46. LLC013 => {name => 'Disney Living Colors' ,type => 'Color light' ,subType => 'colordimmer',
  47. gamut => 'A', icon => 'hue_filled_storylight', },
  48. LLC014 => {name => 'Living Colors Aura' ,type => 'Color light' ,subType => 'colordimmer',
  49. gamut => 'A', icon => 'hue_filled_aura', },
  50. LLC020 => {name => 'Hue Go' ,type => 'Color light' ,subType => 'extcolordimmer',
  51. gamut => 'C', icon => 'hue_filled_go', },
  52. LST001 => {name => 'Hue LightStrips' ,type => 'Color light' ,subType => 'colordimmer',
  53. gamut => 'A', icon => 'hue_filled_lightstrip', },
  54. LST002 => {name => 'Hue LightStrips Plus' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  55. gamut => 'C', icon => 'hue_filled_lightstrip', },
  56. LWB001 => {name => 'Living Whites Bulb' ,type => 'Dimmable light' ,subType => 'dimmer',
  57. icon => 'hue_filled_living_whites', },
  58. LWB003 => {name => 'Living Whites Bulb' ,type => 'Dimmable light' ,subType => 'dimmer',
  59. icon => 'hue_filled_living_whites', },
  60. LWB004 => {name => 'Hue Lux' ,type => 'Dimmable light' ,subType => 'dimmer',
  61. icon => 'hue_filled_white_and_color_e27_b22', },
  62. LWB006 => {name => 'Hue Lux' ,type => 'Dimmable light' ,subType => 'dimmer',
  63. icon => 'hue_filled_white_and_color_e27_b22', },
  64. LWB007 => {name => 'Hue Lux' ,type => 'Dimmable light' ,subType => 'dimmer',
  65. icon => 'hue_filled_white_and_color_e27_b22', },
  66. LWB010 => {name => 'Hue Lux' ,type => 'Dimmable light' ,subType => 'dimmer',
  67. icon => 'hue_filled_white_and_color_e27_b22', },
  68. LWB014 => {name => 'Hue Lux' ,type => 'Dimmable light' ,subType => 'dimmer',
  69. icon => 'hue_filled_white_and_color_e27_b22', },
  70. LTW001 => {name => 'Hue A19 White Ambience' ,type => 'Color temperature light' ,subType => 'ctdimmer',
  71. icon => 'hue_filled_white_and_color_e27_b22', },
  72. LTW004 => {name => 'Hue A19 White Ambience' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  73. LTW012 => {name => 'Hue ambiance candle' ,type => 'Color temperature light' ,subType => 'ctdimmer',
  74. icon => 'hue_filled_gu10_par16', },
  75. LTW013 => {name => 'Hue GU10 White Ambience' ,type => 'Color temperature light' ,subType => 'ctdimmer',
  76. icon => 'hue_filled_gu10_par16', },
  77. LTW014 => {name => 'Hue GU10 White Ambience' ,type => 'Color temperature light' ,subType => 'ctdimmer',
  78. icon => 'hue_filled_gu10_par16', },
  79. LLM001 => {name => 'Color Light Module' ,type => 'Extended color light' ,subType => 'extcolordimmer',
  80. gamut => 'B', },
  81. LLM010 => {name => 'Color Temperature Module' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  82. LLM011 => {name => 'Color Temperature Module' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  83. LLM012 => {name => 'Color Temperature Module' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  84. LWL001 => {name => 'LivingWhites Outlet' ,type => 'Dimmable plug-in unit' ,subType => 'dimmer',
  85. icon => 'hue_filled_outlet', },
  86. RWL020 => {name => 'Hue Dimmer Switch' ,type => 'ZLLSwitch' ,subType => 'sensor',
  87. icon => 'hue_filled_hds', },
  88. RWL021 => {name => 'Hue Dimmer Switch' ,type => 'ZLLSwitch' ,subType => 'sensor',
  89. icon => 'hue_filled_hds', },
  90. ZGPSWITCH => {name => 'Hue Tap' ,type => 'ZGPSwitch' ,subType => 'sensor',
  91. icon => 'hue_filled_tap', },
  92. 'FLS-H3' => {name => 'dresden elektronik FLS-H lp' ,type => 'Color temperature light' ,subType => 'ctdimmer',},
  93. 'FLS-PP3' => {name => 'dresden elektronik FLS-PP lp' ,type => 'Extended color light' ,subType => 'extcolordimmer', },
  94. 'Flex RGBW' => {name => 'LIGHTIFY Flex RGBW' ,type => 'Extended color light' ,subType => 'extcolordimmer', },
  95. 'Classic A60 RGBW' => {name => 'LIGHTIFY Classic A60 RGBW' ,type => 'Extended color light' ,subType => 'extcolordimmer', },
  96. 'CLA60 RGBW OSRAM' => {name => 'SMART+ Classic A60 RGBW' ,type => 'Extended color light' ,subType => 'extcolordimmer', },
  97. 'Gardenspot RGB' => {name => 'LIGHTIFY Gardenspot Mini RGB' ,type => 'Color light' ,subType => 'colordimmer', },
  98. 'Surface Light TW' => {name => 'LIGHTIFY Surface light tunable white' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  99. 'Classic A60 TW' => {name => 'LIGHTIFY Classic A60 tunable white' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  100. 'Classic B40 TW' => {name => 'LIGHTIFY Classic B40 tunable white' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  101. 'PAR16 50 TW' => {name => 'LIGHTIFY PAR16 50 tunable white' ,type => 'Color temperature light' ,subType => 'ctdimmer', },
  102. 'Classic A60' => {name => 'LIGHTIFY Classic A60 dimmable light' ,type => 'Dimmable Light' ,subType => 'dimmer', },
  103. 'Plug - LIGHTIFY' => {name => 'LIGHTIFY Plug' ,type => 'On/Off plug-in unit ' ,subType => 'switch', },
  104. 'Plug 01' => {name => 'LIGHTIFY Plug' ,type => 'On/Off plug-in unit ' ,subType => 'switch', },
  105. 'RM01' => {name => 'Busch-Jaeger ZigBee Light Link Relais', type => 'On/Off light' ,subType => 'switch', },
  106. 'DM01' => {name => 'Busch-Jaeger ZigBee Light Link Dimmer', type => 'Dimmable light' ,subType => 'dimmer', },
  107. );
  108. my %gamut = (
  109. A => { r => { hue => 0, x => 0.704, y => 0.296 },
  110. g => { hue => 100, x => 0.2151, y => 0.7106 },
  111. b => { hue => 184, x => 0.138, y => 0.08 }, },
  112. B => { r => { hue => 0, x => 0.675, y => 0.322 },
  113. g => { hue => 100, x => 0.409, y => 0.518 },
  114. b => { hue => 184, x => 0.167, y => 0.04 }, },
  115. C => { r => { hue => 0, x => 0.692, y => 0.308 },
  116. g => { hue => 100, x => 0.17, y => 0.7 },
  117. b => { hue => 184, x => 0.153, y => 0.048 }, },
  118. );
  119. my %dim_values = (
  120. 0 => "dim06%",
  121. 1 => "dim12%",
  122. 2 => "dim18%",
  123. 3 => "dim25%",
  124. 4 => "dim31%",
  125. 5 => "dim37%",
  126. 6 => "dim43%",
  127. 7 => "dim50%",
  128. 8 => "dim56%",
  129. 9 => "dim62%",
  130. 10 => "dim68%",
  131. 11 => "dim75%",
  132. 12 => "dim81%",
  133. 13 => "dim87%",
  134. 14 => "dim93%",
  135. );
  136. my $HUEDevice_hasDataDumper = 1;
  137. sub HUEDevice_Initialize($)
  138. {
  139. my ($hash) = @_;
  140. # Provide
  141. #Consumer
  142. $hash->{DefFn} = "HUEDevice_Define";
  143. $hash->{UndefFn} = "HUEDevice_Undefine";
  144. $hash->{SetFn} = "HUEDevice_Set";
  145. $hash->{GetFn} = "HUEDevice_Get";
  146. $hash->{AttrFn} = "HUEDevice_Attr";
  147. $hash->{AttrList} = "IODev ".
  148. "delayedUpdate:1 ".
  149. "ignoreReachable:1,0 ".
  150. "realtimePicker:1,0 ".
  151. "color-icons:1,2 ".
  152. "transitiontime ".
  153. "model:".join(",", sort map { $_ =~ s/ /#/g ;$_} keys %hueModels)." ".
  154. "setList:textField-long ".
  155. "subType:extcolordimmer,colordimmer,ctdimmer,dimmer,switch ".
  156. $readingFnAttributes;
  157. #$hash->{FW_summaryFn} = "HUEDevice_summaryFn";
  158. FHEM_colorpickerInit();
  159. eval "use Data::Dumper";
  160. $HUEDevice_hasDataDumper = 0 if($@);
  161. }
  162. sub
  163. HUEDevice_devStateIcon($)
  164. {
  165. my($hash) = @_;
  166. $hash = $defs{$hash} if( ref($hash) ne 'HASH' );
  167. return undef if( !$hash );
  168. my $name = $hash->{NAME};
  169. if( $hash->{helper}->{devtype} && $hash->{helper}->{devtype} eq 'G' ) {
  170. if( $hash->{IODev} ) {
  171. my $createGroupReadings = AttrVal($hash->{IODev}{NAME},"createGroupReadings",undef);
  172. if( defined($createGroupReadings) ) {
  173. return undef if( $createGroupReadings && !AttrVal($hash->{NAME},"createGroupReadings", 1) );
  174. return undef if( !$createGroupReadings && !AttrVal($hash->{NAME},"createGroupReadings", undef) );
  175. return ".*:light_question:toggle" if( !$hash->{helper}{reachable} );
  176. return ".*:off:toggle" if( ReadingsVal($name,"onoff","0") eq "0" );
  177. my $pct = ReadingsVal($name,"pct","100");
  178. my $s = $dim_values{int($pct/7)};
  179. $s="on" if( $pct eq "100" );
  180. return ".*:$s:toggle";
  181. }
  182. }
  183. #return ".*:off:toggle" if( !ReadingsVal($name,'any_on',0) );
  184. #return ".*:on:toggle" if( ReadingsVal($name,'any_on',0) );
  185. return undef;
  186. }
  187. return undef if( $hash->{helper}->{devtype} );
  188. return ".*:light_question:toggle" if( !$hash->{helper}{reachable} );
  189. return ".*:off:toggle" if( ReadingsVal($name,"state","off") eq "off" );
  190. my $pct = ReadingsVal($name,"pct","100");
  191. my $s = $dim_values{int($pct/7)};
  192. $s="on" if( $pct eq "100" );
  193. return ".*:$s:toggle" if( AttrVal($name, "model", "") eq "LWL001" );
  194. return ".*:$s:toggle" if( AttrVal($name, "subType", "") eq "dimmer" );
  195. #return ".*:$s:toggle" if( AttrVal($name, "model", "") eq "LWB001" );
  196. #return ".*:$s:toggle" if( AttrVal($name, "model", "") eq "LWB003" );
  197. #return ".*:$s:toggle" if( AttrVal($name, "model", "") eq "LWB004" );
  198. return ".*:$s@#".CommandGet("","$name RGB").":toggle" if( $pct < 100 && AttrVal($name, "color-icons", 0) == 2 );
  199. return ".*:on@#".CommandGet("","$name rgb").":toggle" if( AttrVal($name, "color-icons", 0) != 0 );
  200. return '<div style="width:32px;height:19px;'.
  201. 'border:1px solid #fff;border-radius:8px;background-color:#'.CommandGet("","$name rgb").';"></div>';
  202. }
  203. sub
  204. HUEDevice_summaryFn($$$$)
  205. {
  206. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  207. my $hash = $defs{$d};
  208. my $name = $hash->{NAME};
  209. return HUEDevice_devStateIcon($hash);
  210. }
  211. sub HUEDevice_Define($$)
  212. {
  213. my ($hash, $def) = @_;
  214. my @args = split("[ \t]+", $def);
  215. $hash->{helper}->{devtype} = "";
  216. if( $args[2] eq "group" ) {
  217. $hash->{helper}->{devtype} = "G";
  218. splice( @args, 2, 1 );
  219. } elsif( $args[2] eq "sensor" ) {
  220. $hash->{helper}->{devtype} = "S";
  221. splice( @args, 2, 1 );
  222. }
  223. my $iodev;
  224. my $i = 0;
  225. foreach my $param ( @args ) {
  226. if( $param =~ m/IODev=([^\s]*)/ ) {
  227. $iodev = $1;
  228. splice( @args, $i, 1 );
  229. last;
  230. }
  231. $i++;
  232. }
  233. return "Usage: define <name> HUEDevice [group|sensor] <id> [interval]" if(@args < 3);
  234. my ($name, $type, $id, $interval) = @args;
  235. $hash->{STATE} = 'Initialized';
  236. $hash->{ID} = $hash->{helper}->{devtype}.$id;
  237. AssignIoPort($hash,$iodev) if( !$hash->{IODev} );
  238. if(defined($hash->{IODev})) {
  239. Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME};
  240. } else {
  241. Log3 $name, 1, "$name: no I/O device";
  242. }
  243. $iodev = $hash->{IODev}->{NAME} if( defined($hash->{IODev}) );
  244. my $code = $hash->{ID};
  245. $code = $iodev ."-". $code if( defined($iodev) );
  246. my $d = $modules{HUEDevice}{defptr}{$code};
  247. return "HUEDevice device $hash->{ID} on HUEBridge $iodev already defined as $d->{NAME}."
  248. if( defined($d)
  249. && $d->{IODev} && $hash->{IODev} && $d->{IODev} == $hash->{IODev}
  250. && $d->{NAME} ne $name );
  251. $modules{HUEDevice}{defptr}{$code} = $hash;
  252. if( AttrVal($iodev, "pollDevices", 1) ) {
  253. $interval = undef unless defined($interval);
  254. } elsif( !$hash->{helper}->{devtype} || $hash->{helper}->{devtype} ne 'G' ) {
  255. $interval = 60 unless defined($interval);
  256. }
  257. $args[3] = "" if( !defined( $args[3] ) );
  258. if( !$hash->{helper}->{devtype} ) {
  259. $hash->{DEF} = "$id $args[3] IODev=$iodev" if( $iodev );
  260. $interval = 60 if( defined($interval) && $interval < 10 );
  261. $hash->{INTERVAL} = $interval;
  262. $hash->{helper}{on} = -1;
  263. $hash->{helper}{reachable} = undef;
  264. $hash->{helper}{colormode} = '';
  265. $hash->{helper}{bri} = -1;
  266. $hash->{helper}{ct} = -1;
  267. $hash->{helper}{hue} = -1;
  268. $hash->{helper}{sat} = -1;
  269. $hash->{helper}{xy} = '';
  270. $hash->{helper}{alert} = '';
  271. $hash->{helper}{effect} = '';
  272. $hash->{helper}{pct} = -1;
  273. $hash->{helper}{rgb} = "";
  274. $attr{$name}{devStateIcon} = '{(HUEDevice_devStateIcon($name),"toggle")}' if( !defined( $attr{$name}{devStateIcon} ) );
  275. my $icon_path = AttrVal("WEB", "iconPath", "default:fhemSVG:openautomation" );
  276. $attr{$name}{'color-icons'} = 2 if( !defined( $attr{$name}{'color-icons'} ) && $icon_path =~ m/openautomation/ );
  277. } elsif( $hash->{helper}->{devtype} eq 'G' ) {
  278. $hash->{DEF} = "group $id $args[3] IODev=$iodev" if( $iodev );
  279. $interval = 60 if( defined($interval) && $interval < 10 );
  280. $hash->{INTERVAL} = $interval;
  281. $hash->{helper}{all_on} = -1;
  282. $hash->{helper}{any_on} = -1;
  283. $attr{$name}{delayedUpdate} = 1 if( !defined( $attr{$name}{delayedUpdate} ) );
  284. $attr{$name}{devStateIcon} = '{(HUEDevice_devStateIcon($name),"toggle")}' if( !defined( $attr{$name}{devStateIcon} ) );
  285. my $icon_path = AttrVal("WEB", "iconPath", "default:fhemSVG:openautomation" );
  286. $attr{$name}{'color-icons'} = 2 if( !defined( $attr{$name}{'color-icons'} ) && $icon_path =~ m/openautomation/ );
  287. addToDevAttrList($name, "createActionReadings:1,0");
  288. addToDevAttrList($name, "createGroupReadings:1,0");
  289. } elsif( $hash->{helper}->{devtype} eq 'S' ) {
  290. $hash->{DEF} = "sensor $id $args[3] IODev=$iodev" if( $iodev );
  291. $interval = 60 if( defined($interval) && $interval < 1 );
  292. $hash->{INTERVAL} = $interval;
  293. }
  294. RemoveInternalTimer($hash);
  295. if( $init_done ) {
  296. HUEDevice_GetUpdate($hash);
  297. } else {
  298. InternalTimer(gettimeofday()+10, "HUEDevice_GetUpdate", $hash, 0);
  299. }
  300. return undef;
  301. }
  302. sub HUEDevice_Undefine($$)
  303. {
  304. my ($hash,$arg) = @_;
  305. RemoveInternalTimer($hash);
  306. my $code = $hash->{ID};
  307. $code = $hash->{IODev}->{NAME} ."-". $code if( defined($hash->{IODev}->{NAME}) );
  308. delete($modules{HUEDevice}{defptr}{$code});
  309. return undef;
  310. }
  311. sub
  312. HUEDevice_SetParam($$@)
  313. {
  314. my ($name, $obj, $cmd, $value, $value2) = @_;
  315. if( $cmd eq "color" ) {
  316. $value = int(1000000/$value);
  317. $cmd = 'ct';
  318. } elsif( $name && $cmd eq "toggle" ) {
  319. $cmd = ReadingsVal($name,"onoff",1) ? "off" :"on";
  320. } elsif( $cmd =~ m/^dim(\d+)/ ) {
  321. $value2 = $value;
  322. $value = $1;
  323. $value = 0 if( $value < 0 );
  324. $value = 100 if( $value > 100 );
  325. $cmd = 'pct';
  326. } elsif( !defined($value) && $cmd =~ m/^(\d+)/) {
  327. $value2 = $value;
  328. $value = $1;
  329. $value = 0 if( $value < 0 );
  330. $value = 254 if( $value > 254 );
  331. $cmd = 'bri';
  332. }
  333. if($cmd eq "pct" && $value == 0 ) {
  334. $cmd = "off";
  335. $value = $value2;
  336. }
  337. if($cmd eq 'on') {
  338. $obj->{'on'} = JSON::true;
  339. # temporary disablea for everything. hast do be disabled for groups.
  340. # see https://forum.fhem.de/index.php/topic,11020.msg497825.html#msg497825
  341. #$obj->{'bri'} = 254 if( $name && ReadingsVal($name,"bri","0") eq 0 && AttrVal($name, 'subType', 'dimmer') ne 'switch' );
  342. $obj->{'transitiontime'} = $value * 10 if( defined($value) );
  343. } elsif($cmd eq 'off') {
  344. $obj->{'on'} = JSON::false;
  345. $obj->{'transitiontime'} = $value * 10 if( defined($value) );
  346. } elsif($cmd eq "pct") {
  347. my $bri;
  348. if( $value > 50 ) {
  349. $bri = 2.57 * ($value-50) + 128;
  350. } else {
  351. $bri = 2.59 * ($value-50) + 128;
  352. }
  353. $bri = 0 if( $bri < 0 );
  354. $bri = 254 if( $bri > 254 );
  355. #$value = 3.5 if( $value < 3.5 && AttrVal($name, "model", "") eq "LWL001" );
  356. $obj->{'on'} = JSON::true;
  357. #$obj->{'bri'} = int(2.55 * $value);
  358. $obj->{'bri'} = int($bri);
  359. $obj->{'transitiontime'} = $value2 * 10 if( defined($value2) );
  360. } elsif($cmd eq "bri") {
  361. #$value = 8 if( $value < 8 && AttrVal($name, "model", "") eq "LWL001" );
  362. $obj->{'on'} = JSON::true;
  363. $obj->{'bri'} = 0+$value;
  364. $obj->{'transitiontime'} = $value2 * 10 if( defined($value2) );
  365. } elsif($name && $cmd eq "dimUp") {
  366. if( $defs{$name}->{IODev}->{helper}{apiversion} && $defs{$name}->{IODev}->{helper}{apiversion} >= (1<<16) + (7<<8) ) {
  367. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  368. $obj->{'bri_inc'} = 25;
  369. $obj->{'bri_inc'} = 0+$value if( defined($value) );
  370. #$obj->{'transitiontime'} = 1;
  371. #$defs{$name}->{helper}->{update_timeout} = 0;
  372. } else {
  373. my $bri = ReadingsVal($name,"bri","0");
  374. $bri += 25;
  375. $bri = 254 if( $bri > 254 );
  376. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  377. $obj->{'bri'} = 0+$bri;
  378. $obj->{'transitiontime'} = 1;
  379. #$obj->{'transitiontime'} = $value * 10 if( defined($value) );
  380. $defs{$name}->{helper}->{update_timeout} = 0;
  381. }
  382. } elsif($name && $cmd eq "dimDown") {
  383. if( $defs{$name}->{IODev}->{helper}{apiversion} && $defs{$name}->{IODev}->{helper}{apiversion} >= (1<<16) + (7<<8) ) {
  384. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  385. $obj->{'bri_inc'} = -25;
  386. $obj->{'bri_inc'} = 0-$value if( defined($value) );
  387. #$obj->{'transitiontime'} = 1;
  388. #$defs{$name}->{helper}->{update_timeout} = 0;
  389. } else {
  390. my $bri = ReadingsVal($name,"bri","0");
  391. $bri -= 25;
  392. $bri = 0 if( $bri < 0 );
  393. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  394. $obj->{'bri'} = 0+$bri;
  395. $obj->{'transitiontime'} = 1;
  396. #$obj->{'transitiontime'} = $value * 10 if( defined($value) );
  397. $defs{$name}->{helper}->{update_timeout} = 0;
  398. }
  399. } elsif($cmd eq "satUp") {
  400. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  401. $obj->{'sat_inc'} = 25;
  402. $obj->{'sat_inc'} = 0+$value if( defined($value) );
  403. } elsif($cmd eq "satDown") {
  404. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  405. $obj->{'sat_inc'} = -25;
  406. $obj->{'sat_inc'} = 0+$value if( defined($value) );
  407. } elsif($cmd eq "hueUp") {
  408. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  409. $obj->{'hue_inc'} = 6553;
  410. $obj->{'hue_inc'} = 0+$value if( defined($value) );
  411. } elsif($cmd eq "hueDown") {
  412. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  413. $obj->{'hue_inc'} = -6553;
  414. $obj->{'hue_inc'} = 0+$value if( defined($value) );
  415. } elsif($cmd eq "ctUp") {
  416. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  417. $obj->{'ct_inc'} = 16;
  418. $obj->{'ct_inc'} = 0+$value if( defined($value) );
  419. } elsif($cmd eq "ctDown") {
  420. $obj->{'on'} = JSON::true if( !$defs{$name}->{helper}{on} );
  421. $obj->{'ct_inc'} = -16;
  422. $obj->{'ct_inc'} = 0+$value if( defined($value) );
  423. } elsif($cmd eq "ct") {
  424. $obj->{'on'} = JSON::true;
  425. $value = int(1000000/$value) if( $value > 1000 );
  426. $obj->{'ct'} = 0+$value;
  427. $obj->{'transitiontime'} = $value2 * 10 if( defined($value2) );
  428. } elsif($cmd eq "hue") {
  429. $obj->{'on'} = JSON::true;
  430. $obj->{'hue'} = 0+$value;
  431. $obj->{'transitiontime'} = $value2 * 10 if( defined($value2) );
  432. } elsif($cmd eq "sat") {
  433. $obj->{'on'} = JSON::true;
  434. $obj->{'sat'} = 0+$value;
  435. $obj->{'transitiontime'} = $value2 * 10 if( defined($value2) );
  436. } elsif($cmd eq "xy" && $value =~ m/^(.+),(.+)/) {
  437. my ($x,$y) = ($1, $2);
  438. $obj->{'on'} = JSON::true;
  439. $obj->{'xy'} = [0+$x, 0+$y];
  440. $obj->{'transitiontime'} = $value2 * 10 if( defined($value2) );
  441. } elsif( $cmd eq "rgb" && $value =~ m/^(..)(..)(..)/) {
  442. my( $r, $g, $b ) = (hex($1)/255.0, hex($2)/255.0, hex($3)/255.0);
  443. if( $name && ( !defined( AttrVal($name, "model", undef) )
  444. || AttrVal($name, "model", undef) eq 'LLC020') ) {
  445. my( $h, $s, $v ) = Color::rgb2hsv($r,$g,$b);
  446. $obj->{'on'} = JSON::true;
  447. $obj->{'hue'} = int( $h * 65535 );
  448. $obj->{'sat'} = int( $s * 254 );
  449. $obj->{'bri'} = int( $v * 254 );
  450. } else {
  451. # calculation from http://www.everyhue.com/vanilla/discussion/94/rgb-to-xy-or-hue-sat-values/p1
  452. my $X = 1.076450 * $r - 0.237662 * $g + 0.161212 * $b;
  453. my $Y = 0.410964 * $r + 0.554342 * $g + 0.034694 * $b;
  454. my $Z = -0.010954 * $r - 0.013389 * $g + 1.024343 * $b;
  455. #Log3 $name, 3, "rgb: ". $r . " " . $g ." ". $b;
  456. #Log3 $name, 3, "XYZ: ". $X . " " . $Y ." ". $Y;
  457. if( $X != 0
  458. || $Y != 0
  459. || $Z != 0 ) {
  460. my $x = $X / ($X + $Y + $Z);
  461. my $y = $Y / ($X + $Y + $Z);
  462. #Log3 $name, 3, "xyY:". $x . " " . $y ." ". $Y;
  463. $Y = 1 if( $Y > 1 );
  464. $x = 0 if( $x < 0);
  465. $x = 1 if( $x > 1);
  466. $y = 0 if( $y < 0);
  467. $y = 1 if( $y > 1);
  468. my $bri = maxNum($r,$g,$b);
  469. #my $bri = $Y;
  470. $obj->{'on'} = JSON::true;
  471. $obj->{'xy'} = [0+$x, 0+$y];
  472. $obj->{'bri'} = int(254*$bri);
  473. } else {
  474. $obj->{'on'} = JSON::false;
  475. }
  476. }
  477. } elsif( $cmd eq "hsv" && $value =~ m/^(..)(..)(..)/) {
  478. my( $h, $s, $v ) = (hex($1), hex($2), hex($3));
  479. $s = 254 if( $s > 254 );
  480. $v = 254 if( $v > 254 );
  481. $obj->{'on'} = JSON::true;
  482. $obj->{'hue'} = int($h*256);
  483. $obj->{'sat'} = 0+$s;
  484. $obj->{'bri'} = 0+$v;
  485. } elsif( $cmd eq "alert" ) {
  486. $obj->{'alert'} = $value;
  487. } elsif( $cmd eq "effect" ) {
  488. $obj->{'effect'} = $value;
  489. } elsif( $cmd eq "transitiontime" ) {
  490. $obj->{'transitiontime'} = 0+$value;
  491. } elsif( $name && $cmd eq "delayedUpdate" ) {
  492. $defs{$name}->{helper}->{update_timeout} = 1;
  493. } elsif( $name && $cmd eq "immediateUpdate" ) {
  494. $defs{$name}->{helper}->{update_timeout} = 0;
  495. } elsif( $name && $cmd eq "noUpdate" ) {
  496. $defs{$name}->{helper}->{update_timeout} = -1;
  497. } else {
  498. return 0;
  499. }
  500. return 1;
  501. }
  502. sub HUEDevice_Set($@);
  503. sub
  504. HUEDevice_Set($@)
  505. {
  506. my ($hash, $name, @aa) = @_;
  507. my ($cmd, @args) = @aa;
  508. my %obj;
  509. $hash->{helper}->{update_timeout} = AttrVal($name, "delayedUpdate", 1);
  510. if( $hash->{helper}->{devtype} eq 'G' ) {
  511. if( $cmd eq 'lights' ) {
  512. return "usage: lights <lights>" if( @args != 1 );
  513. my $obj = { 'lights' => HUEBridge_string2array($args[0]), };
  514. my $result = HUEDevice_ReadFromServer($hash,$hash->{ID},$obj);
  515. if( $result->{success} ) {
  516. RemoveInternalTimer($hash);
  517. HUEDevice_GetUpdate($hash);
  518. }
  519. return $result->{error}{description} if( $result->{error} );
  520. return undef;
  521. } elsif( $cmd eq 'savescene' ) {
  522. if( $defs{$name}->{IODev}->{helper}{apiversion} && $defs{$name}->{IODev}->{helper}{apiversion} >= (1<<16) + (11<<8) ) {
  523. return "usage: savescene <name>" if( @args < 1 );
  524. return fhem( "set $hash->{IODev}{NAME} savescene ". join( ' ', @aa[1..@aa-1]). " $hash->{NAME}" );
  525. } else {
  526. return "usage: savescene <id>" if( @args != 1 );
  527. return fhem( "set $hash->{IODev}{NAME} savescene $aa[1] $aa[1] $hash->{NAME}" );
  528. }
  529. } elsif( $cmd eq 'deletescene' ) {
  530. return "usage: deletescene <id>" if( @args != 1 );
  531. return fhem( "set $hash->{IODev}{NAME} deletescene $aa[1]" );
  532. } elsif( $cmd eq 'scene' ) {
  533. return "usage: scene <id>" if( @args != 1 );
  534. my $obj = {'scene' => $aa[1]};
  535. $hash->{helper}->{update} = 1;
  536. my $result = HUEDevice_ReadFromServer($hash,"$hash->{ID}/action",$obj);
  537. return $result->{error}{description} if( $result->{error} );
  538. if( defined($result) && $result->{'error'} ) {
  539. $hash->{STATE} = $result->{'error'}->{'description'};
  540. return undef;
  541. }
  542. return undef if( !defined($result) );
  543. if( $hash->{helper}->{update_timeout} == -1 ) {
  544. } elsif( $hash->{helper}->{update_timeout} ) {
  545. RemoveInternalTimer($hash);
  546. InternalTimer(gettimeofday()+$hash->{helper}->{update_timeout}, "HUEDevice_GetUpdate", $hash, 0);
  547. } else {
  548. RemoveInternalTimer($hash);
  549. HUEDevice_GetUpdate( $hash );
  550. }
  551. return undef;
  552. }
  553. } elsif( $hash->{helper}->{devtype} eq 'S' ) {
  554. my $shash = $defs{$name}->{IODev};
  555. my $id = $hash->{ID};
  556. $id = $1 if( $id =~ m/^S(\d.*)/ );
  557. $hash->{".triggerUsed"} = 1;
  558. if( $cmd eq "statusRequest" ) {
  559. RemoveInternalTimer($hash);
  560. HUEDevice_GetUpdate($hash);
  561. return undef;
  562. } elsif( $cmd eq 'json' ) {
  563. return HUEBridge_Set( $shash, $shash->{NAME}, 'setsensor', $id, @args );
  564. return undef;
  565. } elsif( my @match = grep { $cmd eq $_ } keys %{($hash->{helper}{setList}{cmds}?$hash->{helper}{setList}{cmds}:{})} ) {
  566. return HUEBridge_Set( $shash, $shash->{NAME}, 'setsensor', $id, $hash->{helper}{setList}{cmds}{$match[0]} );
  567. } elsif( my $entries = $hash->{helper}{setList}{regex} ) {
  568. foreach my $entry (@{$entries}) {
  569. if( join(' ', @aa) =~ /$entry->{regex}/ ) {
  570. my $VALUE1 = $1;
  571. my $VALUE2 = $2;
  572. my $VALUE3 = $3;
  573. my $json = $entry->{json};
  574. $json =~ s/\$1/$VALUE1/;
  575. $json =~ s/\$2/$VALUE2/;
  576. $json =~ s/\$3/$VALUE3/;
  577. return HUEBridge_Set( $shash, $shash->{NAME}, 'setsensor', $id, $json );
  578. }
  579. }
  580. }
  581. my $list = 'statusRequest:noArg';
  582. $list .= ' json' if( $hash->{type} && $hash->{type} =~ /^CLIP/ );
  583. $list .= ' '. join( ':noArg ', keys %{$hash->{helper}{setList}{cmds}} ) if( $hash->{helper}{setList}{cmds} );
  584. $list .= ':noArg' if( $hash->{helper}{setList}{cmds} );
  585. if( my $entries = $hash->{helper}{setList}{regex} ) {
  586. foreach my $entry (@{$entries}) {
  587. $list .= ' ';
  588. $list .= (split( ' ', $entry->{regex} ))[0];
  589. }
  590. }
  591. return SetExtensions($hash, $list, $name, @aa);
  592. }
  593. if( $cmd eq 'rename' ) {
  594. my $new_name = join( ' ', @aa[1..@aa-1]);
  595. my $obj = { 'name' => $new_name, };
  596. my $result = HUEDevice_ReadFromServer($hash,$hash->{ID},$obj);
  597. if( $result->{success} ) {
  598. RemoveInternalTimer($hash);
  599. HUEDevice_GetUpdate($hash);
  600. CommandAttr(undef,"$name alias $new_name");
  601. CommandSave(undef,undef) if( AttrVal( "autocreate", "autosave", 1 ) );
  602. }
  603. return $result->{error}{description} if( $result->{error} );
  604. return undef;
  605. }
  606. if( (my $joined = join(" ", @aa)) =~ /:/ ) {
  607. $joined =~ s/on-till\s+[^\s]+//g; #bad workaround for: https://forum.fhem.de/index.php/topic,61636.msg728557.html#msg728557
  608. my @cmds = split(":", $joined);
  609. for( my $i = 0; $i <= $#cmds; ++$i ) {
  610. HUEDevice_SetParam($name, \%obj, split(" ", $cmds[$i]) );
  611. }
  612. } else {
  613. my ($cmd, $value, $value2, @a) = @aa;
  614. if( $cmd eq "statusRequest" ) {
  615. RemoveInternalTimer($hash);
  616. HUEDevice_GetUpdate($hash);
  617. return undef;
  618. }
  619. HUEDevice_SetParam($name, \%obj, $cmd, $value, $value2);
  620. }
  621. if( %obj ) {
  622. if( defined($obj{on}) ) {
  623. $hash->{desired} = $obj{on}?1:0;
  624. }
  625. if( !defined($obj{transitiontime}) ) {
  626. my $transitiontime = AttrVal($name, "transitiontime", undef);
  627. $obj{transitiontime} = 0 + $transitiontime if( defined( $transitiontime ) );
  628. }
  629. }
  630. # if( $hash->{helper}->{update_timeout} == -1 ) {
  631. # my $diff;
  632. # my ($seconds, $microseconds) = gettimeofday();
  633. # if( $hash->{helper}->{timestamp} ) {
  634. # my ($seconds2, $microseconds2) = @{$hash->{helper}->{timestamp}};
  635. #
  636. # $diff = (($seconds-$seconds2)*1000000 + $microseconds-$microseconds2)/1000;
  637. # }
  638. # $hash->{helper}->{timestamp} = [$seconds, $microseconds];
  639. #
  640. # return undef if( $diff < 100 );
  641. # }
  642. if( scalar keys %obj ) {
  643. my $result;
  644. if( $hash->{helper}->{devtype} eq 'G' ) {
  645. $hash->{helper}->{update} = 1;
  646. $result = HUEDevice_ReadFromServer($hash,"$hash->{ID}/action",\%obj);
  647. } else {
  648. $result = HUEDevice_ReadFromServer($hash,"$hash->{ID}/state",\%obj);
  649. }
  650. SetExtensionsCancel($hash);
  651. if( defined($result) && $result->{'error'} ) {
  652. $hash->{STATE} = $result->{'error'}->{'description'};
  653. return undef;
  654. }
  655. $hash->{".triggerUsed"} = 1;
  656. return undef if( !defined($result) );
  657. if( $hash->{helper}->{update_timeout} == -1 ) {
  658. } elsif( $hash->{helper}->{update_timeout} ) {
  659. RemoveInternalTimer($hash);
  660. InternalTimer(gettimeofday()+$hash->{helper}->{update_timeout}, "HUEDevice_GetUpdate", $hash, 0);
  661. } else {
  662. RemoveInternalTimer($hash);
  663. HUEDevice_GetUpdate( $hash );
  664. }
  665. return undef;
  666. }
  667. my $subtype = AttrVal($name, "subType", "extcolordimmer");
  668. my $list = "off:noArg on:noArg toggle:noArg statusRequest:noArg";
  669. $list .= " pct:colorpicker,BRI,0,1,100 bri:colorpicker,BRI,0,1,254" if( $subtype =~ m/dimmer/ );
  670. $list .= " rgb:colorpicker,RGB" if( $subtype =~ m/color/ );
  671. $list .= " color:colorpicker,CT,2000,1,6500 ct:colorpicker,CT,154,1,500" if( $subtype =~ m/ct|ext/ );
  672. $list .= " hue:colorpicker,HUE,0,1,65535 sat:slider,0,1,254 xy effect:none,colorloop" if( $subtype =~ m/color/ );
  673. if( $defs{$name}->{IODev}->{helper}{apiversion} && $defs{$name}->{IODev}->{helper}{apiversion} >= (1<<16) + (7<<8) ) {
  674. $list .= " dimUp:noArg dimDown:noArg" if( $subtype =~ m/dimmer/ );
  675. $list .= " ctUp:noArg ctDown:noArg" if( $subtype =~ m/ct|ext/ );
  676. $list .= " hueUp:noArg hueDown:noArg satUp:noArg satDown:noArg" if( $subtype =~ m/color/ );
  677. } elsif( !$hash->{helper}->{devtype} && $subtype =~ m/dimmer/ ) {
  678. $list .= " dimUp:noArg dimDown:noArg";
  679. }
  680. $list .= " alert:none,select,lselect";
  681. #$list .= " dim06% dim12% dim18% dim25% dim31% dim37% dim43% dim50% dim56% dim62% dim68% dim75% dim81% dim87% dim93% dim100%" if( $subtype =~ m/dimmer/ );
  682. $list .= " lights" if( $hash->{helper}->{devtype} eq 'G' );
  683. $list .= " savescene deletescene scene" if( $hash->{helper}->{devtype} eq 'G' );
  684. $list .= " rename";
  685. return SetExtensions($hash, $list, $name, @aa);
  686. }
  687. sub
  688. cttorgb($)
  689. {
  690. my ($ct) = @_;
  691. # calculation from http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code
  692. # adjusted by 1000K
  693. my $temp = (1000000/$ct)/100 + 10;
  694. my $r = 0;
  695. my $g = 0;
  696. my $b = 0;
  697. $r = 255;
  698. $r = 329.698727446 * ($temp - 60) ** -0.1332047592 if( $temp > 66 );
  699. $r = 0 if( $r < 0 );
  700. $r = 255 if( $r > 255 );
  701. if( $temp <= 66 ) {
  702. $g = 99.4708025861 * log($temp) - 161.1195681661;
  703. } else {
  704. $g = 288.1221695283 * ($temp - 60) ** -0.0755148492;
  705. }
  706. $g = 0 if( $g < 0 );
  707. $g = 255 if( $g > 255 );
  708. $b = 255;
  709. $b = 0 if( $temp <= 19 );
  710. if( $temp < 66 ) {
  711. $b = 138.5177312231 * log($temp-10) - 305.0447927307;
  712. }
  713. $b = 0 if( $b < 0 );
  714. $b = 255 if( $b > 255 );
  715. return( $r, $g, $b );
  716. }
  717. sub
  718. xyYtorgb($$$)
  719. {
  720. # calculation from http://www.brucelindbloom.com/index.html
  721. my ($x,$y,$Y) = @_;
  722. #Log 3, "xyY:". $x . " " . $y ." ". $Y;
  723. my $r = 0;
  724. my $g = 0;
  725. my $b = 0;
  726. if( $y > 0 ) {
  727. my $X = $x * $Y / $y;
  728. my $Z = (1 - $x - $y)*$Y / $y;
  729. if( $X > 1
  730. || $Y > 1
  731. || $Z > 1 ) {
  732. my $f = maxNum($X,$Y,$Z);
  733. $X /= $f;
  734. $Y /= $f;
  735. $Z /= $f;
  736. }
  737. #Log 3, "XYZ: ". $X . " " . $Y ." ". $Y;
  738. $r = 0.7982 * $X + 0.3389 * $Y - 0.1371 * $Z;
  739. $g = -0.5918 * $X + 1.5512 * $Y + 0.0406 * $Z;
  740. $b = 0.0008 * $X + 0.0239 * $Y + 0.9753 * $Z;
  741. if( $r > 1
  742. || $g > 1
  743. || $b > 1 ) {
  744. my $f = maxNum($r,$g,$b);
  745. $r /= $f;
  746. $g /= $f;
  747. $b /= $f;
  748. }
  749. #Log 3, "rgb: ". $r . " " . $g ." ". $b;
  750. $r *= 255;
  751. $g *= 255;
  752. $b *= 255;
  753. }
  754. return( $r, $g, $b );
  755. }
  756. sub
  757. HUEDevice_Get($@)
  758. {
  759. my ($hash, @a) = @_;
  760. my $name = $a[0];
  761. return "$name: get needs at least one parameter" if(@a < 2);
  762. my $cmd= $a[1];
  763. if($cmd eq "rgb") {
  764. my $r = 0;
  765. my $g = 0;
  766. my $b = 0;
  767. my $cm = ReadingsVal($name,"colormode","");
  768. if( $cm eq "ct" ) {
  769. if( ReadingsVal($name,"ct","") =~ m/(\d+) .*/ ) {
  770. ($r,$g,$b) = cttorgb($1);
  771. }
  772. } elsif( $cm eq "hs" ) {
  773. my $h = ReadingsVal($name,"hue",0) / 65535.0;
  774. my $s = ReadingsVal($name,"sat",0) / 254.0;
  775. my $v = ReadingsVal($name,"bri",0) / 254.0;
  776. ($r,$g,$b) = Color::hsv2rgb($h,$s,$v);
  777. $r *= 255;
  778. $g *= 255;
  779. $b *= 255;
  780. } elsif( ReadingsVal($name,"xy","") =~ m/(.+),(.+)/ ) {
  781. my ($x,$y) = ($1, $2);
  782. my $Y = ReadingsVal($name,"bri","") / 254.0;
  783. ($r,$g,$b) = xyYtorgb($x,$y,$Y);
  784. }
  785. return sprintf( "%02x%02x%02x", $r+0.5, $g+0.5, $b+0.5 );
  786. } elsif($cmd eq "RGB") {
  787. my $r = 0;
  788. my $g = 0;
  789. my $b = 0;
  790. my $cm = ReadingsVal($name,"colormode","");
  791. if( $cm eq "ct" ) {
  792. if( ReadingsVal($name,"ct","") =~ m/(\d+) .*/ ) {
  793. ($r,$g,$b) = cttorgb($1);
  794. }
  795. } elsif( $cm eq "hs" ) {
  796. my $h = ReadingsVal($name,"hue",0) / 65535.0;
  797. my $s = ReadingsVal($name,"sat",0) / 254.0;
  798. my $v = 1;
  799. ($r,$g,$b) = Color::hsv2rgb($h,$s,$v);
  800. $r *= 255;
  801. $g *= 255;
  802. $b *= 255;
  803. } elsif( ReadingsVal($name,"xy","") =~ m/(.+),(.+)/ ) {
  804. my ($x,$y) = ($1, $2);
  805. my $Y = 1;
  806. ($r,$g,$b) = xyYtorgb($x,$y,$Y);
  807. }
  808. return sprintf( "%02x%02x%02x", $r+0.5, $g+0.5, $b+0.5 );
  809. } elsif ( $cmd eq "devStateIcon" ) {
  810. return HUEDevice_devStateIcon($hash);
  811. }
  812. return "Unknown argument $cmd, choose one of rgb:noArg RGB:noArg devStateIcon:noArg";
  813. }
  814. ###################################
  815. # This could be IORead in fhem, But there is none.
  816. # Read http://forum.fhem.de/index.php?t=tree&goto=54027&rid=10#msg_54027
  817. # to find out why.
  818. sub
  819. HUEDevice_ReadFromServer($@)
  820. {
  821. my ($hash,@a) = @_;
  822. my $name = $hash->{NAME};
  823. #return if(IsDummy($name) || IsIgnored($name));
  824. no strict "refs";
  825. my $ret;
  826. unshift(@a,$name);
  827. #$ret = IOWrite($hash, @a);
  828. $ret = IOWrite($hash,$hash,@a);
  829. use strict "refs";
  830. return $ret;
  831. }
  832. sub
  833. HUEDevice_GetUpdate($)
  834. {
  835. my ($hash) = @_;
  836. my $name = $hash->{NAME};
  837. if( $hash->{helper}->{devtype} eq 'G' ) {
  838. my $result = HUEDevice_ReadFromServer($hash,$hash->{ID});
  839. if( !defined($result) ) {
  840. $hash->{STATE} = "unknown";
  841. return;
  842. } elsif( $result->{'error'} ) {
  843. $hash->{STATE} = $result->{'error'}->{'description'};
  844. return;
  845. }
  846. HUEDevice_Parse($hash,$result);
  847. } elsif( $hash->{helper}->{devtype} eq 'S' ) {
  848. }
  849. if(!$hash->{LOCAL}) {
  850. RemoveInternalTimer($hash);
  851. InternalTimer(gettimeofday()+$hash->{INTERVAL}, "HUEDevice_GetUpdate", $hash, 0) if( $hash->{INTERVAL} );
  852. }
  853. return undef if( $hash->{helper}->{devtype} eq 'G' );
  854. my $result = HUEDevice_ReadFromServer($hash,$hash->{ID});
  855. if( !defined($result) ) {
  856. $hash->{helper}{reachable} = 0;
  857. #$hash->{STATE} = "unknown";
  858. return;
  859. } elsif( $result->{'error'} ) {
  860. $hash->{helper}{reachable} = 0;
  861. $hash->{STATE} = $result->{'error'}->{'description'};
  862. return;
  863. }
  864. HUEDevice_Parse($hash,$result);
  865. HUEBridge_updateGroups($hash->{IODev}, $hash->{ID}) if( $hash->{IODev}{TYPE} eq 'HUEBridge' );
  866. }
  867. sub
  868. HUEDeviceSetIcon($;$)
  869. {
  870. my ($hash,$force) = @_;
  871. $hash = $defs{$hash} if( ref($hash) ne 'HASH' );
  872. return undef if( !$hash );
  873. my $name = $hash->{NAME};
  874. return if( defined($attr{$name}{icon}) && !$force );
  875. if( $hash->{modelid} ) {
  876. my $model = $hueModels{$hash->{modelid}};
  877. return undef if( !$model );
  878. my $icon = $model->{icon};
  879. return undef if( !$icon );
  880. $attr{$name}{icon} = $icon;
  881. } elsif( $hash->{class} ) {
  882. my $class = lc( $hash->{class} );
  883. $class =~ s/ room//;
  884. $attr{$name}{icon} = "hue_room_$class";
  885. }
  886. }
  887. sub
  888. HUEDevice_Parse($$)
  889. {
  890. my($hash,$result) = @_;
  891. my $name = $hash->{NAME};
  892. if( ref($result) ne "HASH" ) {
  893. if( ref($result) && $HUEDevice_hasDataDumper) {
  894. Log3 $name, 2, "$name: got wrong status message for $name: ". Dumper $result;
  895. } else {
  896. Log3 $name, 2, "$name: got wrong status message for $name: $result";
  897. }
  898. return undef;
  899. }
  900. Log3 $name, 4, "parse status message for $name";
  901. Log3 $name, 5, Dumper $result if($HUEDevice_hasDataDumper);
  902. $hash->{name} = $result->{name} if( defined($result->{name}) );
  903. $hash->{type} = $result->{type} if( defined($result->{type}) );
  904. $hash->{class} = $result->{class} if( defined($result->{class}) );
  905. $hash->{uniqueid} = $result->{uniqueid} if( defined($result->{uniqueid}) );
  906. if( $hash->{helper}->{devtype} eq 'G' ) {
  907. if( $result->{lights} ) {
  908. $hash->{lights} = join( ",", sort { $a <=> $b } @{$result->{lights}} );
  909. } else {
  910. $hash->{lights} = '';
  911. }
  912. if( ref($result->{state}) eq 'HASH' ) {
  913. my %readings;
  914. if( $result->{state} ) {
  915. $readings{all_on} = $result->{state}{all_on};
  916. $readings{any_on} = $result->{state}{any_on};
  917. }
  918. if( AttrVal($name, 'createActionReadings', 0) ) {
  919. if( my $state = $result->{action} ) {
  920. $readings{ct} = $state->{ct}; $readings{ct} .= " (".int(1000000/$readings{ct})."K)" if( $readings{ct} );
  921. $readings{hue} = $state->{hue};
  922. $readings{sat} = $state->{sat};
  923. $readings{bri} = $state->{bri}; $readings{bri} = $hash->{helper}{bri} if( !defined($readings{bri}) );
  924. $readings{xy} = $state->{'xy'}->[0] .",". $state->{'xy'}->[1] if( defined($state->{'xy'}) );
  925. $readings{colormode} = $state->{colormode};
  926. $readings{alert} = $state->{alert};
  927. $readings{effect} = $state->{effect};
  928. $readings{reachable} = $state->{reachable}?1:0 if( defined($state->{reachable}) );
  929. my $s = '';
  930. my $pct = -1;
  931. my $on = $state->{on}; $readings{on} = $hash->{helper}{onoff} if( !defined($on) );
  932. if( $on ) {
  933. $s = 'on';
  934. $readings{onoff} = 1;
  935. if( !defined($readings{bri}) || AttrVal($name, 'subType', 'dimmer') eq 'switch' ) {
  936. $pct = 100;
  937. } else {
  938. $pct = int($readings{bri} * 99 / 254 + 1);
  939. if( $pct > 0
  940. && $pct < 100 ) {
  941. $s = $dim_values{int($pct/7)};
  942. }
  943. $s = 'off' if( $pct == 0 );
  944. }
  945. } else {
  946. $on = 0;
  947. $s = 'off';
  948. $pct = 0;
  949. $readings{onoff} = 0;
  950. }
  951. $readings{pct} = $pct;
  952. $s = 'unreachable' if( defined($readings{reachable}) && !$readings{reachable} );
  953. #$readings{state} = $s;
  954. }
  955. }
  956. readingsBeginUpdate($hash);
  957. foreach my $key ( keys %readings ) {
  958. if( defined($readings{$key}) ) {
  959. readingsBulkUpdate($hash, $key, $readings{$key}, 1) if( !defined($hash->{helper}{$key}) || $hash->{helper}{$key} ne $readings{$key} );
  960. $hash->{helper}{$key} = $readings{$key};
  961. }
  962. }
  963. readingsEndUpdate($hash,1);
  964. }
  965. if( defined($hash->{helper}->{update}) ) {
  966. delete $hash->{helper}->{update};
  967. fhem( "set $hash->{IODev}{NAME} statusRequest" );
  968. return undef;
  969. }
  970. return undef;
  971. }
  972. $hash->{modelid} = $result->{modelid} if( defined($result->{modelid}) );
  973. $hash->{productid} = $result->{productid} if( defined($result->{productid}) );
  974. $hash->{swversion} = $result->{swversion} if( defined($result->{swversion}) );
  975. $hash->{swconfigid} = $result->{swconfigid} if( defined($result->{swconfigid}) );
  976. $hash->{manufacturername} = $result->{manufacturername} if( defined($result->{manufacturername}) );
  977. $hash->{luminaireuniqueid} = $result->{luminaireuniqueid} if( defined($result->{luminaireuniqueid}) );
  978. if( $hash->{helper}->{devtype} eq 'S' ) {
  979. my %readings;
  980. if( my $config = $result->{config} ) {
  981. $hash->{on} = $config->{on}?1:0 if( defined($config->{on}) );
  982. $hash->{reachable} = $config->{reachable}?1:0 if( defined($config->{reachable}) );
  983. $hash->{url} = $config->{url} if( defined($config->{url}) );
  984. $hash->{lat} = $config->{lat} if( defined($config->{lat}) );
  985. $hash->{long} = $config->{long} if( defined($config->{long}) );
  986. $hash->{sunriseoffset} = $config->{sunriseoffset} if( defined($config->{sunriseoffset}) );
  987. $hash->{sunsetoffset} = $config->{sunsetoffset} if( defined($config->{sunsetoffset}) );
  988. $hash->{tholddark} = $config->{tholddark} if( defined($config->{tholddark}) );
  989. $hash->{sensitivity} = $config->{sensitivity} if( defined($config->{sensitivity}) );
  990. $readings{battery} = $config->{battery} if( defined($config->{battery}) );
  991. $readings{reachable} = $config->{reachable} if( defined($config->{reachable}) );
  992. }
  993. my $lastupdated;
  994. if( my $state = $result->{state} ) {
  995. $lastupdated = $state->{lastupdated};
  996. return undef if( !$lastupdated );
  997. return undef if( $lastupdated eq 'none' );
  998. substr( $lastupdated, 10, 1, ' ' ) if($lastupdated);
  999. my $offset = 0;
  1000. if( my $iohash = $hash->{IODev} ) {
  1001. substr( $lastupdated, 10, 1, '_' );
  1002. my $sec = SVG_time_to_sec($lastupdated);
  1003. if( my $offset = $iohash->{helper}{offsetUTC} ) {
  1004. $sec += $offset;
  1005. Log3 $name, 4, "$name: offsetUTC: $offset";
  1006. }
  1007. $lastupdated = FmtDateTime($sec);
  1008. }
  1009. $hash->{lastupdated} = ReadingsVal( $name, '.lastupdated', undef ) if( !$hash->{lastupdated} );
  1010. return undef if( $hash->{lastupdated} && $hash->{lastupdated} eq $lastupdated );
  1011. Log3 $name, 4, "$name: lastupdated: $lastupdated, hash->{lastupdated}: $hash->{lastupdated}";
  1012. Log3 $name, 5, "$name: ". Dumper $result if($HUEDevice_hasDataDumper);
  1013. $hash->{lastupdated} = $lastupdated;
  1014. $readings{state} = $state->{status} if( defined($state->{status}) );
  1015. $readings{state} = $state->{flag}?'1':'0' if( defined($state->{flag}) );
  1016. $readings{state} = $state->{open}?'open':'closed' if( defined($state->{open}) );
  1017. $readings{state} = $state->{lightlevel} if( defined($state->{lightlevel}) && !defined($state->{lux}) );
  1018. $readings{state} = $state->{buttonevent} if( defined($state->{buttonevent}) );
  1019. $readings{state} = $state->{presence}?'motion':'nomotion' if( defined($state->{presence}) );
  1020. $readings{dark} = $state->{dark}?'1':'0' if( defined($state->{dark}) );
  1021. $readings{humidity} = $state->{humidity} * 0.01 if( defined($state->{humidity}) );
  1022. $readings{daylight} = $state->{daylight}?'1':'0' if( defined($state->{daylight}) );
  1023. $readings{temperature} = $state->{temperature} * 0.01 if( defined($state->{temperature}) );
  1024. $readings{pressure} = $state->{pressure} if( defined($state->{pressure}) );
  1025. $readings{lightlevel} = $state->{lightlevel} if( defined($state->{lightlevel}) );
  1026. $readings{lux} = $state->{lux} if( defined($state->{lux}) );
  1027. $readings{power} = $state->{power} if( defined($state->{power}) );
  1028. $readings{voltage} = $state->{voltage} if( defined($state->{voltage}) );
  1029. $readings{current} = $state->{current} if( defined($state->{current}) );
  1030. $readings{consumption} = $state->{consumption} if( defined($state->{consumption}) );
  1031. $readings{water} = $state->{water} if( defined($state->{water}) );
  1032. }
  1033. if( scalar keys %readings ) {
  1034. readingsBeginUpdate($hash);
  1035. my $i = 0;
  1036. foreach my $key ( keys %readings ) {
  1037. if( defined($readings{$key}) ) {
  1038. if( $lastupdated ) {
  1039. $hash->{'.updateTimestamp'} = $lastupdated;
  1040. $hash->{CHANGETIME}[$i] = $lastupdated;
  1041. }
  1042. readingsBulkUpdate($hash, $key, $readings{$key}, 1);
  1043. ++$i;
  1044. }
  1045. }
  1046. if( $lastupdated ) {
  1047. $hash->{'.updateTimestamp'} = $lastupdated;
  1048. $hash->{CHANGETIME}[$i] = $lastupdated;
  1049. readingsBulkUpdate($hash, '.lastupdated', $lastupdated, 0);
  1050. }
  1051. readingsEndUpdate($hash,1);
  1052. delete $hash->{CHANGETIME};
  1053. }
  1054. return undef;
  1055. }
  1056. $attr{$name}{model} = $result->{modelid} if( !defined($attr{$name}{model}) && $result->{modelid} );
  1057. if( !defined($attr{$name}{subType}) ) {
  1058. if( defined($attr{$name}{model}) ) {
  1059. if( defined($hueModels{$attr{$name}{model}}{subType}) ) {
  1060. $attr{$name}{subType} = $hueModels{$attr{$name}{model}}{subType};
  1061. HUEDeviceSetIcon($hash) if( $hash->{helper}{fromAutocreate} );
  1062. } elsif( $attr{$name}{model} =~ m/TW$/ ) {
  1063. $attr{$name}{subType} = 'ctdimmer';
  1064. } elsif( $attr{$name}{model} =~ m/RGB$/ ) {
  1065. $attr{$name}{subType} = 'colordimmer';
  1066. } elsif( $attr{$name}{model} =~ m/RGBW$/ ) {
  1067. $attr{$name}{subType} = 'extcolordimmer';
  1068. }
  1069. delete $hash->{helper}{fromAutocreate};
  1070. }
  1071. if( !defined($attr{$name}{subType}) && $hash->{type} ) {
  1072. if( $hash->{type} eq "Extended color light" ) {
  1073. $attr{$name}{subType} = 'extcolordimmer';
  1074. } elsif( $hash->{type} eq "Color light" ) {
  1075. $attr{$name}{subType} = 'colordimmer';
  1076. } elsif( $hash->{type} eq "Color temperature light" ) {
  1077. $attr{$name}{subType} = 'ctdimmer';
  1078. } elsif( $hash->{type} =~ m/Dimmable/ ) {
  1079. $attr{$name}{subType} = 'dimmer';
  1080. } elsif( $hash->{type} =~ m/On.Off/ ) {
  1081. $attr{$name}{subType} = 'switch';
  1082. }
  1083. }
  1084. } elsif( $attr{$name}{subType} eq "colordimmer" && defined($attr{$name}{model}) ) {
  1085. $attr{$name}{subType} = $hueModels{$attr{$name}{model}}{subType} if( defined($hueModels{$attr{$name}{model}}{subType}) );
  1086. }
  1087. $attr{$name}{devStateIcon} = '{(HUEDevice_devStateIcon($name),"toggle")}' if( !defined( $attr{$name}{devStateIcon} ) );
  1088. if( !defined($attr{$name}{webCmd}) && defined($attr{$name}{subType}) ) {
  1089. my $subtype = $attr{$name}{subType};
  1090. if( !$hash->{helper}->{devtype} ) {
  1091. $attr{$name}{webCmd} = 'rgb:rgb ff0000:rgb DEFF26:rgb 0000ff:ct 490:ct 380:ct 270:ct 160:toggle:on:off' if( $subtype eq "extcolordimmer" );
  1092. $attr{$name}{webCmd} = 'hue:rgb:rgb ff0000:rgb 98FF23:rgb 0000ff:toggle:on:off' if( $subtype eq "colordimmer" );
  1093. $attr{$name}{webCmd} = 'ct:ct 490:ct 380:ct 270:ct 160:toggle:on:off' if( $subtype eq "ctdimmer" );
  1094. $attr{$name}{webCmd} = 'pct:toggle:on:off' if( $subtype eq "dimmer" );
  1095. $attr{$name}{webCmd} = 'toggle:on:off' if( $subtype eq "switch" );
  1096. } elsif( $hash->{helper}->{devtype} eq 'G' ) {
  1097. $attr{$name}{webCmd} = 'on:off';
  1098. }
  1099. }
  1100. readingsBeginUpdate($hash);
  1101. my $state = $result->{'state'};
  1102. my $on = $state->{on};
  1103. $on = $hash->{helper}{on} if( !defined($on) );
  1104. my $reachable = $state->{reachable}?1:0;
  1105. $reachable = $hash->{helper}{reachable} if( !defined($state->{reachable}) );
  1106. $reachable = 1 if( !$reachable && AttrVal($name, 'ignoreReachable', 0) );
  1107. my $colormode = $state->{'colormode'};
  1108. my $bri = $state->{'bri'};
  1109. $bri = $hash->{helper}{bri} if( !defined($bri) );
  1110. my $ct = $state->{'ct'};
  1111. my $hue = $state->{'hue'};
  1112. my $sat = $state->{'sat'};
  1113. my $xy = undef;
  1114. $xy = $state->{'xy'}->[0] .",". $state->{'xy'}->[1] if( defined($state->{'xy'}) );
  1115. my $alert = $state->{alert};
  1116. my $effect = $state->{effect};
  1117. if( defined($colormode) && $colormode ne $hash->{helper}{colormode} ) {readingsBulkUpdate($hash,"colormode",$colormode);}
  1118. if( defined($bri) && $bri != $hash->{helper}{bri} ) {readingsBulkUpdate($hash,"bri",$bri);}
  1119. if( defined($ct) && $ct != $hash->{helper}{ct} ) {
  1120. if( $ct == 0 ) {
  1121. readingsBulkUpdate($hash,"ct",$ct);
  1122. }
  1123. else {
  1124. readingsBulkUpdate($hash,"ct",$ct . " (".int(1000000/$ct)."K)");
  1125. }
  1126. }
  1127. if( defined($hue) && $hue != $hash->{helper}{hue} ) {readingsBulkUpdate($hash,"hue",$hue);}
  1128. if( defined($sat) && $sat != $hash->{helper}{sat} ) {readingsBulkUpdate($hash,"sat",$sat);}
  1129. if( defined($xy) && $xy ne $hash->{helper}{xy} ) {readingsBulkUpdate($hash,"xy",$xy);}
  1130. if( !defined($hash->{helper}{reachable}) || $reachable != $hash->{helper}{reachable} ) {readingsBulkUpdate($hash,"reachable",$reachable?1:0);}
  1131. if( defined($alert) && $alert ne $hash->{helper}{alert} ) {readingsBulkUpdate($hash,"alert",$alert);}
  1132. if( defined($effect) && $effect ne $hash->{helper}{effect} ) {readingsBulkUpdate($hash,"effect",$effect);}
  1133. my $s = '';
  1134. my $pct = -1;
  1135. if( $on )
  1136. {
  1137. $s = 'on';
  1138. if( $on != $hash->{helper}{on} ) {readingsBulkUpdate($hash,"onoff",1);}
  1139. if( $bri < 0 || AttrVal($name, 'subType', 'dimmer') eq 'switch' ) {
  1140. $pct = 100;
  1141. } else {
  1142. $pct = int($bri * 99 / 254 + 1);
  1143. if( $pct > 0
  1144. && $pct < 100 ) {
  1145. $s = $dim_values{int($pct/7)};
  1146. }
  1147. $s = 'off' if( $pct == 0 );
  1148. }
  1149. }
  1150. else
  1151. {
  1152. $on = 0;
  1153. $s = 'off';
  1154. $pct = 0;
  1155. if( $on != $hash->{helper}{on} ) {readingsBulkUpdate($hash,"onoff",0);}
  1156. }
  1157. if( $pct != $hash->{helper}{pct} ) {readingsBulkUpdate($hash,"pct", $pct);}
  1158. #if( $pct != $hash->{helper}{pct} ) {readingsBulkUpdate($hash,"level", $pct . ' %');}
  1159. $s = 'unreachable' if( !$reachable );
  1160. $hash->{helper}{on} = $on if( defined($on) );
  1161. $hash->{helper}{reachable} = $reachable if( defined($reachable) );
  1162. $hash->{helper}{colormode} = $colormode if( defined($colormode) );
  1163. $hash->{helper}{bri} = $bri if( defined($bri) );
  1164. $hash->{helper}{ct} = $ct if( defined($ct) );
  1165. $hash->{helper}{hue} = $hue if( defined($hue) );
  1166. $hash->{helper}{sat} = $sat if( defined($sat) );
  1167. $hash->{helper}{xy} = $xy if( defined($xy) );
  1168. $hash->{helper}{alert} = $alert if( defined($alert) );
  1169. $hash->{helper}{effect} = $effect if( defined($effect) );
  1170. $hash->{helper}{pct} = $pct;
  1171. my $changed = $hash->{CHANGED}?1:0;
  1172. if( $s ne $hash->{STATE} ) {readingsBulkUpdate($hash,"state",$s);}
  1173. readingsEndUpdate($hash,1);
  1174. if( defined($colormode) ) {
  1175. my $rgb = CommandGet("","$name rgb");
  1176. if( $rgb ne $hash->{helper}{rgb} ) { readingsSingleUpdate($hash,"rgb", $rgb,1); };
  1177. $hash->{helper}{rgb} = $rgb;
  1178. }
  1179. $hash->{helper}->{update_timeout} = -1;
  1180. RemoveInternalTimer($hash);
  1181. return $changed;
  1182. }
  1183. sub
  1184. HUEDevice_Attr($$$;$)
  1185. {
  1186. my ($cmd, $name, $attrName, $attrVal) = @_;
  1187. if( $attrName eq "setList" ) {
  1188. my $hash = $defs{$name};
  1189. delete $hash->{helper}{setList};
  1190. return "$name is not a sensor device" if( $hash->{helper}->{devtype} ne 'S' );
  1191. return "$name is not a CLIP sensor device" if( $hash->{type} && $hash->{type} !~ m/^CLIP/ );
  1192. if( $cmd eq "set" && $attrVal ) {
  1193. foreach my $line ( split( "\n", $attrVal ) ) {
  1194. my($cmd,$json) = split( ":", $line,2 );
  1195. if( $cmd =~ m'^/(.*)/$' ) {
  1196. my $regex = $1;
  1197. $hash->{helper}{setList}{'regex'} = [] if( !$hash->{helper}{setList}{':regex'} );
  1198. push @{$hash->{helper}{setList}{'regex'}}, { regex => $regex, json => $json };
  1199. } else {
  1200. $hash->{helper}{setList}{cmds}{$cmd} = $json;
  1201. }
  1202. }
  1203. }
  1204. }
  1205. return;
  1206. }
  1207. 1;
  1208. =pod
  1209. =item summary devices connected to a phillips hue bridge or a osram lightify gateway
  1210. =item summary_DE Geräte an einer Philips HUE Bridge oder einem Osram LIGHTIFY Gateway
  1211. =begin html
  1212. <a name="HUEDevice"></a>
  1213. <h3>HUEDevice</h3>
  1214. <ul>
  1215. <br>
  1216. <a name="HUEDevice_Define"></a>
  1217. <b>Define</b>
  1218. <ul>
  1219. <code>define &lt;name&gt; HUEDevice [group|sensor] &lt;id&gt; [&lt;interval&gt;]</code><br>
  1220. <br>
  1221. Defines a device connected to a <a href="#HUEBridge">HUEBridge</a>.<br><br>
  1222. This can be a hue bulb, a living colors light or a living whites bulb or dimmer plug.<br><br>
  1223. The device status will be updated every &lt;interval&gt; seconds. 0 means no updates.
  1224. The default and minimum is 60 if the IODev has not set pollDevices to 1.
  1225. The default ist 0 if the IODev has set pollDevices to 1.
  1226. Groups are updated only on definition and statusRequest<br><br>
  1227. Examples:
  1228. <ul>
  1229. <code>define bulb HUEDevice 1</code><br>
  1230. <code>define LC HUEDevice 2</code><br>
  1231. <code>define allLights HUEDevice group 0</code><br>
  1232. </ul>
  1233. </ul><br>
  1234. <a name="HUEDevice_Readings"></a>
  1235. <b>Readings</b>
  1236. <ul>
  1237. <li>bri<br>
  1238. the brightness reported from the device. the value can be betwen 1 and 254</li>
  1239. <li>colormode<br>
  1240. the current colormode</li>
  1241. <li>ct<br>
  1242. the colortemperature in mireds and kelvin</li>
  1243. <li>hue<br>
  1244. the current hue</li>
  1245. <li>pct<br>
  1246. the current brightness in percent</li>
  1247. <li>onoff<br>
  1248. the current on/off state as 0 or 1</li>
  1249. <li>sat<br>
  1250. the current saturation</li>
  1251. <li>xy<br>
  1252. the current xy color coordinates</li>
  1253. <li>state<br>
  1254. the current state</li>
  1255. <br>
  1256. Notes:
  1257. <ul>
  1258. <li>with current bridge firware versions groups have <code>all_on</code> and <code>any_on</code> readings,
  1259. with older firmware versions groups have no readings.</li>
  1260. <li>not all readings show the actual device state. all readings not related to the current colormode have to be ignored.</li>
  1261. <li>the actual state of a device controlled by a living colors or living whites remote can be different and will
  1262. be updated after some time.</li>
  1263. </ul><br>
  1264. </ul><br>
  1265. <a name="HUEDevice_Set"></a>
  1266. <b>Set</b>
  1267. <ul>
  1268. <li>on [&lt;ramp-time&gt;]</li>
  1269. <li>off [&lt;ramp-time&gt;]</li>
  1270. <li>toggle [&lt;ramp-time&gt;]</li>
  1271. <li>statusRequest<br>
  1272. Request device status update.</li>
  1273. <li>pct &lt;value&gt; [&lt;ramp-time&gt;]<br>
  1274. dim to &lt;value&gt;<br>
  1275. Note: the FS20 compatible dimXX% commands are also accepted.</li>
  1276. <li>color &lt;value&gt;<br>
  1277. set colortemperature to &lt;value&gt; kelvin.</li>
  1278. <li>bri &lt;value&gt; [&lt;ramp-time&gt;]<br>
  1279. set brighness to &lt;value&gt;; range is 0-254.</li>
  1280. <li>dimUp [delta]</li>
  1281. <li>dimDown [delta]</li>
  1282. <li>ct &lt;value&gt; [&lt;ramp-time&gt;]<br>
  1283. set colortemperature to &lt;value&gt; in mireds (range is 154-500) or kelvin (range is 2000-6493).</li>
  1284. <li>ctUp [delta]</li>
  1285. <li>ctDown [delta]</li>
  1286. <li>hue &lt;value&gt; [&lt;ramp-time&gt;]<br>
  1287. set hue to &lt;value&gt;; range is 0-65535.</li>
  1288. <li>hueUp [delta]</li>
  1289. <li>hueDown [delta]</li>
  1290. <li>sat &lt;value&gt; [&lt;ramp-time&gt;]<br>
  1291. set saturation to &lt;value&gt;; range is 0-254.</li>
  1292. <li>satUp [delta]</li>
  1293. <li>satDown [delta]</li>
  1294. <li>xy &lt;x&gt;,&lt;y&gt; [&lt;ramp-time&gt;]<br>
  1295. set the xy color coordinates to &lt;x&gt;,&lt;y&gt;</li>
  1296. <li>alert [none|select|lselect]</li>
  1297. <li>effect [none|colorloop]</li>
  1298. <li>transitiontime &lt;time&gt;<br>
  1299. set the transitiontime to &lt;time&gt; 1/10s</li>
  1300. <li>rgb &lt;rrggbb&gt;<br>
  1301. set the color to (the nearest equivalent of) &lt;rrggbb&gt;</li>
  1302. <br>
  1303. <li>delayedUpdate</li>
  1304. <li>immediateUpdate</li>
  1305. <br>
  1306. <li>savescene &lt;id&gt;</li>
  1307. <li>deletescene &lt;id&gt;</li>
  1308. <li>scene</li>
  1309. <br>
  1310. <li>lights &lt;lights&gt;<br>
  1311. Only valid for groups. Changes the list of lights in this group.
  1312. The lights are given as a comma sparated list of fhem device names or bridge light numbers.</li>
  1313. <li>rename &lt;new name&gt;<br>
  1314. Renames the device in the bridge and changes the fhem alias.</li>
  1315. <br>
  1316. <li><a href="#setExtensions"> set extensions</a> are supported.</li>
  1317. <br>
  1318. Note:
  1319. <ul>
  1320. <li>&lt;ramp-time&gt; is given in seconds</li>
  1321. <li>multiple paramters can be set at once separated by <code>:</code><br>
  1322. Examples:<br>
  1323. <code>set LC on : transitiontime 100</code><br>
  1324. <code>set bulb on : bri 100 : color 4000</code><br></li>
  1325. </ul>
  1326. </ul><br>
  1327. <a name="HUEDevice_Get"></a>
  1328. <b>Get</b>
  1329. <ul>
  1330. <li>rgb</li>
  1331. <li>RGB</li>
  1332. <li>devStateIcon<br>
  1333. returns html code that can be used to create an icon that represents the device color in the room overview.</li>
  1334. </ul><br>
  1335. <a name="HUEDevice_Attr"></a>
  1336. <b>Attributes</b>
  1337. <ul>
  1338. <li>color-icon<br>
  1339. 1 -> use lamp color as icon color and 100% shape as icon shape<br>
  1340. 2 -> use lamp color scaled to full brightness as icon color and dim state as icon shape</li>
  1341. <li>createActionReadings<br>
  1342. create readings for the last action in group devices</li>
  1343. <li>createGroupReadings<br>
  1344. create 'artificial' readings for group devices. default depends on the createGroupReadings setting in the bridge device.</li>
  1345. <li>ignoreReachable<br>
  1346. ignore the reachable state that is reported by the hue bridge. assume the device is allways reachable.</li>
  1347. <li>setList<br>
  1348. The list of know set commands for sensor type devices. one command per line, eg.: <code><br>
  1349. attr mySensor setList present:{&lt;json&gt;}\<br>
  1350. absent:{&lt;json&gt;}</code></li>
  1351. <li>subType<br>
  1352. extcolordimmer -> device has rgb and color temperatur control<br>
  1353. colordimmer -> device has rgb controll<br>
  1354. ctdimmer -> device has color temperature control<br>
  1355. dimmer -> device has brightnes controll<br>
  1356. switch -> device has on/off controll<br></li>
  1357. <li>transitiontime<br>
  1358. default transitiontime for all set commands if not specified directly in the set.</li>
  1359. <li>delayedUpdate<br>
  1360. 1 -> the update of the device status after a set command will be delayed for 1 second. usefull if multiple devices will be switched.
  1361. </li>
  1362. <li>devStateIcon<br>
  1363. will be initialized to <code>{(HUEDevice_devStateIcon($name),"toggle")}</code> to show device color as default in room overview.</li>
  1364. <li>webCmd<br>
  1365. will be initialized to a device specific value according to subType.</li>
  1366. </ul>
  1367. </ul><br>
  1368. =end html
  1369. =cut