Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative temperatures and fraction of KKM K6 sensor #1329

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions custom_components/ble_monitor/ble_parser/kkm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ def parse_kkm(self, data: bytes, mac: str):
">BBBHbBBBhhh", data[4:19]
)
if frame_type == 0x21 and version == 1:
if temp < 0:
temperature = -(temp + 128 + temp_frac / 100)
else:
temperature = temp + temp_frac / 100
humidity = humi + humi_frac / 100
temperature = temp + temp_frac / 256
humidity = humi + humi_frac / 256
result.update(
{
"temperature": temperature,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ble_monitor/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"btsocket>=0.2.0",
"pyric>=0.1.6.3"
],
"version": "12.10.1"
"version": "12.10.2"
}
32 changes: 28 additions & 4 deletions custom_components/ble_monitor/test/test_kkm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class TestKKM:
"""Tests for the KKM parser"""
def test_kkm_k6(self):
"""Test KKM BLE parser for K6 sensors"""
data_string = "043E26020100016CD0060234DD1A0201060303AAFE1216AAFE21010F0E07192A224FFFFCFFEC03EBD3"
data_string = "043E26020100016CD0060234DD1A0201060303AAFE1216AAFE2101070e5b16531f95FFFCFFEC03EBD3"
data = bytes(bytearray.fromhex(data_string))

# pylint: disable=unused-variable
Expand All @@ -18,12 +18,36 @@ def test_kkm_k6(self):
assert sensor_msg["mac"] == "DD340206D06C"
assert sensor_msg["packet"] == "no packet id"
assert sensor_msg["data"]
assert sensor_msg["temperature"] == 25.42
assert sensor_msg["humidity"] == 34.79
assert sensor_msg["temperature"] == 22.32421875
assert sensor_msg["humidity"] == 31.58203125
assert sensor_msg["acceleration"] == 1003.2
assert sensor_msg["acceleration x"] == -4
assert sensor_msg["acceleration y"] == -20
assert sensor_msg["acceleration z"] == 1003
assert sensor_msg["voltage"] == 3.591
assert sensor_msg["voltage"] == 3.675
assert sensor_msg["battery"] == 100
assert sensor_msg["rssi"] == -45

def test_kkm_k6_neg_temp(self):
"""Test KKM BLE parser for K6 sensors with negative temperature"""
data_string = "043E26020100016CD0060234DD1A0201060303AAFE1216AAFE2101070e5bffc01f95FFFCFFEC03EBD3"
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"] == "KKM"
assert sensor_msg["type"] == "K6 Sensor Beacon"
assert sensor_msg["mac"] == "DD340206D06C"
assert sensor_msg["packet"] == "no packet id"
assert sensor_msg["data"]
assert sensor_msg["temperature"] == -0.25
assert sensor_msg["humidity"] == 31.58203125
assert sensor_msg["acceleration"] == 1003.2
assert sensor_msg["acceleration x"] == -4
assert sensor_msg["acceleration y"] == -20
assert sensor_msg["acceleration z"] == 1003
assert sensor_msg["voltage"] == 3.675
assert sensor_msg["battery"] == 100
assert sensor_msg["rssi"] == -45
Loading