Skip to content

Commit

Permalink
fix: devices not being detected with bluez (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 5, 2022
1 parent 0ed44ff commit ba7c6c6
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/thermopro_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ def _start_update(self, service_info: BluetoothServiceInfo) -> None:
_LOGGER.debug("Parsing thermopro BLE advertisement data: %s", service_info)
if not service_info.name.startswith(("TP35", "TP39")):
return

if not service_info.manufacturer_data:
return
if len(list(service_info.manufacturer_data.values())[0]) != 4:
return
self.set_device_type(service_info.name.split(" ")[0])
self.set_title(f"{service_info.name} {short_address(service_info.address)}")
self.set_device_name(service_info.name)
self.set_precision(2)
self.set_device_manufacturer("ThermoPro")
changed_manufacturer_data = self.changed_manufacturer_data(service_info)

if not changed_manufacturer_data:
Expand All @@ -45,10 +53,5 @@ def _start_update(self, service_info: BluetoothServiceInfo) -> None:
return

(temp, humi) = UNPACK(data[1:4])
self.set_precision(2)
self.set_device_type(service_info.name.split(" ")[0])
self.update_predefined_sensor(SensorLibrary.TEMPERATURE__CELSIUS, temp / 10)
self.update_predefined_sensor(SensorLibrary.HUMIDITY__PERCENTAGE, humi)
self.set_title(f"{service_info.name} {short_address(service_info.address)}")
self.set_device_name(service_info.name)
self.set_device_manufacturer("ThermoPro")
90 changes: 90 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,61 @@ def test_can_create():
)


TP393_DETECT_CHANGED_1 = BluetoothServiceInfo(
name="TP393 (9376)",
manufacturer_data={
194: b"\x00\x00\x00,",
62146: b"\x00(\x02,",
61890: b"\x00(\x02,",
61634: b"\x00(\x02,",
61378: b"\x00(\x02,",
61122: b"\x00(\x02,",
60866: b"\x00(\x02,",
60610: b"\x00(\x02,",
60354: b"\x00)\x02,",
60098: b"\x00)\x02,",
59842: b"\x00)\x02,",
59586: b"\x00)\x02,",
59330: b"\x00*\x02,",
59074: b"\x00*\x02,",
58818: b"\x00*\x02,",
58562: b"\x00*\x02,",
},
service_uuids=[],
address="aa:bb:cc:dd:ee:ff",
rssi=-60,
service_data={},
source="local",
)

TP393_DETECT_CHANGED_2 = BluetoothServiceInfo(
name="TP393 (9376)",
manufacturer_data={
194: b"\x00\x00\x00,",
62146: b"\x00(\x02,",
61890: b"\x00(\x02,",
61634: b"\x00(\x02,",
61378: b"\x00(\x02,",
61122: b"\x00(\x02,",
60866: b"\x00(\x02,",
60610: b"\x00(\x02,",
60354: b"\x00)\x02,",
60098: b"\x00)\x02,",
59842: b"\x00)\x02,",
59586: b"\x00)\x02,",
59330: b"\x00*\x02,",
59074: b"\x00*\x02,",
58818: b"\x00*\x02,",
58562: b"\x00*\x02,",
},
service_uuids=[],
address="aa:bb:cc:dd:ee:ff",
rssi=-60,
service_data={},
source="local",
)


def test_supported_set_the_title():
parser = ThermoProBluetoothDeviceData()
parser.supported(TP357) is True
Expand Down Expand Up @@ -206,3 +261,38 @@ def test_tp393():
binary_entity_descriptions={},
binary_entity_values={},
)


def test_tp393_multi_updates():
parser = ThermoProBluetoothDeviceData()
assert parser.supported(TP393_DETECT_CHANGED_1) is True
parser.update(TP393_DETECT_CHANGED_1)
result = parser.update(TP393_DETECT_CHANGED_2)
assert result == SensorUpdate(
title="TP393 (9376) EEFF",
devices={
None: SensorDeviceInfo(
name="TP393 (9376)",
model="TP393",
manufacturer="ThermoPro",
sw_version=None,
hw_version=None,
)
},
entity_descriptions={
DeviceKey(key="signal_strength", device_id=None): SensorDescription(
device_key=DeviceKey(key="signal_strength", device_id=None),
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
)
},
entity_values={
DeviceKey(key="signal_strength", device_id=None): SensorValue(
device_key=DeviceKey(key="signal_strength", device_id=None),
name="Signal " "Strength",
native_value=-60,
)
},
binary_entity_descriptions={},
binary_entity_values={},
)

0 comments on commit ba7c6c6

Please sign in to comment.