10_EQ3BT.pm 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. #############################################################
  2. #
  3. # EQ3BT.pm (c) by Dominik Karall, 2016-2017
  4. # dominik karall at gmail dot com
  5. # $Id: 10_EQ3BT.pm 15447 2017-11-18 19:48:43Z dominik $
  6. #
  7. # FHEM module to communicate with EQ-3 Bluetooth thermostats
  8. #
  9. # Version: 2.0.2
  10. #
  11. #############################################################
  12. #
  13. # v2.0.2 - 20171118
  14. # - FEATURE: support remote bluetooth interfaces via SSH (thx@Cooltux!)
  15. #
  16. # v2.0.1 - 20170204
  17. # - BUGFIX: fix lastChangeBy
  18. # - BUGFIX: fix retry of updateStatus, updateSystemInformation
  19. # if it BlockingCall timeouts
  20. #
  21. # v2.0.0 - 20170129
  22. # - FEATURE: use all available bluetooth interfaces to communicate
  23. # with the bluetooth thermostat
  24. # - FEATURE: new reading bluetoothDevice (shows used hci device)
  25. # - CHANGE: change maximum retries to 20
  26. # - FEATURE: new set function resetErrorCounters
  27. # - FEATURE: new set function resetConsumption (not today/yesterday)
  28. # - FEATURE: new reading lastChangeBy FHEM or thermostat
  29. # indicates who was responsible for the last change
  30. # - FEATURE: support $readingFnAttributes
  31. # - FEATURE: add VERSION internal and log output
  32. # - CHANGE: updateStatus is now 3min intervall starting from
  33. # last working updateStatus
  34. # - BUGFIX: do not run parallel gatttool commands for the same device
  35. #
  36. # v1.1.3 - 20161211
  37. # - BUGFIX: better error handling if no notification was received
  38. # - BUGFIX: update system information fixed
  39. # - CHANGE: allow multiple gatttools to be executed in parallel
  40. # - CHANGE: remove error reading
  41. # - CHANGE: add errorCounters based on function (update/...)
  42. # which will be increased if reading from the thermostat
  43. # fails 30 times for one command
  44. # - BUGFIX: retry mechanism for commands with notifications (updateStatus)
  45. # - BUGFIX: remain consumption values after restart
  46. #
  47. # v1.1.2 - 20161108
  48. # - FEATURE: support set <name> eco (eco temperature)
  49. # - FEATURE: support set <name> comfort (comfort temperature)
  50. # - CHANGE: updated commandref
  51. #
  52. # v1.1.1 - 20161106
  53. # - FEATURE: new reading consumption today/yesterday
  54. # - FEATURE: new reading firmware which shows the current version
  55. # - FEATURE: support set <name> mode automatic/manual
  56. #
  57. # v1.1.0 - 20161105
  58. # - CHANGE: code cleanup to make support of new functions easier
  59. # - FEATURE: support boost on/off command
  60. # - BUGFIX: redirect stderr to stdout to avoid "Device or ressource busy"
  61. # and other error messages in the log output, only
  62. # if an action fails 20 times an error will be shown in the log
  63. #
  64. # v1.0.7 - 20161101
  65. # - FEATURE: new reading consumption
  66. # calculation based on valvePosition and time (unit = %h)
  67. # - FEATURE: new reading battery
  68. # - FEATURE: new reading boost
  69. # - FEATURE: new reading windowOpen
  70. # - CHANGE: change mode reading to Automatic/Manual only
  71. # - FEATURE: new reading ecoMode (=holiday)
  72. #
  73. # v1.0.6 - 20161028
  74. # - BUGFIX: support temperature down to 4.5 (=OFF) degrees
  75. #
  76. # v1.0.5 - 20161027
  77. # - BUGFIX: fix wrong date/time after updateStatus again
  78. #
  79. # v1.0.4 - 20161025
  80. # - BUGFIX: remove unnecessary scan command on define
  81. #
  82. # v1.0.3 - 20161024
  83. # - BUGFIX: another fix for retry mechanism
  84. # - BUGFIX: wait before gatttool execution when
  85. # another gatttool/hcitool process is running
  86. # - BUGFIX: fix wrong date/time after updateStatus
  87. #
  88. # v1.0.2 - 20161020
  89. # - FEATURE: automatically pair/trust device on define
  90. # - FEATURE: add updateStatus method to update all values
  91. # - BUGFIX: fix retry mechanism for setDesiredTemperature
  92. # - BUGFIX: fix valvePosition value
  93. # - BUGFIX: fix uninitialized value error
  94. # - BUGFIX: RemoveTimer if set desired temp works again
  95. # - BUGFIX: set error reading to "" after it works again
  96. # - BUGFIX: disconnect device on define (startup)
  97. #
  98. # v1.0.1 - 20161016
  99. # - FEATURE: read mode/desiredTemp/valvePos every 2 hours
  100. # might have impact on battery life!
  101. # - CHANGED: temperature renamed to desiredTemperature
  102. # - FEATURE: retry setTemperature 20 times if it fails
  103. #
  104. # v1.0.0 - 20161015
  105. # - FEATURE: first public release
  106. #
  107. # NOTES
  108. # command dec
  109. # DONE: boost mode command 69 00/01
  110. # temperature offset 19 (x*2)+7
  111. # request profile 32 01-07
  112. # vacation mode 64 ...
  113. # system info 00 => frameType=1,version=value[1],typeCode=value[2]
  114. # window 20 t*2 time*5
  115. # factory reset -16
  116. # DONE: comfort temp 67
  117. # lock -128 00/01
  118. # DONE: mode 64 mode<<6
  119. # DONE: temp 65 temp*2
  120. # timer 3...
  121. # start FW update -96
  122. # DONE: eco mode 68
  123. # FW data -95 ...
  124. # profile set 16 ...
  125. # set tempconf 17 comfort*2 eco*2
  126. #
  127. # TODOs
  128. # - create virtual device (wohnzimmer)
  129. # - read/set eco/comfort temperature
  130. # - read/set tempOffset
  131. # - read/set windowOpen time settings
  132. # - read/set profiles per day
  133. #
  134. #############################################################
  135. package main;
  136. use strict;
  137. use warnings;
  138. use Blocking;
  139. use Encode;
  140. use SetExtensions;
  141. sub EQ3BT_Initialize($) {
  142. my ($hash) = @_;
  143. $hash->{DefFn} = 'EQ3BT_Define';
  144. $hash->{UndefFn} = 'EQ3BT_Undef';
  145. $hash->{GetFn} = 'EQ3BT_Get';
  146. $hash->{SetFn} = 'EQ3BT_Set';
  147. $hash->{AttrFn} = 'EQ3BT_Attribute';
  148. $hash->{AttrList} = 'sshHost '.
  149. $readingFnAttributes;
  150. return undef;
  151. }
  152. sub EQ3BT_Define($$) {
  153. #save BTMAC address
  154. my ($hash, $def) = @_;
  155. my @a = split("[ \t]+", $def);
  156. my $name = $a[0];
  157. my $mac;
  158. my $sshHost;
  159. $hash->{STATE} = "initialized";
  160. $hash->{VERSION} = "2.0.2";
  161. Log3 $hash, 3, "EQ3BT: EQ-3 Bluetooth Thermostat ".$hash->{VERSION};
  162. if (int(@a) > 4) {
  163. return 'EQ3BT: Wrong syntax, must be define <name> EQ3BT <mac address> "<sshHost-IP>"';
  164. } elsif(int(@a) == 3) {
  165. $mac = $a[2];
  166. $hash->{MAC} = $a[2];
  167. } elsif(int(@a) == 4) {
  168. $mac = $a[2];
  169. $hash->{MAC} = $a[2];
  170. $attr{$name}{sshHost} = $a[3];
  171. }
  172. EQ3BT_updateHciDevicelist($hash);
  173. BlockingCall("EQ3BT_pairDevice", $name."|".$hash->{MAC});
  174. RemoveInternalTimer($hash);
  175. InternalTimer(gettimeofday()+60, "EQ3BT_updateStatus", $hash, 0);
  176. InternalTimer(gettimeofday()+20, "EQ3BT_updateSystemInformation", $hash, 0);
  177. return undef;
  178. }
  179. sub EQ3BT_updateHciDevicelist {
  180. my ($hash) = @_;
  181. my $name = $hash->{NAME};
  182. #check for hciX devices
  183. $hash->{helper}{hcidevices} = ();
  184. my @btDevices;
  185. my $sshHost = AttrVal($name,"sshHost","none");
  186. if( $sshHost ne 'none' ) {
  187. @btDevices = split("\n", qx(ssh $sshHost 'hcitool dev'));
  188. } else {
  189. @btDevices = split("\n", qx(hcitool dev));
  190. }
  191. foreach my $btDevLine (@btDevices) {
  192. if($btDevLine =~ /hci(.)/) {
  193. push(@{$hash->{helper}{hcidevices}}, $1);
  194. }
  195. }
  196. $hash->{helper}{currenthcidevice} = 0;
  197. readingsSingleUpdate($hash, "bluetoothDevice", "hci".$hash->{helper}{hcidevices}[$hash->{helper}{currenthcidevice}], 1);
  198. return undef;
  199. }
  200. sub EQ3BT_pairDevice {
  201. my ($string) = @_;
  202. my ($name, $mac) = split("\\|", $string);
  203. my $sshHost = AttrVal($name,"sshHost","none");
  204. if( $sshHost ne 'none' ) {
  205. qx(ssh $sshHost 'echo "pair $mac\\n";sleep 7;echo "trust $mac\\ndisconnect $mac\\n";sleep 2; echo "quit\\n" | bluetoothctl');
  206. } else {
  207. qx(echo "pair $mac\\n";sleep 7;echo "trust $mac\\ndisconnect $mac\\n";sleep 2; echo "quit\\n" | bluetoothctl);
  208. }
  209. return $name;
  210. }
  211. sub EQ3BT_Attribute($$$$) {
  212. my ($mode, $devName, $attrName, $attrValue) = @_;
  213. if($mode eq "set") {
  214. } elsif($mode eq "del") {
  215. }
  216. return undef;
  217. }
  218. sub EQ3BT_Set($@) {
  219. #set temperature/mode/...
  220. #BlockingCall for gatttool
  221. #handle result from BlockingCall in separate function and
  222. # write result into readings
  223. #
  224. my ($hash, $name, @params) = @_;
  225. my $workType = shift(@params);
  226. my $list = "desiredTemperature:slider,4.5,0.5,29.5,1 updateStatus:noArg boost:on,off mode:manual,automatic eco:noArg comfort:noArg ".
  227. "resetErrorCounters:noArg resetConsumption:noArg";
  228. # check parameters for set function
  229. if($workType eq "?") {
  230. return SetExtensions($hash, $list, $name, $workType, @params);
  231. }
  232. if($workType eq "desiredTemperature") {
  233. return "EQ3BT: desiredTemperature requires <temperature> in celsius degrees as additional parameter" if(int(@params) < 1);
  234. return "EQ3BT: desiredTemperature supports temperatures from 4.5 - 29.5 degrees" if($params[0]<4.5 || $params[0]>29.5);
  235. EQ3BT_setDesiredTemperature($hash, $params[0]);
  236. } elsif($workType eq "updateStatus") {
  237. $hash->{helper}{retryUpdateStatusCounter} = 0;
  238. EQ3BT_updateStatus($hash, 1);
  239. } elsif($workType eq "boost") {
  240. return "EQ3BT: boost requires on/off as additional parameter" if(int(@params) < 1);
  241. EQ3BT_setBoost($hash, $params[0]);
  242. } elsif($workType eq "mode") {
  243. return "EQ3BT: mode requires automatic/manual as additional parameter" if(int(@params) < 1);
  244. EQ3BT_setMode($hash, $params[0]);
  245. } elsif($workType eq "eco") {
  246. EQ3BT_setEco($hash);
  247. } elsif($workType eq "comfort") {
  248. EQ3BT_setComfort($hash);
  249. } elsif($workType eq "resetErrorCounters") {
  250. EQ3BT_setResetErrorCounters($hash);
  251. } elsif($workType eq "resetConsumption") {
  252. EQ3BT_setResetConsumption($hash);
  253. } elsif($workType eq "childlock") {
  254. return "EQ3BT: childlock requires on/off as additional parameter" if(int(@params) < 1);
  255. EQ3BT_setChildlock($hash, $params[0]);
  256. } elsif($workType eq "holidaymode") {
  257. return "EQ3BT: holidaymode requires YYMMDDHHMM as additional parameter" if(int(@params) < 1);
  258. EQ3BT_setHolidaymode($hash, $params[0]);
  259. } elsif($workType eq "datetime") {
  260. return "EQ3BT: datetime requires YYMMDDHHMM as additional parameter" if(int(@params) < 1);
  261. EQ3BT_setDatetime($hash, $params[0]);
  262. } elsif($workType eq "window") {
  263. return "EQ3BT: windows requires open/closed as additional parameter" if(int(@params) < 1);
  264. EQ3BT_setWindow($hash, $params[0]);
  265. } elsif($workType eq "program") {
  266. return "EQ3BT: programming the device is not supported yet";
  267. } else {
  268. return SetExtensions($hash, $list, $name, $workType, @params);
  269. }
  270. return undef;
  271. }
  272. ### resetErrorCounters ###
  273. sub EQ3BT_setResetErrorCounters {
  274. my ($hash) = @_;
  275. foreach my $reading (keys %{ $hash->{READINGS} }) {
  276. if($reading =~ /errorCount-.*/) {
  277. readingsSingleUpdate($hash, $reading, 0, 1);
  278. }
  279. }
  280. return undef;
  281. }
  282. ### resetConsumption ###
  283. sub EQ3BT_setResetConsumption {
  284. my ($hash) = @_;
  285. readingsSingleUpdate($hash, "consumption", 0, 1);
  286. return undef;
  287. }
  288. ### updateSystemInformation ###
  289. sub EQ3BT_updateSystemInformation {
  290. my ($hash) = @_;
  291. my $name = $hash->{NAME};
  292. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|updateSystemInformation|0x0411|00|listen", "EQ3BT_processGatttoolResult", 300, "EQ3BT_updateSystemInformationFailed", $hash);
  293. }
  294. sub EQ3BT_updateSystemInformationSuccessful {
  295. my ($hash, $handle, $value) = @_;
  296. InternalTimer(gettimeofday()+7200+int(rand(180)), "EQ3BT_updateSystemInformation", $hash, 0);
  297. return undef;
  298. }
  299. sub EQ3BT_updateSystemInformationRetry {
  300. my ($hash) = @_;
  301. EQ3BT_updateSystemInformation($hash);
  302. return undef;
  303. }
  304. sub EQ3BT_updateSystemInformationFailed {
  305. my ($hash) = @_;
  306. InternalTimer(gettimeofday()+7000+int(rand(180)), "EQ3BT_updateSystemInformation", $hash, 0);
  307. return undef;
  308. }
  309. ### updateStatus ###
  310. sub EQ3BT_updateStatus {
  311. my ($hash) = @_;
  312. my $name = $hash->{NAME};
  313. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|updateStatus|0x0411|03|listen", "EQ3BT_processGatttoolResult", 300, "EQ3BT_updateStatusFailed", $hash);
  314. }
  315. sub EQ3BT_updateStatusSuccessful {
  316. my ($hash, $handle, $value) = @_;
  317. InternalTimer(gettimeofday()+140+int(rand(60)), "EQ3BT_updateStatus", $hash, 0);
  318. return undef;
  319. }
  320. sub EQ3BT_updateStatusRetry {
  321. my ($hash) = @_;
  322. EQ3BT_updateStatus($hash);
  323. return undef;
  324. }
  325. sub EQ3BT_updateStatusFailed {
  326. my ($hash, $handle, $value) = @_;
  327. InternalTimer(gettimeofday()+170+int(rand(60)), "EQ3BT_updateStatus", $hash, 0);
  328. return undef;
  329. }
  330. ### setDesiredTemperature ###
  331. sub EQ3BT_setDesiredTemperature($$) {
  332. my ($hash, $desiredTemp) = @_;
  333. my $name = $hash->{NAME};
  334. my $eq3Temp = sprintf("%02X", $desiredTemp * 2);
  335. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|setDesiredTemperature|0x0411|41".$eq3Temp, "EQ3BT_processGatttoolResult", 60, "EQ3BT_killGatttool", $hash);
  336. return undef;
  337. }
  338. sub EQ3BT_setDesiredTemperatureSuccessful {
  339. my ($hash, $handle, $tempVal) = @_;
  340. my $temp = (hex($tempVal) - 0x4100) / 2;
  341. readingsSingleUpdate($hash, "desiredTemperature", sprintf("%.1f", $temp), 1);
  342. return undef;
  343. }
  344. sub EQ3BT_setDesiredTemperatureRetry {
  345. my ($hash) = @_;
  346. EQ3BT_retryGatttool($hash, "setDesiredTemperature");
  347. return undef;
  348. }
  349. ### setBoost ###
  350. sub EQ3BT_setBoost {
  351. my ($hash, $onoff) = @_;
  352. my $name = $hash->{NAME};
  353. my $data = "01";
  354. $data = "00" if($onoff eq "off");
  355. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|setBoost|0x0411|45".$data, "EQ3BT_processGatttoolResult", 60, "EQ3BT_killGatttool", $hash);
  356. return undef;
  357. }
  358. sub EQ3BT_setBoostSuccessful {
  359. my ($hash, $handle, $value) = @_;
  360. my $val = (hex($value) - 0x4500);
  361. readingsSingleUpdate($hash, "boost", $val, 1);
  362. return undef;
  363. }
  364. sub EQ3BT_setBoostRetry {
  365. my ($hash) = @_;
  366. EQ3BT_retryGatttool($hash, "setBoost");
  367. return undef;
  368. }
  369. ### setMode ###
  370. sub EQ3BT_setMode {
  371. my ($hash, $mode) = @_;
  372. my $name = $hash->{NAME};
  373. my $data = "40";
  374. $data = "00" if($mode eq "automatic");
  375. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|setMode|0x0411|40".$data."|listen", "EQ3BT_processGatttoolResult", 60, "EQ3BT_killGatttool", $hash);
  376. return undef;
  377. }
  378. sub EQ3BT_setModeSuccessful {
  379. my ($hash, $handle, $value) = @_;
  380. return undef;
  381. }
  382. sub EQ3BT_setModeRetry {
  383. my ($hash) = @_;
  384. EQ3BT_retryGatttool($hash, "setMode");
  385. return undef;
  386. }
  387. ### setEco ###
  388. sub EQ3BT_setEco {
  389. my ($hash) = @_;
  390. my $name = $hash->{NAME};
  391. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|setEco|0x0411|44|listen", "EQ3BT_processGatttoolResult", 60, "EQ3BT_killGatttool", $hash);
  392. return undef;
  393. }
  394. sub EQ3BT_setEcoSuccessful {
  395. my ($hash, $handle, $value) = @_;
  396. return undef;
  397. }
  398. sub EQ3BT_setEcoRetry {
  399. my ($hash) = @_;
  400. EQ3BT_retryGatttool($hash, "setEco");
  401. return undef;
  402. }
  403. ### setComfort ###
  404. sub EQ3BT_setComfort {
  405. my ($hash) = @_;
  406. my $name = $hash->{NAME};
  407. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $name."|".$hash->{MAC}."|setComfort|0x0411|43|listen", "EQ3BT_processGatttoolResult", 60, "EQ3BT_killGatttool", $hash);
  408. return undef;
  409. }
  410. sub EQ3BT_setComfortSuccessful {
  411. my ($hash, $handle, $value) = @_;
  412. return undef;
  413. }
  414. sub EQ3BT_setComfortRetry {
  415. my ($hash) = @_;
  416. EQ3BT_retryGatttool($hash, "setEco");
  417. return undef;
  418. }
  419. ### Gatttool functions ###
  420. sub EQ3BT_retryGatttool {
  421. my ($hash, $workType) = @_;
  422. $hash->{helper}{RUNNING_PID} = BlockingCall("EQ3BT_execGatttool", $hash->{NAME}."|".$hash->{MAC}."|$workType|".$hash->{helper}{"handle$workType"}."|".$hash->{helper}{"value$workType"}."|".$hash->{helper}{"listen$workType"}, "EQ3BT_processGatttoolResult", 60, "EQ3BT_killGatttool", $hash);
  423. return undef;
  424. }
  425. sub EQ3BT_execGatttool($) {
  426. my ($string) = @_;
  427. my ($name, $mac, $workType, $handle, $value, $listen) = split("\\|", $string);
  428. my $wait = 1;
  429. my $hash = $main::defs{$name};
  430. my $gatttool = qx(which gatttool);
  431. chomp $gatttool;
  432. if(-x $gatttool) {
  433. my $gtResult;
  434. my $cmd;
  435. my $sshHost = AttrVal($name,"sshHost","none");
  436. while($wait) {
  437. my $grepGatttool = qx(ps ax| grep -E \'gatttool -b $mac\' | grep -v grep);
  438. if(not $grepGatttool =~ /^\s*$/) {
  439. #another gattool is running
  440. Log3 $name, 5, "EQ3BT ($name): another gatttool process is running. waiting...";
  441. sleep(1);
  442. } else {
  443. $wait = 0;
  444. }
  445. }
  446. if($value eq "03") {
  447. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
  448. my $currentDate = sprintf("%02X%02X%02X%02X%02X", $year+1900-2000, $mon+1, $mday, $hour, $min);
  449. $value .= $currentDate;
  450. }
  451. my $hciDevice = "hci".$hash->{helper}{hcidevices}[$hash->{helper}{currenthcidevice}];
  452. #my $cmd = "gatttool -b $mac -i $hciDevice --char-write-req --handle=$handle --value=$value";
  453. if( $sshHost ne 'none' ) {
  454. $cmd = "ssh $sshHost 'gatttool -b $mac -i $hciDevice --char-write-req --handle=$handle --value=$value";
  455. } else {
  456. $cmd = "gatttool -b $mac -i $hciDevice --char-write-req --handle=$handle --value=$value";
  457. }
  458. if(defined($listen) && $listen eq "listen") {
  459. $cmd = "timeout 15 ".$cmd." --listen";
  460. }
  461. #redirect stderr to stdout
  462. if( $sshHost ne 'none' ) {
  463. $cmd .= " 2>&1'";
  464. } else {
  465. $cmd .= " 2>&1";
  466. }
  467. Log3 $name, 5, "EQ3BT ($name): $cmd";
  468. $gtResult = qx($cmd);
  469. chomp $gtResult;
  470. my @gtResultArr = split("\n", $gtResult);
  471. Log3 $name, 4, "EQ3BT ($name): gatttool result: ".join(",", @gtResultArr);
  472. if(defined($gtResultArr[0]) && $gtResultArr[0] eq "Characteristic value was written successfully") {
  473. #read notification
  474. if(defined($gtResultArr[1]) && $gtResultArr[1] =~ /Notification handle = 0x0421 value: (.*)/) {
  475. return "$name|$mac|ok|$workType|$handle|$value|$1";
  476. } else {
  477. if(defined($listen) && $listen eq "listen") {
  478. return "$name|$mac|error|$workType|$handle|$value|notification missing";
  479. } else {
  480. return "$name|$mac|ok|$workType|$handle|$value";
  481. }
  482. }
  483. } else {
  484. return "$name|$mac|error|$workType|$handle|$value|$workType failed";
  485. }
  486. } else {
  487. return "$name|$mac|error|$workType|$handle|$value|no gatttool binary found. Please check if bluez-package is properly installed";
  488. }
  489. }
  490. sub EQ3BT_processGatttoolResult($) {
  491. my ($string) = @_;
  492. return unless(defined($string));
  493. my @a = split("\\|", $string);
  494. my $name = $a[0];
  495. my $hash = $defs{$name};
  496. my $mac = $a[1];
  497. my $ret = $a[2];
  498. my $workType = $a[3];
  499. my $handle = $a[4];
  500. my $value = $a[5];
  501. my $notification = $a[6];
  502. delete($hash->{helper}{RUNNING_PID});
  503. Log3 $hash, 5, "EQ3BT ($name): gatttool return string: $string";
  504. $hash->{helper}{"handle$workType"} = $handle;
  505. $hash->{helper}{"value$workType"} = $value;
  506. $hash->{helper}{"listen$workType"} = $notification;
  507. if($ret eq "ok") {
  508. #process notification
  509. if(defined($notification)) {
  510. EQ3BT_processNotification($hash, $notification);
  511. }
  512. if($workType =~ /set.*/) {
  513. readingsSingleUpdate($hash, "lastChangeBy", "FHEM", 1);
  514. }
  515. #call WorkTypeSuccessful function
  516. my $call = "EQ3BT_".$workType."Successful";
  517. no strict "refs";
  518. eval {
  519. &{$call}($hash, $handle, $value);
  520. };
  521. use strict "refs";
  522. RemoveInternalTimer($hash, "EQ3BT_".$workType."Retry");
  523. $hash->{helper}{"retryCounter$workType"} = 0;
  524. } else {
  525. $hash->{helper}{"retryCounter$workType"} = 0 if(!defined($hash->{helper}{"retryCounter$workType"}));
  526. $hash->{helper}{"retryCounter$workType"}++;
  527. Log3 $hash, 4, "EQ3BT ($name): $workType failed ($handle, $value, $notification)";
  528. if ($hash->{helper}{"retryCounter$workType"} > 20) {
  529. my $errorCount = ReadingsVal($hash->{NAME}, "errorCount-$workType", 0);
  530. readingsSingleUpdate($hash, "errorCount-$workType", $errorCount+1, 1);
  531. Log3 $hash, 3, "EQ3BT ($name): $workType, $handle, $value failed 20 times.";
  532. $hash->{helper}{"retryCounter$workType"} = 0;
  533. $hash->{helper}{"retryCounterHci".$hash->{helper}{currenthcidevice}} = 0;
  534. #call WorkTypeFailed function
  535. my $call = "EQ3BT_".$workType."Failed";
  536. no strict "refs";
  537. eval {
  538. &{$call}($hash, $handle, $value);
  539. };
  540. use strict "refs";
  541. #update hci devicelist
  542. EQ3BT_updateHciDevicelist($hash);
  543. } else {
  544. $hash->{helper}{"retryCounterHci".$hash->{helper}{currenthcidevice}} = 0 if(!defined($hash->{helper}{"retryCounterHci".$hash->{helper}{currenthcidevice}}));
  545. $hash->{helper}{"retryCounterHci".$hash->{helper}{currenthcidevice}}++;
  546. if ($hash->{helper}{"retryCounterHci".$hash->{helper}{currenthcidevice}} > 7) {
  547. #reset error counter
  548. $hash->{helper}{"retryCounterHci".$hash->{helper}{currenthcidevice}} = 0;
  549. #use next hci device next time
  550. $hash->{helper}{currenthcidevice} += 1;
  551. my $maxHciDevices = @{ $hash->{helper}{hcidevices} } - 1;
  552. if($hash->{helper}{currenthcidevice} > $maxHciDevices) {
  553. $hash->{helper}{currenthcidevice} = 0;
  554. }
  555. #update reading
  556. readingsSingleUpdate($hash, "bluetoothDevice", "hci".$hash->{helper}{hcidevices}[$hash->{helper}{currenthcidevice}], 1);
  557. }
  558. InternalTimer(gettimeofday()+3+int(rand(5)), "EQ3BT_".$workType."Retry", $hash, 0);
  559. }
  560. }
  561. return undef;
  562. }
  563. sub EQ3BT_processNotification {
  564. my ($hash, $notification) = @_;
  565. my @vals = split(" ", $notification);
  566. my $frameType = $vals[0];
  567. if($frameType eq "01") {
  568. my $version = hex($vals[1]);
  569. my $typeCode = hex($vals[2]);
  570. readingsSingleUpdate($hash, "firmware", $version, 1);
  571. #readingsSingleUpdate($hash, "typeCode", $typeCode, 1);
  572. } elsif($frameType eq "02") {
  573. return undef if(!defined($vals[2]));
  574. #vals[2]
  575. my $mode = hex($vals[2]) & 1;
  576. my $modeStr = "Manual";
  577. if($mode == 0) {
  578. $modeStr = "Automatic";
  579. }
  580. my $eco = (hex($vals[2]) & 2) >> 1;
  581. my $isBoost = (hex($vals[2]) & 4) >> 2;
  582. my $dst = (hex($vals[2]) & 8) >> 3;
  583. my $wndOpen = (hex($vals[2]) & 16) >> 4;
  584. my $unknown = (hex($vals[2]) & 32) >> 5;
  585. $unknown = (hex($vals[2]) & 64) >> 6;
  586. my $isLowBattery = (hex($vals[2]) & 128) >> 7;
  587. my $batteryStr = "ok";
  588. if($isLowBattery > 0) {
  589. $batteryStr = "low";
  590. }
  591. #vals[3]
  592. my $pct = hex($vals[3]);
  593. #vals[5]
  594. my $temp = hex($vals[5]) / 2;
  595. my $timeSinceLastChange = ReadingsAge($hash->{NAME}, "valvePosition", 0);
  596. my $consumption = ReadingsVal($hash->{NAME}, "consumption", 0);
  597. my $consumptionToday = ReadingsVal($hash->{NAME}, "consumptionToday", 0);
  598. my $consumptionTodaySecSinceLastChange = ReadingsAge($hash->{NAME}, "consumptionToday", 0);
  599. my $oldVal = ReadingsVal($hash->{NAME}, "valvePosition", 0);
  600. my $consumptionDiff = 0;
  601. if($timeSinceLastChange < 600) {
  602. $consumptionDiff += ($oldVal + $pct) / 2 * $timeSinceLastChange / 3600;
  603. }
  604. EQ3BT_readingsSingleUpdateIfChanged($hash, "consumption", sprintf("%.3f", $consumption+$consumptionDiff));
  605. my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
  606. if($consumptionTodaySecSinceLastChange > ($hour*3600+$min*60+$sec)) {
  607. readingsSingleUpdate($hash, "consumptionYesterday", $consumptionToday + $consumptionDiff/2, 1);
  608. readingsSingleUpdate($hash, "consumptionToday", 0 + $consumptionDiff/2, 1);
  609. } else {
  610. EQ3BT_readingsSingleUpdateIfChanged($hash, "consumptionToday", sprintf("%.3f", $consumptionToday+$consumptionDiff));
  611. }
  612. readingsSingleUpdate($hash, "valvePosition", $pct, 1);
  613. #changes below this line will set lastchangeby
  614. EQ3BT_readingsSingleUpdateIfChanged($hash, "windowOpen", $wndOpen, 1);
  615. EQ3BT_readingsSingleUpdateIfChanged($hash, "ecoMode", $eco, 1);
  616. EQ3BT_readingsSingleUpdateIfChanged($hash, "battery", $batteryStr, 1);
  617. EQ3BT_readingsSingleUpdateIfChanged($hash, "boost", $isBoost, 1);
  618. EQ3BT_readingsSingleUpdateIfChanged($hash, "mode", $modeStr, 1);
  619. EQ3BT_readingsSingleUpdateIfChanged($hash, "desiredTemperature", sprintf("%.1f", $temp), 1);
  620. }
  621. return undef;
  622. }
  623. sub EQ3BT_readingsSingleUpdateIfChanged {
  624. my ($hash, $reading, $value, $setLastChange) = @_;
  625. my $curVal = ReadingsVal($hash->{NAME}, $reading, "");
  626. if($curVal ne $value) {
  627. readingsSingleUpdate($hash, $reading, $value, 1);
  628. if(defined($setLastChange)) {
  629. readingsSingleUpdate($hash, "lastChangeBy", "Thermostat", 1);
  630. }
  631. }
  632. }
  633. sub EQ3BT_killGatttool($) {
  634. }
  635. sub EQ3BT_setDaymode($) {
  636. my ($hash) = @_;
  637. }
  638. sub EQ3BT_setNightmode($) {
  639. my ($hash) = @_;
  640. }
  641. sub EQ3BT_setChildlock($$) {
  642. my ($hash, $desiredState) = @_;
  643. }
  644. sub EQ3BT_setHolidaymode($$) {
  645. my ($hash, $holidayEndTime) = @_;
  646. }
  647. sub EQ3BT_setDatetime($$) {
  648. my ($hash, $currentDatetime) = @_;
  649. }
  650. sub EQ3BT_setWindow($$) {
  651. my ($hash, $desiredState) = @_;
  652. }
  653. sub EQ3BT_setProgram($$) {
  654. my ($hash, $program) = @_;
  655. }
  656. sub EQ3BT_Undef($) {
  657. my ($hash) = @_;
  658. #remove internal timer
  659. RemoveInternalTimer($hash);
  660. return undef;
  661. }
  662. sub EQ3BT_Get($$) {
  663. return undef;
  664. }
  665. 1;
  666. =pod
  667. =item device
  668. =item summary Control EQ3 Bluetooth Smart Radiator Thermostat
  669. =item summary_DE Steuerung des EQ3 Bluetooth Thermostats
  670. =begin html
  671. <a name="EQ3BT"></a>
  672. <h3>EQ3BT</h3>
  673. <ul>
  674. EQ3BT is used to control a EQ3 Bluetooth Smart Radiator Thermostat<br><br>
  675. <b>Note:</b> The bluez package is required to run this module. Please check if gatttool executable is available on your system.
  676. <br>
  677. <br>
  678. <a name="EQ3BTdefine" id="EQ3BTdefine"></a>
  679. <b>Define</b>
  680. <ul>
  681. <code>define &lt;name&gt; EQ3BT &lt;mac address&gt;</code><br>
  682. <br>
  683. Example:
  684. <ul>
  685. <code>define livingroom.thermostat EQ3BT 00:33:44:33:22:11</code><br>
  686. </ul>
  687. </ul>
  688. <br>
  689. <a name="EQ3BTset" id="EQ3BTset"></a>
  690. <b>Set</b>
  691. <ul>
  692. <code>set &lt;name&gt; &lt;command&gt; [&lt;parameter&gt;]</code><br>
  693. The following commands are defined:<br><br>
  694. <ul>
  695. <li><code><b>desiredTemperature</b> [4.5...29.5]</code> &nbsp;&nbsp;-&nbsp;&nbsp; set the temperature</li>
  696. <li><code><b>boost</b> on/off</code> &nbsp;&nbsp;-&nbsp;&nbsp; activate boost command</li>
  697. <li><code><b>mode</b> manual/automatic</code> &nbsp;&nbsp;-&nbsp;&nbsp; set manual/automatic mode</li>
  698. <li><code><b>updateStatus</b></code> &nbsp;&nbsp;-&nbsp;&nbsp; read current thermostat state and update readings</li>
  699. <li><code><b>eco</b> </code> &nbsp;&nbsp;-&nbsp;&nbsp; set eco temperature</li>
  700. <li><code><b>comfort</b> </code> &nbsp;&nbsp;-&nbsp;&nbsp; set comfort temperature</li>
  701. </ul>
  702. <br>
  703. </ul>
  704. <a name="EQ3BTget" id="EQ3BTget"></a>
  705. <b>Get</b>
  706. <ul>
  707. <code>n/a</code>
  708. </ul>
  709. <br>
  710. <a name="EQ3BTattr" id="EQ3BTattr"></a>
  711. <b>attr</b>
  712. <ul>
  713. <li>sshHost - FQD-Name or IP of ssh remote system / you must configure your ssh system for certificate authentication. For better handling you can config ssh Client with .ssh/config file</li>
  714. </ul>
  715. <br>
  716. </ul>
  717. =end html
  718. =cut