From 5a6096d00e190a51423e1e021cd25c38b065c212 Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Sat, 23 Sep 2023 17:46:55 +0200 Subject: [PATCH] and more binary sensors --- .../ble_monitor/ble_parser/bthome_const.py | 10 +- custom_components/ble_monitor/const.py | 172 ++++++++++-------- .../ble_monitor/test/test_bthome_v2.py | 80 +++++++- 3 files changed, 180 insertions(+), 82 deletions(-) diff --git a/custom_components/ble_monitor/ble_parser/bthome_const.py b/custom_components/ble_monitor/ble_parser/bthome_const.py index 38cc90b27..0342de5ed 100644 --- a/custom_components/ble_monitor/ble_parser/bthome_const.py +++ b/custom_components/ble_monitor/ble_parser/bthome_const.py @@ -126,10 +126,10 @@ class MeasTypes: meas_format="battery", ), 0x16: MeasTypes( - meas_format="battery_charging", + meas_format="battery charging", ), 0x17: MeasTypes( - meas_format="carbon_monoxide", + meas_format="carbon monoxide", ), 0x18: MeasTypes( meas_format="cold", @@ -141,10 +141,10 @@ class MeasTypes: meas_format="door", ), 0x1B: MeasTypes( - meas_format="garage_door", + meas_format="garage door", ), 0x1C: MeasTypes( - meas_format="gas_detected", + meas_format="gas detected", ), 0x1D: MeasTypes( meas_format="heat", @@ -156,7 +156,7 @@ class MeasTypes: meas_format="lock", ), 0x20: MeasTypes( - meas_format="moisture_detected", + meas_format="moisture detected", ), 0x21: MeasTypes( meas_format="motion", diff --git a/custom_components/ble_monitor/const.py b/custom_components/ble_monitor/const.py index da4440b98..b1ba483bd 100755 --- a/custom_components/ble_monitor/const.py +++ b/custom_components/ble_monitor/const.py @@ -161,6 +161,15 @@ class BLEMonitorBinarySensorEntityDescription( device_class=BinarySensorDeviceClass.CONNECTIVITY, force_update=True, ), + BLEMonitorBinarySensorEntityDescription( + key="door", + sensor_class="BaseBinarySensor", + update_behavior="Instantly", + name="ble door", + unique_id="do_", + device_class=BinarySensorDeviceClass.DOOR, + force_update=False, + ), BLEMonitorBinarySensorEntityDescription( key="garage_door", sensor_class="BaseBinarySensor", @@ -188,49 +197,67 @@ class BLEMonitorBinarySensorEntityDescription( device_class=BinarySensorDeviceClass.HEAT, force_update=True, ), + BLEMonitorBinarySensorEntityDescription( + key="light", + sensor_class="BaseBinarySensor", + update_behavior="Instantly", + name="ble light", + unique_id="lt_", + device_class=BinarySensorDeviceClass.LIGHT, + force_update=False, + ), + BLEMonitorBinarySensorEntityDescription( + key="lock", + sensor_class="BaseBinarySensor", + update_behavior="Instantly", + name="ble lock", + unique_id="lock_", + device_class=BinarySensorDeviceClass.LOCK, + force_update=True, + ), BLEMonitorBinarySensorEntityDescription( key="moisture_detected", sensor_class="BaseBinarySensor", update_behavior="Instantly", name="moisture", - unique_id="bmoist_", + unique_id="mo_", device_class=BinarySensorDeviceClass.MOISTURE, force_update=True, ), BLEMonitorBinarySensorEntityDescription( - key="binary", + key="moisture", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble binary", - unique_id="bi_", - device_class=None, - force_update=True, + name="ble moisture", + unique_id="mo_", + device_class=BinarySensorDeviceClass.MOISTURE, + force_update=False, ), BLEMonitorBinarySensorEntityDescription( - key="remote single press", - sensor_class="BaseBinarySensor", + key="motion", + sensor_class="MotionBinarySensor", update_behavior="Instantly", - name="ble remote binary single press", - unique_id="rb_single_press_", - device_class=None, - force_update=True, + name="ble motion", + unique_id="mn_", + device_class=BinarySensorDeviceClass.MOTION, + force_update=False, ), BLEMonitorBinarySensorEntityDescription( - key="remote long press", + key="moving", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble remote binary long press", - unique_id="rb_long_press_", - device_class=None, + name="moving", + unique_id="bmove_", + device_class=BinarySensorDeviceClass.MOVING, force_update=True, ), BLEMonitorBinarySensorEntityDescription( - key="switch", + key="occupancy", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble switch", - unique_id="sw_", - device_class=BinarySensorDeviceClass.POWER, + name="occupancy", + unique_id="bocc_", + device_class=BinarySensorDeviceClass.OCCUPANCY, force_update=True, ), BLEMonitorBinarySensorEntityDescription( @@ -243,77 +270,87 @@ class BLEMonitorBinarySensorEntityDescription( force_update=False, ), BLEMonitorBinarySensorEntityDescription( - key="light", + key="plug", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble light", - unique_id="lt_", - device_class=BinarySensorDeviceClass.LIGHT, + name="ble plug", + unique_id="plug_", + device_class=BinarySensorDeviceClass.PLUG, force_update=False, ), BLEMonitorBinarySensorEntityDescription( - key="moisture", + key="smoke detector", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble moisture", - unique_id="mo_", - device_class=BinarySensorDeviceClass.MOISTURE, + name="ble smoke detector", + unique_id="sd_", + device_class=BinarySensorDeviceClass.SMOKE, force_update=False, ), BLEMonitorBinarySensorEntityDescription( - key="motion", - sensor_class="MotionBinarySensor", + key="vibration", + sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble motion", - unique_id="mn_", - device_class=BinarySensorDeviceClass.MOTION, - force_update=False, + name="ble vibration", + unique_id="vi_", + icon="mdi:vibrate", + device_class=BinarySensorDeviceClass.VIBRATION, + force_update=True, ), BLEMonitorBinarySensorEntityDescription( - key="weight removed", + key="binary", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble weight removed", - icon="mdi:weight", - unique_id="wr_", + name="ble binary", + unique_id="bi_", device_class=None, - force_update=False, + force_update=True, ), BLEMonitorBinarySensorEntityDescription( - key="smoke detector", + key="remote single press", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble smoke detector", - unique_id="sd_", - device_class=BinarySensorDeviceClass.SMOKE, - force_update=False, + name="ble remote binary single press", + unique_id="rb_single_press_", + device_class=None, + force_update=True, ), BLEMonitorBinarySensorEntityDescription( - key="fingerprint", + key="remote long press", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble fingerprint", - icon="mdi:fingerprint", - unique_id="fp_", + name="ble remote binary long press", + unique_id="rb_long_press_", device_class=None, force_update=True, ), BLEMonitorBinarySensorEntityDescription( - key="door", + key="switch", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble door", - unique_id="do_", - device_class=BinarySensorDeviceClass.DOOR, + name="ble switch", + unique_id="sw_", + device_class=BinarySensorDeviceClass.POWER, + force_update=True, + ), + BLEMonitorBinarySensorEntityDescription( + key="weight removed", + sensor_class="BaseBinarySensor", + update_behavior="Instantly", + name="ble weight removed", + icon="mdi:weight", + unique_id="wr_", + device_class=None, force_update=False, ), BLEMonitorBinarySensorEntityDescription( - key="lock", + key="fingerprint", sensor_class="BaseBinarySensor", update_behavior="Instantly", - name="ble lock", - unique_id="lock_", - device_class=BinarySensorDeviceClass.LOCK, + name="ble fingerprint", + icon="mdi:fingerprint", + unique_id="fp_", + device_class=None, force_update=True, ), BLEMonitorBinarySensorEntityDescription( @@ -363,16 +400,6 @@ class BLEMonitorBinarySensorEntityDescription( device_class=None, force_update=False, ), - BLEMonitorBinarySensorEntityDescription( - key="vibration", - sensor_class="BaseBinarySensor", - update_behavior="Instantly", - name="ble vibration", - unique_id="vi_", - icon="mdi:vibrate", - device_class=BinarySensorDeviceClass.VIBRATION, - force_update=True, - ), BLEMonitorBinarySensorEntityDescription( key="dropping", sensor_class="BaseBinarySensor", @@ -1572,22 +1599,25 @@ class BLEMonitorBinarySensorEntityDescription( # Binary Sensors that are automatically added if device is in AUTO_MANUFACTURER_DICT AUTO_BINARY_SENSOR_LIST = [ - "battery_charging", - "carbon_monoxide", + "battery charging", + "carbon monoxide", "cold", "connectivity", "binary", "door", "dropping", - "garage_door", - "gas_detected", + "garage door", + "gas detected", "heat", "impact", "light", "lock", "motion", - "moisture_detected" + "moisture detected" + "moving", + "occupancy", "opening", + "plug", "smoke", "switch", "tilt", diff --git a/custom_components/ble_monitor/test/test_bthome_v2.py b/custom_components/ble_monitor/test/test_bthome_v2.py index 37fe219ff..27f525b44 100644 --- a/custom_components/ble_monitor/test/test_bthome_v2.py +++ b/custom_components/ble_monitor/test/test_bthome_v2.py @@ -319,7 +319,7 @@ def test_bthome_v2_battery_charging(self): assert sensor_msg["mac"] == "5448E68F80A5" assert sensor_msg["packet"] == "no packet id" assert sensor_msg["data"] - assert sensor_msg["battery_charging"] == 1 + assert sensor_msg["battery charging"] == 1 assert sensor_msg["rssi"] == -52 def test_bthome_v2_carbon_monoxide(self): @@ -336,7 +336,7 @@ def test_bthome_v2_carbon_monoxide(self): assert sensor_msg["mac"] == "5448E68F80A5" assert sensor_msg["packet"] == "no packet id" assert sensor_msg["data"] - assert sensor_msg["carbon_monoxide"] == 1 + assert sensor_msg["carbon monoxide"] == 1 assert sensor_msg["rssi"] == -52 def test_bthome_v2_cold(self): @@ -404,7 +404,7 @@ def test_bthome_v2_garage_door(self): assert sensor_msg["mac"] == "5448E68F80A5" assert sensor_msg["packet"] == "no packet id" assert sensor_msg["data"] - assert sensor_msg["garage_door"] == 1 + assert sensor_msg["garage door"] == 1 assert sensor_msg["rssi"] == -52 def test_bthome_v2_gas_detected(self): @@ -421,7 +421,7 @@ def test_bthome_v2_gas_detected(self): assert sensor_msg["mac"] == "5448E68F80A5" assert sensor_msg["packet"] == "no packet id" assert sensor_msg["data"] - assert sensor_msg["gas_detected"] == 0 + assert sensor_msg["gas detected"] == 0 assert sensor_msg["rssi"] == -52 def test_bthome_v2_heat(self): @@ -477,7 +477,75 @@ def test_bthome_v2_lock(self): def test_bthome_v2_moisture_detected(self): """Test BTHome parser for moisture detected measurement""" - data_string = "043E1602010000A5808FE648540A0201060616D2FC401F01CC" + data_string = "043E1602010000A5808FE648540A0201060616D2FC402001CC" + data = bytes(bytearray.fromhex(data_string)) + + # pylint: disable=unused-variable + ble_parser = BleParser() + sensor_msg, tracker_msg = ble_parser.parse_raw_data(data) + + assert sensor_msg["firmware"] == "BTHome V2" + assert sensor_msg["type"] == "BTHome" + assert sensor_msg["mac"] == "5448E68F80A5" + assert sensor_msg["packet"] == "no packet id" + assert sensor_msg["data"] + assert sensor_msg["moisture detected"] == 1 + assert sensor_msg["rssi"] == -52 + + def test_bthome_v2_motion(self): + """Test BTHome parser for motion measurement""" + data_string = "043E1602010000A5808FE648540A0201060616D2FC402101CC" + data = bytes(bytearray.fromhex(data_string)) + + # pylint: disable=unused-variable + ble_parser = BleParser() + sensor_msg, tracker_msg = ble_parser.parse_raw_data(data) + + assert sensor_msg["firmware"] == "BTHome V2" + assert sensor_msg["type"] == "BTHome" + assert sensor_msg["mac"] == "5448E68F80A5" + assert sensor_msg["packet"] == "no packet id" + assert sensor_msg["data"] + assert sensor_msg["motion"] == 1 + assert sensor_msg["rssi"] == -52 + + def test_bthome_v2_moving(self): + """Test BTHome parser for moving measurement""" + data_string = "043E1602010000A5808FE648540A0201060616D2FC402201CC" + data = bytes(bytearray.fromhex(data_string)) + + # pylint: disable=unused-variable + ble_parser = BleParser() + sensor_msg, tracker_msg = ble_parser.parse_raw_data(data) + + assert sensor_msg["firmware"] == "BTHome V2" + assert sensor_msg["type"] == "BTHome" + assert sensor_msg["mac"] == "5448E68F80A5" + assert sensor_msg["packet"] == "no packet id" + assert sensor_msg["data"] + assert sensor_msg["moving"] == 1 + assert sensor_msg["rssi"] == -52 + + def test_bthome_v2_occupancy(self): + """Test BTHome parser for occupancy measurement""" + data_string = "043E1602010000A5808FE648540A0201060616D2FC402300CC" + data = bytes(bytearray.fromhex(data_string)) + + # pylint: disable=unused-variable + ble_parser = BleParser() + sensor_msg, tracker_msg = ble_parser.parse_raw_data(data) + + assert sensor_msg["firmware"] == "BTHome V2" + assert sensor_msg["type"] == "BTHome" + assert sensor_msg["mac"] == "5448E68F80A5" + assert sensor_msg["packet"] == "no packet id" + assert sensor_msg["data"] + assert sensor_msg["occupancy"] == 0 + assert sensor_msg["rssi"] == -52 + + def test_bthome_v2_plug(self): + """Test BTHome parser for plug measurement""" + data_string = "043E1602010000A5808FE648540A0201060616D2FC402401CC" data = bytes(bytearray.fromhex(data_string)) # pylint: disable=unused-variable @@ -489,7 +557,7 @@ def test_bthome_v2_moisture_detected(self): assert sensor_msg["mac"] == "5448E68F80A5" assert sensor_msg["packet"] == "no packet id" assert sensor_msg["data"] - assert sensor_msg["moisture_detected"] == 1 + assert sensor_msg["plug"] == 1 assert sensor_msg["rssi"] == -52 def test_bthome_v2_button(self):