Skip to content

Commit

Permalink
Add binary bthome sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 committed Sep 23, 2023
1 parent fa6ead3 commit a963c94
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
41 changes: 41 additions & 0 deletions custom_components/ble_monitor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ class BLEMonitorBinarySensorEntityDescription(


BINARY_SENSOR_TYPES: tuple[BLEMonitorBinarySensorEntityDescription, ...] = (
BLEMonitorBinarySensorEntityDescription(
key="battery_charging",
sensor_class="BaseBinarySensor",
update_behavior="Instantly",
name="battery_charging",
unique_id="bc_",
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
force_update=True,
),
BLEMonitorBinarySensorEntityDescription(
key="carbon_monoxide",
sensor_class="BaseBinarySensor",
update_behavior="Instantly",
name="carbon monoxide",
unique_id="bco_",
device_class=BinarySensorDeviceClass.CO,
force_update=True,
),
BLEMonitorBinarySensorEntityDescription(
key="cold",
sensor_class="BaseBinarySensor",
update_behavior="Instantly",
name="cold",
unique_id="bcold_",
device_class=BinarySensorDeviceClass.COLD,
force_update=True,
),
BLEMonitorBinarySensorEntityDescription(
key="connectivity",
sensor_class="BaseBinarySensor",
update_behavior="Instantly",
name="connectivity",
unique_id="bconn_",
device_class=BinarySensorDeviceClass.CONNECTIVITY,
force_update=True,
),
BLEMonitorBinarySensorEntityDescription(
key="binary",
sensor_class="BaseBinarySensor",
Expand Down Expand Up @@ -1500,6 +1536,10 @@ class BLEMonitorBinarySensorEntityDescription(

# Binary Sensors that are automatically added if device is in AUTO_MANUFACTURER_DICT
AUTO_BINARY_SENSOR_LIST = [
"battery_charging",
"carbon_monoxide",
"cold",
"connectivity",
"binary",
"door",
"dropping",
Expand All @@ -1524,6 +1564,7 @@ class BLEMonitorBinarySensorEntityDescription(
"conductivity",
"count",
"dewpoint",
"dimmer",
"energy",
"gravity",
"humidity",
Expand Down
68 changes: 68 additions & 0 deletions custom_components/ble_monitor/test/test_bthome_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,74 @@ def test_bthome_v2_moisture(self):
assert sensor_msg["moisture"] == 3.07
assert sensor_msg["rssi"] == -52

def test_bthome_v2_battery_charging(self):
"""Test BTHome parser for battery charging measurement"""
data_string = "043E1602010000A5808FE648540A0201060616D2FC401601CC"
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["battery_charging"] == 1
assert sensor_msg["rssi"] == -52

def test_bthome_v2_carbon_monoxide(self):
"""Test BTHome parser for carbon monoxide measurement"""
data_string = "043E1602010000A5808FE648540A0201060616D2FC401701CC"
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["carbon_monoxide"] == 1
assert sensor_msg["rssi"] == -52

def test_bthome_v2_cold(self):
"""Test BTHome parser for cold measurement"""
data_string = "043E1602010000A5808FE648540A0201060616D2FC401801CC"
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["cold"] == 1
assert sensor_msg["rssi"] == -52

def test_bthome_v2_connectivity(self):
"""Test BTHome parser for connectivity measurement"""
data_string = "043E1602010000A5808FE648540A0201060616D2FC401901CC"
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["connectivity"] == 1
assert sensor_msg["rssi"] == -52

def test_bthome_v2_button(self):
"""Test BTHome parser for button sensor measurement"""
data_string = "043E1602010000A5808FE648540A0201060616D2FC403A02CC"
Expand Down

0 comments on commit a963c94

Please sign in to comment.