31_HUEDevice.pm 58 KB

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