54
54
CONF_ENCRYPTORS ,
55
55
CONF_REPORT_UNKNOWN ,
56
56
CONF_WHITELIST ,
57
+ CONF_SENSOR_NAMES ,
57
58
CONF_TMIN ,
58
59
CONF_TMAX ,
59
60
CONF_HMIN ,
70
71
MAC_REGEX = "(?i)^(?:[0-9A-F]{2}[:]){5}(?:[0-9A-F]{2})$"
71
72
AES128KEY_REGEX = "(?i)^[A-F0-9]{32}$"
72
73
74
+ SENSOR_NAMES_LIST_SCHEMA = vol .Schema (
75
+ {
76
+ cv .matches_regex (MAC_REGEX ): cv .string
77
+ }
78
+ )
79
+
73
80
ENCRYPTORS_LIST_SCHEMA = vol .Schema (
74
81
{
75
82
cv .matches_regex (MAC_REGEX ): cv .matches_regex (AES128KEY_REGEX )
93
100
vol .Optional (
94
101
CONF_WHITELIST , default = DEFAULT_WHITELIST
95
102
): vol .Any (vol .All (cv .ensure_list , [cv .matches_regex (MAC_REGEX )]), cv .boolean ),
103
+ vol .Optional (CONF_SENSOR_NAMES , default = {}): SENSOR_NAMES_LIST_SCHEMA ,
96
104
}
97
105
)
98
106
@@ -363,6 +371,16 @@ def parse_raw_message(data, aeskeyslist, whitelist, report_unknown=False):
363
371
return result
364
372
365
373
374
+ def sensor_name (config , mac ):
375
+ """Set sensor name."""
376
+ fmac = ':' .join (mac [i :i + 2 ] for i in range (0 , len (mac ), 2 ))
377
+ if fmac in config [CONF_SENSOR_NAMES ]:
378
+ custom_name = config [CONF_SENSOR_NAMES ].get (fmac )
379
+ _LOGGER .debug ("Name of sensor with mac adress %s is set to: %s" , fmac , custom_name )
380
+ return custom_name
381
+ return mac
382
+
383
+
366
384
class BLEScanner :
367
385
"""BLE scanner."""
368
386
@@ -450,11 +468,15 @@ def lpacket(mac, packet=None):
450
468
if config [CONF_WHITELIST ] is True :
451
469
for mac in config [CONF_ENCRYPTORS ]:
452
470
whitelist .append (mac )
471
+ for mac in config [CONF_SENSOR_NAMES ]:
472
+ whitelist .append (mac )
453
473
if isinstance (config [CONF_WHITELIST ], list ):
454
474
for mac in config [CONF_WHITELIST ]:
455
475
whitelist .append (mac )
456
476
for mac in config [CONF_ENCRYPTORS ]:
457
477
whitelist .append (mac )
478
+ for mac in config [CONF_SENSOR_NAMES ]:
479
+ whitelist .append (mac )
458
480
for i , mac in enumerate (whitelist ):
459
481
whitelist [i ] = bytes .fromhex (reverse_mac (mac .replace (":" , "" )).lower ())
460
482
_LOGGER .debug ("%s whitelist item(s) loaded." , len (whitelist ))
@@ -625,31 +647,31 @@ def discover_ble_devices(config, aeskeyslist, whitelist):
625
647
else :
626
648
sensors = []
627
649
if t_i != 9 :
628
- sensors .insert (t_i , TemperatureSensor (mac ))
650
+ sensors .insert (t_i , TemperatureSensor (config , mac ))
629
651
if h_i != 9 :
630
- sensors .insert (h_i , HumiditySensor (mac ))
652
+ sensors .insert (h_i , HumiditySensor (config , mac ))
631
653
if m_i != 9 :
632
- sensors .insert (m_i , MoistureSensor (mac ))
654
+ sensors .insert (m_i , MoistureSensor (config , mac ))
633
655
if c_i != 9 :
634
- sensors .insert (c_i , ConductivitySensor (mac ))
656
+ sensors .insert (c_i , ConductivitySensor (config , mac ))
635
657
if i_i != 9 :
636
- sensors .insert (i_i , IlluminanceSensor (mac ))
658
+ sensors .insert (i_i , IlluminanceSensor (config , mac ))
637
659
if f_i != 9 :
638
- sensors .insert (f_i , FormaldehydeSensor (mac ))
660
+ sensors .insert (f_i , FormaldehydeSensor (confing , mac ))
639
661
if cn_i != 9 :
640
- sensors .insert (cn_i , ConsumableSensor (mac ))
662
+ sensors .insert (cn_i , ConsumableSensor (config , mac ))
641
663
try :
642
664
setattr (sensors [cn_i ], "_cn_name" , CN_NAME_DICT [stype [mac ]])
643
665
except KeyError :
644
666
pass
645
667
if sw_i != 9 :
646
- sensors .insert (sw_i , SwitchBinarySensor (mac ))
668
+ sensors .insert (sw_i , SwitchBinarySensor (config , mac ))
647
669
try :
648
670
setattr (sensors [sw_i ], "_swclass" , SW_CLASS_DICT [stype [mac ]])
649
671
except KeyError :
650
672
pass
651
673
if config [CONF_BATT_ENTITIES ] and (b_i != 9 ):
652
- sensors .insert (b_i , BatterySensor (mac ))
674
+ sensors .insert (b_i , BatterySensor (config , mac ))
653
675
sensors_by_mac [mac ] = sensors
654
676
add_entities (sensors )
655
677
# append joint attributes
@@ -661,6 +683,9 @@ def discover_ble_devices(config, aeskeyslist, whitelist):
661
683
sts .mean (rssi [mac ])
662
684
)
663
685
getattr (sensor , "_device_state_attributes" )["sensor type" ] = stype [mac ]
686
+ getattr (sensor , "_device_state_attributes" )["mac address" ] = (
687
+ ':' .join (mac [i :i + 2 ] for i in range (0 , len (mac ), 2 ))
688
+ )
664
689
if not isinstance (sensor , BatterySensor ) and mac in batt :
665
690
getattr (sensor , "_device_state_attributes" )[
666
691
ATTR_BATTERY_LEVEL
@@ -791,18 +816,18 @@ def update_ble(now):
791
816
792
817
class TemperatureSensor (Entity ):
793
818
"""Representation of a sensor."""
794
-
795
- def __init__ (self , mac ):
796
- """Initialize the sensor."""
819
+ def __init__ (self , config , mac ):
820
+ "Initialize the sensor." ""
797
821
self ._state = None
798
822
self ._battery = None
799
- self ._unique_id = "t_" + mac
823
+ self ._sensor_name = sensor_name (config , mac )
824
+ self ._unique_id = "t_" + sensor_name (config , mac )
800
825
self ._device_state_attributes = {}
801
826
802
827
@property
803
828
def name (self ):
804
829
"""Return the name of the sensor."""
805
- return "mi {}" .format (self ._unique_id )
830
+ return "mi temperature {}" .format (self ._sensor_name )
806
831
807
832
@property
808
833
def state (self ):
@@ -843,17 +868,18 @@ def force_update(self):
843
868
class HumiditySensor (Entity ):
844
869
"""Representation of a Sensor."""
845
870
846
- def __init__ (self , mac ):
871
+ def __init__ (self , config , mac ):
847
872
"""Initialize the sensor."""
848
873
self ._state = None
849
874
self ._battery = None
850
- self ._unique_id = "h_" + mac
875
+ self ._sensor_name = sensor_name (config , mac )
876
+ self ._unique_id = "h_" + sensor_name (config , mac )
851
877
self ._device_state_attributes = {}
852
878
853
879
@property
854
880
def name (self ):
855
881
"""Return the name of the sensor."""
856
- return "mi {}" .format (self ._unique_id )
882
+ return "mi humidity {}" .format (self ._sensor_name )
857
883
858
884
@property
859
885
def state (self ):
@@ -894,17 +920,18 @@ def force_update(self):
894
920
class MoistureSensor (Entity ):
895
921
"""Representation of a Sensor."""
896
922
897
- def __init__ (self , mac ):
923
+ def __init__ (self , config , mac ):
898
924
"""Initialize the sensor."""
899
925
self ._state = None
900
926
self ._battery = None
901
- self ._unique_id = "m_" + mac
927
+ self ._sensor_name = sensor_name (config , mac )
928
+ self ._unique_id = "m_" + sensor_name (config , mac )
902
929
self ._device_state_attributes = {}
903
930
904
931
@property
905
932
def name (self ):
906
933
"""Return the name of the sensor."""
907
- return "mi {}" .format (self ._unique_id )
934
+ return "mi moisture {}" .format (self ._sensor_name )
908
935
909
936
@property
910
937
def state (self ):
@@ -945,17 +972,18 @@ def force_update(self):
945
972
class ConductivitySensor (Entity ):
946
973
"""Representation of a Sensor."""
947
974
948
- def __init__ (self , mac ):
975
+ def __init__ (self , config , mac ):
949
976
"""Initialize the sensor."""
950
977
self ._state = None
951
978
self ._battery = None
952
- self ._unique_id = "c_" + mac
979
+ self ._sensor_name = sensor_name (config , mac )
980
+ self ._unique_id = "c_" + sensor_name (config , mac )
953
981
self ._device_state_attributes = {}
954
982
955
983
@property
956
984
def name (self ):
957
985
"""Return the name of the sensor."""
958
- return "mi {}" .format (self ._unique_id )
986
+ return "mi conductivity {}" .format (self ._sensor_name )
959
987
960
988
@property
961
989
def state (self ):
@@ -996,17 +1024,18 @@ def force_update(self):
996
1024
class IlluminanceSensor (Entity ):
997
1025
"""Representation of a Sensor."""
998
1026
999
- def __init__ (self , mac ):
1027
+ def __init__ (self , config , mac ):
1000
1028
"""Initialize the sensor."""
1001
1029
self ._state = None
1002
1030
self ._battery = None
1003
- self ._unique_id = "l_" + mac
1031
+ self ._sensor_name = sensor_name (config , mac )
1032
+ self ._unique_id = "l_" + sensor_name (config , mac )
1004
1033
self ._device_state_attributes = {}
1005
1034
1006
1035
@property
1007
1036
def name (self ):
1008
1037
"""Return the name of the sensor."""
1009
- return "mi {}" .format (self ._unique_id )
1038
+ return "mi llluminance {}" .format (self ._sensor_name )
1010
1039
1011
1040
@property
1012
1041
def state (self ):
@@ -1046,17 +1075,18 @@ def force_update(self):
1046
1075
class FormaldehydeSensor (Entity ):
1047
1076
"""Representation of a Sensor."""
1048
1077
1049
- def __init__ (self , mac ):
1078
+ def __init__ (self , config , mac ):
1050
1079
"""Initialize the sensor."""
1051
1080
self ._state = None
1052
1081
self ._battery = None
1053
- self ._unique_id = "f_" + mac
1082
+ self ._sensor_name = sensor_name (config , mac )
1083
+ self ._unique_id = "f_" + sensor_name (config , mac )
1054
1084
self ._device_state_attributes = {}
1055
1085
1056
1086
@property
1057
1087
def name (self ):
1058
1088
"""Return the name of the sensor."""
1059
- return "mi {}" .format (self ._unique_id )
1089
+ return "mi formaldehyde {}" .format (self ._sensor_name )
1060
1090
1061
1091
@property
1062
1092
def state (self ):
@@ -1096,16 +1126,17 @@ def force_update(self):
1096
1126
class BatterySensor (Entity ):
1097
1127
"""Representation of a Sensor."""
1098
1128
1099
- def __init__ (self , mac ):
1129
+ def __init__ (self , config , mac ):
1100
1130
"""Initialize the sensor."""
1101
1131
self ._state = None
1102
- self ._unique_id = "batt_" + mac
1132
+ self ._sensor_name = sensor_name (config , mac )
1133
+ self ._unique_id = "batt_" + sensor_name (config , mac )
1103
1134
self ._device_state_attributes = {}
1104
1135
1105
1136
@property
1106
1137
def name (self ):
1107
1138
"""Return the name of the sensor."""
1108
- return "mi {}" .format (self ._unique_id )
1139
+ return "mi battery {}" .format (self ._sensor_name )
1109
1140
1110
1141
@property
1111
1142
def state (self ):
@@ -1145,18 +1176,18 @@ def force_update(self):
1145
1176
class ConsumableSensor (Entity ):
1146
1177
"""Representation of a Sensor."""
1147
1178
1148
- def __init__ (self , mac ):
1179
+ def __init__ (self , config , mac ):
1149
1180
"""Initialize the sensor."""
1150
1181
self ._state = None
1151
1182
self ._battery = None
1152
- self ._cn_name = "cn_"
1153
- self ._nmac = mac
1183
+ self ._sensor_name = sensor_name ( config , mac )
1184
+ self ._unique_id = "cn_" + sensor_name ( config , mac )
1154
1185
self ._device_state_attributes = {}
1155
1186
1156
1187
@property
1157
1188
def name (self ):
1158
1189
"""Return the name of the sensor."""
1159
- return "mi {}" .format (self ._cn_name + self . _nmac )
1190
+ return "mi consumable {}" .format (self ._sensor_name )
1160
1191
1161
1192
@property
1162
1193
def state (self ):
@@ -1186,7 +1217,7 @@ def device_state_attributes(self):
1186
1217
@property
1187
1218
def unique_id (self ) -> str :
1188
1219
"""Return a unique ID."""
1189
- return self ._cn_name + self . _nmac
1220
+ return self ._unique_id
1190
1221
1191
1222
@property
1192
1223
def force_update (self ):
@@ -1196,12 +1227,13 @@ def force_update(self):
1196
1227
class SwitchBinarySensor (BinarySensorEntity ):
1197
1228
"""Representation of a Sensor."""
1198
1229
1199
- def __init__ (self , mac ):
1230
+ def __init__ (self , config , mac ):
1200
1231
"""Initialize the sensor."""
1201
1232
self ._state = None
1202
1233
self ._swclass = None
1203
1234
self ._battery = None
1204
- self ._unique_id = "sw_" + mac
1235
+ self ._sensor_name = sensor_name (config , mac )
1236
+ self ._unique_id = "sw_" + sensor_name (config , mac )
1205
1237
self ._device_state_attributes = {}
1206
1238
1207
1239
@property
@@ -1212,7 +1244,7 @@ def is_on(self):
1212
1244
@property
1213
1245
def name (self ):
1214
1246
"""Return the name of the sensor."""
1215
- return "mi {}" .format (self ._unique_id )
1247
+ return "mi switch {}" .format (self ._sensor_name )
1216
1248
1217
1249
@property
1218
1250
def state (self ):
0 commit comments