Skip to content

Commit

Permalink
Handle exceptions for binary sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 committed Jan 10, 2024
1 parent fce7f95 commit 3c7969f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
28 changes: 17 additions & 11 deletions custom_components/ble_monitor/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ async def async_add_binary_sensor(key, device_model, firmware, auto_sensors, man
if key not in sensors_by_key:
sensors_by_key[key] = {}
if measurement not in sensors_by_key[key]:
entity_description = [item for item in BINARY_SENSOR_TYPES if item.key is measurement][0]
sensors[measurement] = globals()[entity_description.sensor_class](
self.config, key, device_model, firmware, entity_description, manufacturer
)
self.add_entities([sensors[measurement]])
sensors_by_key[key].update(sensors)
try:
entity_description = [item for item in BINARY_SENSOR_TYPES if item.key is measurement][0]
sensors[measurement] = globals()[entity_description.sensor_class](
self.config, key, device_model, firmware, entity_description, manufacturer
)
self.add_entities([sensors[measurement]])
sensors_by_key[key].update(sensors)
except IndexError:
_LOGGER.error("Error adding measurement %s", measurement)
else:
sensors = sensors_by_key[key]
else:
Expand All @@ -102,11 +105,14 @@ async def async_add_binary_sensor(key, device_model, firmware, auto_sensors, man
sensors = {}
sensors_by_key[key] = {}
for measurement in device_sensors:
entity_description = [item for item in BINARY_SENSOR_TYPES if item.key is measurement][0]
sensors[measurement] = globals()[entity_description.sensor_class](
self.config, key, device_model, firmware, entity_description, manufacturer
)
self.add_entities([sensors[measurement]])
try:
entity_description = [item for item in BINARY_SENSOR_TYPES if item.key is measurement][0]
sensors[measurement] = globals()[entity_description.sensor_class](
self.config, key, device_model, firmware, entity_description, manufacturer
)
self.add_entities([sensors[measurement]])
except IndexError:
_LOGGER.error("Error adding measurement %s", measurement)
sensors_by_key[key] = sensors
else:
sensors = sensors_by_key[key]
Expand Down
6 changes: 2 additions & 4 deletions custom_components/ble_monitor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,6 @@ class BLEMonitorBinarySensorEntityDescription(
'YM-K1501' : [["rssi"], ["temperature"], ["switch"]],
'YM-K1501EU' : [["rssi"], ["temperature"], ["switch"]],
'V-SK152' : [["rssi"], ["temperature"], ["switch"]],
'SJWS01LM' : [["battery", "rssi"], ["button"], ["moisture detected"]],
'MJYD02YL' : [["battery", "rssi"], [], ["light", "motion"]],
'MUE4094RT' : [["rssi"], [], ["motion"]],
'RTCGQ02LM' : [["battery", "rssi"], ["button"], ["light", "motion"]],
Expand Down Expand Up @@ -1667,7 +1666,6 @@ class BLEMonitorBinarySensorEntityDescription(
'MHO-C401' : [["temperature", "humidity", "battery", "voltage", "rssi"], [], []],
'MHO-C303' : [["temperature", "humidity", "battery", "rssi"], [], []],
'JQJCY01YM' : [["temperature", "humidity", "battery", "formaldehyde", "rssi"], [], []],
'JTYJGD03MI' : [["rssi"], ["button", "battery"], ["smoke detector"]],
'K9B-1BTN' : [["rssi"], ["one btn switch"], []],
'K9B-2BTN' : [["rssi"], ["two btn switch left", "two btn switch right"], []],
'K9B-3BTN' : [["rssi"], ["three btn switch left", "three btn switch middle", "three btn switch right"], []],
Expand Down Expand Up @@ -1772,7 +1770,6 @@ class BLEMonitorBinarySensorEntityDescription(
'YM-K1501' : 'Xiaomi',
'YM-K1501EU' : 'Xiaomi',
'V-SK152' : 'Viomi',
'SJWS01LM' : 'Xiaomi',
'MJYD02YL' : 'Xiaomi',
'MUE4094RT' : 'Xiaomi',
'RTCGQ02LM' : 'Xiaomi',
Expand Down Expand Up @@ -1801,7 +1798,6 @@ class BLEMonitorBinarySensorEntityDescription(
'MHO-C401' : 'Miaomiaoce',
'MHO-C303' : 'Miaomiaoce',
'JQJCY01YM' : 'Honeywell',
'JTYJGD03MI' : 'Honeywell',
'YLAI003' : 'Yeelight',
'YLYK01YL' : 'Yeelight',
'YLYK01YL-FANCL' : 'Yeelight',
Expand Down Expand Up @@ -1927,6 +1923,7 @@ class BLEMonitorBinarySensorEntityDescription(
'MI401' : 'Grundfos',
'HHCCJCY10' : 'HHCC',
'HolyIOT BLE tracker' : 'HolyIOT',
'JTYJGD03MI' : 'Honeywell',
'Supramatic E4 BS' : 'Hörmann',
'IBS-TH' : 'Inkbird',
'IBS-TH2/P01B' : 'Inkbird',
Expand All @@ -1952,6 +1949,7 @@ class BLEMonitorBinarySensorEntityDescription(
'Tilt Yellow' : 'Tilt',
'Tilt Pink' : 'Tilt',
'MMC-W505' : 'Xiaomi',
'SJWS01LM' : 'Xiaomi',
}


Expand Down

0 comments on commit 3c7969f

Please sign in to comment.