-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
blerry.be
1082 lines (975 loc) · 30.8 KB
/
blerry.be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#######################################################################
# Blerry = BLE with Berry for Tasmota ESP32
#
# use : `ON System#Boot DO br load('blerry.be') ENDON`
#
# Provides MQTT Discovery and Reporting for BLE Devices
#######################################################################
var blerry_version = 'v0.2.4-dev'
# TODO
# Resource Optimization
# Switchbot Bot Support
# Update Actions to support mi32 BLE driver
# Detect mi32 vs ble32 driver earlier (test disjoint commands)
#######################################################################
# Module Imports
#######################################################################
import math
import json
import path
import string
import persist
if path.exists('blerry_setup.be')
path.remove('blerry_setup.be')
end
if path.exists('blerry_setup.bec')
path.remove('blerry_setup.bec')
end
#######################################################################
# Helpers
#######################################################################
class blerry_helpers
static def round(x, p)
return math.ceil(math.pow(10.0, p)*x)/math.pow(10.0, p)
end
static def get_dewpoint(t, h) # temp, humidity
var gamma = math.log(h / 100.0) + 17.62 * t / (243.5 + t)
return (243.5 * gamma / (17.62 - gamma))
end
static def string_replace(x, y, r)
var z = x[0..]
var n = size(y)
var m = size(r)
var k = 0
while k < size(z)
var j = string.find(z, y, k)
if j < 0
break
end
if j > 0
z = z[0..j-1] + r + z[j+n..]
else
z = r + z[j+n..]
end
k = j+m
end
return z
end
static def bitval(x, i)
return (x & (1 << i)) >> i
end
static def read_config()
var config
blerry_helpers.download_config()
if path.exists("blerry_config.json")
var f = open("blerry_config.json", "r")
config = json.load(f.read())
f.close()
else
config = {'devices':{}}
blerry_helpers.write_config(config)
end
return config
end
static def write_config(config)
var f = open("blerry_config.json", "w")
f.write(json.dump(config))
f.close()
end
static def write_url(url)
persist.blerry_configurl = url
persist.save()
end
static def cmd_set_device(cmd, idx, payload, payload_json)
var config = blerry_helpers.read_config()
var new_dev = json.load(payload)
for m:new_dev.keys()
config['devices'][m] = new_dev[m]
end
blerry_helpers.write_config(config)
tasmota.resp_cmnd_done()
end
static def cmd_get_device(cmd, idx, payload, payload_json)
var config = blerry_helpers.read_config()
tasmota.resp_cmnd(json.dump({string.toupper(payload): config['devices'][string.toupper(payload)]}))
end
static def cmd_del_device(cmd, idx, payload, payload_json)
var config = blerry_helpers.read_config()
var new_dev = json.load(payload)
config['devices'].remove(payload)
blerry_helpers.write_config(config)
tasmota.resp_cmnd_done()
end
static def cmd_set_config(cmd, idx, payload, payload_json)
var config = json.load(payload)
blerry_helpers.write_config(config)
tasmota.resp_cmnd_done()
end
static def cmd_get_config(cmd, idx, payload, payload_json)
var config = blerry_helpers.read_config()
tasmota.resp_cmnd(json.dump(config))
end
static def cmd_del_config(cmd, idx, payload, payload_json)
blerry_helpers.write_config({'devices':{}})
tasmota.resp_cmnd_done()
end
static def cmd_url_config(cmd, idx, url)
if string.find(url,'http',0,4)
print('BLY: URL does not start with http(s)', url)
return false
end
blerry_helpers.write_url(url)
tasmota.resp_cmnd_done()
end
static def download_file(file_name, url)
var cl = webclient()
cl.begin(url)
var r = cl.GET()
if r != 200
print('BLY: Could not download file:', file_name, 'from:', url)
return false
end
var s = cl.get_string()
cl.close()
var f = open(file_name, 'w')
f.write(s)
f.close()
return true
end
static def download_driver(driver_fname)
var url = 'https://raw.githubusercontent.com/tony-fav/tasmota-blerry/dev/blerry/drivers/' + driver_fname
return blerry_helpers.download_file(driver_fname, url)
end
static def download_config()
var url
if persist.has("blerry_configurl")
url = persist.blerry_configurl
else
return
end
var cl = webclient()
cl.begin(url)
var r = cl.GET()
if r != 200
print('BLY: Could not download config from:', url)
return false
end
var s = cl.get_string()
cl.close()
var val = json.load(s)
if isinstance(val, map)
var f = open("blerry_config.json", 'w')
f.write(s)
f.close()
return true
else
print('BLY: Data from:', url, 'is not valid json:', s)
return false
end
return true
end
static def ensure_driver_exists(driver_fname)
if !(path.exists(driver_fname) || path.exists(driver_fname + 'c'))
if blerry_helpers.download_driver(driver_fname)
print('BLY: Downloaded driver successfully:', driver_fname)
return true
else
print('BLY: Could not download driver:', driver_fname)
return false
end
end
end
end
#######################################################################
# Dev helpers bc I overuse dir when coding Python
#######################################################################
import introspect
var dir = introspect.members
#######################################################################
# Required Globals for Driver Loading
#######################################################################
var blerry_handle
var blerry_active
var blerry_op_handle
var blerry_op_cmd
#######################################################################
# BLE_AdvElement
#######################################################################
class BLE_AdvElement
var length
var type
var data
def init(l, t, d)
self.length = l
self.type = t
self.data = d
end
end
#######################################################################
# BLE_AdvData
#######################################################################
class BLE_AdvData
var elements
def init(p)
var i = 0
var adv_len = 0
var adv_data = bytes('')
var adv_type = 0
self.elements = []
while i < size(p)
adv_len = p.get(i,1)
adv_type = p.get(i+1,1)
adv_data = p[i+2..i+adv_len]
self.elements.push(BLE_AdvElement(adv_len, adv_type, adv_data))
i = i + adv_len + 1
end
end
def get_elements_by_type(t)
var out = []
for e:self.elements
if e.type == t
out.push(e)
end
end
return out
end
def get_elements_by_type_length(t, l)
var out = []
for e:self.elements
if e.type == t && e.length == l
out.push(e)
end
end
return out
end
def get_elements_by_type_data(t, d, di)
var out = []
for e:self.elements
if e.type == t && e.data[di..di+size(d)-1] == d
out.push(e)
end
end
return out
end
def get_elements_by_type_length_data(t, l, d, di)
var out = []
for e:self.elements
if e.type == t && e.length == l && e.data[di..di+size(d)-1] == d
out.push(e)
end
end
return out
end
end
#######################################################################
# Blerry_Attribute
#######################################################################
class Blerry_Attribute
var name
var value
def init(name, value)
self.name = name
self.value = value
end
end
#######################################################################
# Blerry_Sensor
#######################################################################
class Blerry_Sensor
var name
var dev_cla
var unit_of_meas
var value
def init(name, value, dev_cla, unit_of_meas)
self.name = name
self.value = value
self.dev_cla = dev_cla
self.unit_of_meas = unit_of_meas
end
end
#######################################################################
# Blerry_Binary_Sensor
#######################################################################
class Blerry_Binary_Sensor
var name
var dev_cla
var unit_of_meas
var value
def init(name, value, dev_cla)
self.name = name
if value
self.value = 'ON'
else
self.value = 'OFF'
end
self.dev_cla = dev_cla
end
def compare(in_value) # todo: improve
if in_value
if self.value == 'ON'
return true
else
return false
end
else
if self.value == 'OFF'
return true
else
return false
end
end
end
end
#######################################################################
# Blerry_Action
#######################################################################
class Blerry_Action
var name
var command
def init(name, command)
self.name = name
self.command = command
end
def execute()
return tasmota.cmd(self.command)
end
end
#######################################################################
# Blerry_Device
#######################################################################
class Blerry_Device
var name
var mac
var config
var b
var alias
var handle
var active
var op_handle
var op_cmd
var attributes
var sensors
var sensors_to_discover
var binary_sensors
var binary_sensors_to_discover
var actions
var actions_to_discover
var topic
var publish_available
var next_forced_publish
def init(mac, config, blerry_inst)
self.name = config.find('name', config['alias'])
self.mac = mac
self.config = config
self.b = blerry_inst
self.alias = config['alias']
self.load_driver()
self.attributes = {}
self.sensors = {}
self.sensors_to_discover = []
self.binary_sensors = {}
self.binary_sensors_to_discover = []
self.actions = {}
self.actions_to_discover = []
# static attributes
self.add_attribute('MAC', self.mac)
self.add_attribute('Name', self.name)
self.add_attribute('Alias', self.alias)
self.add_attribute('Model', self.config['model'])
# publication related
self.next_forced_publish = tasmota.millis(300000)
self.publish_available = false
self.topic = self.config['base_topic']
if self.topic[-1] != '/'
self.topic = self.topic + '/'
end
self.topic = self.topic + self.alias
end
def load_driver()
var model_drivers =
{
'dev' : 'blerry_driver_dev.be',
'BTHome' : 'blerry_driver_BTHome.be',
'ATCpvvx' : 'blerry_driver_ATCpvvx.be',
'ATC' : 'blerry_driver_ATCpvvx.be',
'pvvx' : 'blerry_driver_ATCpvvx.be',
'GVH5074' : 'blerry_driver_GVH5074.be',
'GVH5075' : 'blerry_driver_GVH5075.be',
'GVH5072' : 'blerry_driver_GVH5075.be',
'GVH5101' : 'blerry_driver_GVH5075.be',
'GVH5102' : 'blerry_driver_GVH5075.be',
'IBSTH1' : 'blerry_driver_IBSTH2.be',
'IBSTH2' : 'blerry_driver_IBSTH2.be',
'MiScale2' : 'blerry_driver_MiScale2.be',
'EufyC1' : 'blerry_driver_EufyC1.be',
'Xiaomi' : 'blerry_driver_Xiaomi.be',
'ATCmi' : 'blerry_driver_Xiaomi.be',
'Xiaomi_LYWSDCGQ' : 'blerry_driver_Xiaomi.be',
'ThermoPro_TP59' : 'blerry_driver_ThermoPro_TP59.be',
'WoContact' : 'blerry_driver_WoContact.be',
'WoHand' : 'blerry_driver_WoHand.be',
'WoPresence' : 'blerry_driver_WoPresence.be',
'WoSensorTH' : 'blerry_driver_WoSensorTH.be',
'WoSensorTHO' : 'blerry_driver_WoSensorTHO.be',
'GVH5182' : 'blerry_driver_GVH5184.be',
'GVH5183' : 'blerry_driver_GVH5183.be',
'GVH5184' : 'blerry_driver_GVH5184.be',
'iBBQ4' : 'blerry_driver_iBBQ4.be',
'iBBQ6' : 'blerry_driver_iBBQ6.be',
'WP6003' : 'blerry_driver_WP6003.be',
'TP630' : 'blerry_driver_TP630.be',
}
var fn = model_drivers[self.config['model']]
blerry_handle = def () return nil end
blerry_active = false
blerry_op_handle = def () return nil end
blerry_op_cmd = def () return nil end
blerry_helpers.ensure_driver_exists(fn)
load(fn)
self.handle = blerry_handle
self.active = blerry_active
self.op_handle = blerry_op_handle
self.op_cmd = blerry_op_cmd
end
def add_attribute(name, value)
self.attributes[name] = Blerry_Attribute(name, value)
end
def get_attribute(name)
if self.attributes.contains(name)
return self.attributes[name]
else
return nil
end
end
def add_action_force(name, command)
self.actions[name] = Blerry_Action(name, command)
if self.actions_to_discover.find(name) == nil
self.actions_to_discover.push(name)
end
end
def add_action(name, command)
if !self.actions.contains(name)
self.add_action_force(name, command)
end
end
def execute_action(name)
if self.actions.contains(name)
self.actions[name].execute()
end
end
def add_sensor_no_pub(name, value, dev_cla, unit_of_meas)
if !self.sensors.contains(name)
self.sensors_to_discover.push(name)
end
self.sensors[name] = Blerry_Sensor(name, value, dev_cla, unit_of_meas)
end
def add_sensor(name, value, dev_cla, unit_of_meas)
if !self.sensors.contains(name)
self.sensors[name] = Blerry_Sensor(name, value, dev_cla, unit_of_meas)
self.sensors_to_discover.push(name)
self.publish_available = true
elif self.sensors[name].value != value
self.sensors[name] = Blerry_Sensor(name, value, dev_cla, unit_of_meas)
self.publish_available = true
end
end
def add_sensor_in_range(name, value, dev_cla, unit_of_meas, low, high)
if ((low == nil) || (value > low)) && ((high == nil) || (value < high))
self.add_sensor(name, value, dev_cla, unit_of_meas)
end
end
def add_binary_sensor_no_pub(name, value, dev_cla)
if !self.binary_sensors.contains(name)
self.binary_sensors_to_discover.push(name)
end
self.binary_sensors[name] = Blerry_Binary_Sensor(name, value, dev_cla)
end
def add_binary_sensor(name, value, dev_cla)
if !self.binary_sensors.contains(name)
self.binary_sensors[name] = Blerry_Binary_Sensor(name, value, dev_cla)
self.binary_sensors_to_discover.push(name)
self.publish_available = true
elif !self.binary_sensors[name].compare(value)
self.binary_sensors[name] = Blerry_Binary_Sensor(name, value, dev_cla)
self.publish_available = true
end
end
def publish()
if self.publish_available
var msg = {}
for a:self.attributes
msg[a.name] = a.value
end
for s:self.sensors
msg[s.name] = s.value
end
for bs:self.binary_sensors
msg[bs.name] = bs.value
end
if self.config['via_pubs']
msg['Time_via_' + self.b.device_topic] = msg['Time']
msg['RSSI_via_' + self.b.device_topic] = msg['RSSI']
end
# calibration
if self.config.contains('calibration') # if calibration is defined in the config
for k:self.config['calibration'].keys() # loop over sensors being calibrated
if msg.contains(k) # if we have that sensor value to calibrate
if type(msg[k]) == 'real' || type(msg[k]) == 'int'
var c = self.config['calibration'][k] # calibration has to be a list
if isinstance(c, list)
if size(c) == 1 # size 1 = just a delta
msg[k] = c[0] + msg[k]
elif size(c) == 2
msg[k] = c[0] + c[1]*msg[k] # size 2 = delta and slope
end
end
end
end
end
end
# precision
if self.config.contains('precision') # if precision is defined in the config
for k:self.config['precision'].keys() # loop over sensors being rounded
if msg.contains(k) # if we have that sensor value to round
if type(msg[k]) == 'real' || type(msg[k]) == 'int'
msg[k] = blerry_helpers.round(msg[k], self.config['precision'][k])
end
end
end
end
tasmota.publish(self.topic, json.dump(msg), self.config['sensor_retain'])
if self.config['publish_attributes']
for k:msg.keys()
tasmota.publish(self.topic + '/' + k, string.format('%s', msg[k]), self.config['sensor_retain'])
end
end
self.next_forced_publish = tasmota.millis(300000)
self.publish_available = false
return true
end
return false
end
def publish_discovery()
if self.config['discovery']
if self.publish_sensor_discovery()
return true
end
if self.publish_binary_sensor_discovery()
return true
end
if self.publish_action_discovery()
return true
end
end
return false
end
def get_discovery_packet_base() # Make each time so it can be GC'd. Don't save as a member of the class.
var msg = {}
# LWT Part
if self.config['use_lwt']
msg['avty_t'] = self.b.device_tele_topic + '/LWT'
msg['pl_avail'] = 'Online'
msg['pl_not_avail'] = 'Offline'
else
msg['avty'] = []
end
# Device Association Part
msg['dev'] = {}
msg['dev']['ids'] = [('blerry_' + self.alias)]
msg['dev']['name'] = self.name
msg['dev']['mf'] = 'BLErry ' + blerry_version
msg['dev']['mdl'] = self.config['model']
msg['dev']['via_device'] = self.b.hostname
# Topic Part
msg['json_attr_t'] = self.topic
msg['stat_t'] = self.topic
return msg
end
def get_discovery_override(msg, name)
if self.config.contains('discovery_override')
if self.config['discovery_override'].contains(name)
for k:self.config['discovery_override'][name].keys()
msg[k] = self.config['discovery_override'][name][k]
end
end
end
return msg
end
def publish_sensor_discovery()
var topic_fmt = 'homeassistant/sensor/blerry_' + self.alias + '/%s/config'
if size(self.sensors_to_discover)
var s = self.sensors_to_discover[0]
var msg = self.get_discovery_packet_base()
msg['exp_aft'] = 600
msg['name'] = self.sensors[s].name
msg['uniq_id'] = 'blerry_' + self.alias + '_' + self.sensors[s].name
if self.sensors[s].dev_cla
msg['dev_cla'] = self.sensors[s].dev_cla
end
msg['unit_of_meas'] = self.sensors[s].unit_of_meas
msg['val_tpl'] = '{{ value_json.' + self.sensors[s].name + ' }}'
msg = self.get_discovery_override(msg, s)
tasmota.publish(string.format(topic_fmt, s), json.dump(msg), self.config['discovery_retain'])
self.sensors_to_discover = self.sensors_to_discover[1..]
return true
end
return false
end
def publish_binary_sensor_discovery()
var topic_fmt = 'homeassistant/binary_sensor/blerry_' + self.alias + '/%s/config'
if size(self.binary_sensors_to_discover)
var s = self.binary_sensors_to_discover[0]
var msg = self.get_discovery_packet_base()
msg['exp_aft'] = 600
msg['name'] = self.binary_sensors[s].name
msg['uniq_id'] = 'blerry_' + self.alias + '_' + self.binary_sensors[s].name
if self.binary_sensors[s].dev_cla != 'none'
msg['dev_cla'] = self.binary_sensors[s].dev_cla
end
msg['val_tpl'] = '{{ value_json.' + self.binary_sensors[s].name + ' }}'
msg = self.get_discovery_override(msg, s)
tasmota.publish(string.format(topic_fmt, s), json.dump(msg), self.config['discovery_retain'])
self.binary_sensors_to_discover = self.binary_sensors_to_discover[1..]
return true
end
return false
end
def publish_action_discovery()
var topic_fmt = 'homeassistant/button/blerry_' + self.alias + '/%s/config'
if size(self.actions_to_discover)
var a = self.actions_to_discover[0]
var msg = self.get_discovery_packet_base()
msg['name'] = self.actions[a].name
msg['uniq_id'] = 'blerry_' + self.alias + '_' + self.actions[a].name
msg['cmd_t'] = self.b.device_cmnd_topic + '/BlerryAction'
msg['payload_press'] = json.dump({self.mac: self.actions[a].name})
msg = self.get_discovery_override(msg, a)
tasmota.publish(string.format(topic_fmt, a), json.dump(msg), self.config['discovery_retain'])
self.actions_to_discover = self.actions_to_discover[1..]
return true
end
return false
end
end
#######################################################################
# Class for Blerry Instance
#######################################################################
class Blerry
# blerry config
var default_config
var user_config
var device_config
var devices
var details_trigger
# tasmota config
var device_topic
var cmnd_prefix
var stat_prefix
var tele_prefix
var full_topic_f
var hostname
var device_cmnd_topic
var device_tele_topic
var mi32ble
def init()
self.get_tasmota_settings()
self.load_default_config()
self.load_user_config()
self.setup_device_config()
self.setup_devices()
self.setup_packet_rule()
self.mi32ble = false
end
def get_tasmota_settings()
self.device_topic = tasmota.cmd('Status')['Status']['Topic']
self.cmnd_prefix = tasmota.cmd('Prefix1')['Prefix1']
self.stat_prefix = tasmota.cmd('Prefix2')['Prefix2']
self.tele_prefix = tasmota.cmd('Prefix3')['Prefix3']
self.full_topic_f = tasmota.cmd('FullTopic')['FullTopic']
self.hostname = tasmota.cmd('Status 5')['StatusNET']['Hostname']
self.device_cmnd_topic = blerry_helpers.string_replace(blerry_helpers.string_replace(self.full_topic_f, '%prefix%', self.cmnd_prefix), '%topic%', self.device_topic)
if self.device_cmnd_topic[-1] == '/' # allow / at the end but remove it here
self.device_cmnd_topic = self.device_cmnd_topic[0..-2]
end
self.device_tele_topic = blerry_helpers.string_replace(blerry_helpers.string_replace(self.full_topic_f, '%prefix%', self.tele_prefix), '%topic%', self.device_topic)
if self.device_tele_topic[-1] == '/' # allow / at the end but remove it here
self.device_tele_topic = self.device_tele_topic[0..-2]
end
end
# Load user config and default config from JSON files
# Validate JSON with https://jsonformatter.curiousconcept.com/
# Convert back and form to YAML with https://www.json2yaml.com/
def load_user_config() # based on persist module
var f
var val
blerry_helpers.download_config()
if !(path.exists("blerry_config.json"))
blerry_helpers.write_config({'devices':{}})
end
if path.exists("blerry_config.json")
try
f = open("blerry_config.json", "r")
val = json.load(f.read())
f.close()
except .. as e, m
if f != nil f.close() end
raise e, m
end
if isinstance(val, map)
self.user_config = val
else
raise "blerry_error", "failed to load blerry_config.json"
end
else
raise "blerry_error", "no blerry_config.json found"
end
# sanitize mac addresses to be upper case
var new_devices = {}
for k:self.user_config['devices'].keys()
new_devices[string.toupper(k)] = self.user_config['devices'][k]
end
self.user_config['devices'] = new_devices
self.details_trigger = 'DetailsBLE'
if self.user_config.contains('advanced')
if self.user_config['advanced'].contains('old_details')
if self.user_config['advanced']['old_details']
self.details_trigger = 'details'
end
end
end
end
def load_default_config()
self.default_config =
{
'base_topic': 'tele/tasmota_blerry',
'discovery': true,
'discovery_retain': true,
'use_lwt': false,
'via_pubs': false,
'sensor_retain': false,
'publish_attributes': false,
'ignored': false,
'precision':
{
'Temperature': 2,
'DewPoint': 2,
'Humidity': 1,
'Battery': 0
}
}
end
# for each device: default, then device specific, then override, then ignore
def setup_device_config()
self.device_config = {}
for m:self.user_config['devices'].keys()
self.device_config[m] = {}
for k:self.default_config.keys()
self.device_config[m][k] = self.default_config[k]
end
for k:self.user_config['devices'][m].keys()
self.device_config[m][k] = self.user_config['devices'][m][k]
end
if self.user_config.contains('override')
for k:self.user_config['override'].keys()
self.device_config[m][k] = self.user_config['override'][k]
end
end
end
var ignored_devices = []
for m:self.device_config.keys()
if self.device_config[m]['ignored']
ignored_devices.push(m)
end
end
for m:ignored_devices
self.device_config.remove(m)
end
end
def setup_devices()
var active = false
self.devices = {}
for m:self.device_config.keys()
var device = Blerry_Device(m, self.device_config[m], self)
# host based attributes here
self.devices[m] = device
active = active || device.active
tasmota.cmd(string.format('BLEAlias %s=%s', device.mac, device.alias))
end
if active
tasmota.cmd('BLEScan0 1')
end
tasmota.cmd('BLEDetails4')
end
def handle_BLE_packet(value, trigger, msg)
var advert = BLE_AdvData(bytes(value['p']))
try
var device = self.devices[value['mac']]
# device.handle(device, advert)
var handle_f = device.handle
handle_f(device, advert)
device.add_attribute('Time', tasmota.time_str(tasmota.rtc()['local']))
device.add_sensor_no_pub('RSSI', value['RSSI'], 'signal_strength', 'dB')
if tasmota.millis() > device.next_forced_publish
device.publish_available = true
end
except .. as e, m
print('BLY: tried to handle mac =', value['mac'], 'with alias =', value['a'])
raise e, m
end
end
def handle_BLEOp_packet(value, trigger, msg)
try
var device = self.devices[value['MAC']]
var handle_f = device.op_handle
handle_f(device, value) # BLEOp is more complex, we do more in the driver
except .. as e, m
#
end
end
def setup_packet_rule()
# tasmota.add_rule(self.details_trigger, def (value, trigger, msg) self.handle_BLE_packet(value, trigger, msg) end)
tasmota.add_rule(self.details_trigger, / value, trigger, msg -> self.handle_BLE_packet(value, trigger, msg)) # "I prefer a lambda for the closure...." - sfromis
tasmota.add_rule('BLEOperation', / value, trigger, msg -> self.handle_BLEOp_packet(value, trigger, msg))
end
def publish()
for d:self.devices
if d.publish()
return true
end
end
return false
end
def publish_discovery()
for d:self.devices
if d.publish_discovery()
return true
end
end
return false
end
def load_success()
print('BLY: BLErry ' + blerry_version + ' Loaded Successfully')
end
def execute_action(cmd, idx, payload, payload_json)
tasmota.resp_cmnd_done()
for k:payload_json.keys()
if self.devices.contains(k)
self.devices[k].execute_action(payload_json[k])
end
end
end
end
#######################################################################
# Blerry_Driver
# periodic publication of data
# WebUI display of data
# The goal of using a driver for publication is to rate limit a bit
#######################################################################
class Blerry_Driver : Driver
var b
var d_idx
var next_send
var last_sent
var next_poll
var d_idx_poll
def init(blerry_inst)
self.b = blerry_inst
self.d_idx = 0
self.next_send = tasmota.millis(10000)
self.last_sent = ''
self.d_idx_poll = 0
self.next_poll = tasmota.millis(10000)
end
def every_second()
if self.b.publish_discovery()
return true
end
if self.b.publish()
return true
end
if tasmota.millis() > self.next_poll
var d
var i = 0
self.d_idx_poll = (self.d_idx_poll + 1) % size(self.b.devices)
for de:self.b.devices
if i == self.d_idx_poll
d = de
break