Skip to content

Commit

Permalink
Fix negative temperatures using signed hex #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bkbilly committed Feb 16, 2025
1 parent 94303a1 commit d0f8490
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions custom_components/tpms_ble/tpms_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ def _start_update(self, service_info: BluetoothServiceInfo) -> None:
self.set_device_manufacturer("TPMS")

if "000027a5-0000-1000-8000-00805f9b34fb" in service_info.service_uuids:
self._process_tpms_2(address, local_name, mfr_data, company_id)
self._process_tpms_b(address, local_name, mfr_data, company_id)
elif company_id == 256:
self._process_tpms_1(address, local_name, mfr_data)
self._process_tpms_a(address, local_name, mfr_data)
else:
_LOGGER.error("Can't find the correct data type")

def _process_tpms_1(self, address: str, local_name: str, data: bytes) -> None:
def _process_tpms_a(self, address: str, local_name: str, data: bytes) -> None:
"""Parser for TPMS sensors."""
_LOGGER.debug("Parsing TPMS sensor: %s", data)
_LOGGER.debug("Parsing TPMS TypeA sensor: %s", data)
msg_length = len(data)
if msg_length != 16:
_LOGGER.error("Can't parse the data because the data length should be 16")
Expand All @@ -68,9 +68,9 @@ def _process_tpms_1(self, address: str, local_name: str, data: bytes) -> None:
temperature = temperature / 100
self._update_sensors(address, pressure, battery, temperature, alarm)

def _process_tpms_2(self, address: str, local_name: str, data: bytes, company_id: int) -> None:
def _process_tpms_b(self, address: str, local_name: str, data: bytes, company_id: int) -> None:
"""Parser for TPMS sensors."""
_LOGGER.debug("Parsing TPMS2 sensor: %s", data)
_LOGGER.debug("Parsing TPMS TypeB sensor: (%s) %s", company_id, data)
comp_hex = re.findall("..", hex(company_id)[2:].zfill(4))[::-1]
comp_hex = "".join(comp_hex)
data_hex = data.hex()
Expand All @@ -81,6 +81,8 @@ def _process_tpms_2(self, address: str, local_name: str, data: bytes, company_id
return
voltage = int(comp_hex[2:4], 16) / 10
temperature = int(data_hex[0:2], 16)
if temperature >= 2 ** 7:
temperature -= 2 ** 8
psi_pressure = (int(data_hex[2:6], 16) - 145) / 10

pressure = round(psi_pressure * 0.0689476, 3)
Expand Down

0 comments on commit d0f8490

Please sign in to comment.