10_EQ3BT.pm 29 KB

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