31_HUEDevice.pm 61 KB

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