31_HUEDevice.pm 59 KB

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