Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1117 from custom-components/miband
Add Mi Band heart rate sensor
- Loading branch information
Showing
11 changed files
with
111 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Parser for Xiaomi Mi Band BLE advertisements | ||
import logging | ||
|
||
from .helpers import ( | ||
to_mac, | ||
to_unformatted_mac, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def parse_miband(self, data, source_mac, rssi): | ||
# check for data length | ||
msg_length = len(data) | ||
if msg_length == 28: | ||
device_type = "Mi Band" | ||
heart_rate = data[5] | ||
if heart_rate == 0xFF: | ||
return None | ||
result = {"heart rate": heart_rate} | ||
else: | ||
if self.report_unknown == "Mi Band": | ||
_LOGGER.info( | ||
"BLE ADV from UNKNOWN Mi Band DEVICE: MAC: %s, ADV: %s", | ||
to_mac(source_mac), | ||
data.hex() | ||
) | ||
return None | ||
|
||
firmware = device_type | ||
|
||
# check for MAC presence in sensor whitelist, if needed | ||
if self.discovery is False and source_mac.lower() not in self.sensor_whitelist: | ||
_LOGGER.debug("Discovery is disabled. MAC: %s is not whitelisted!", to_mac(source_mac)) | ||
return None | ||
|
||
result.update({ | ||
"type": device_type, | ||
"firmware": firmware, | ||
"mac": to_unformatted_mac(source_mac), | ||
"packet": 'no packet id', | ||
"rssi": rssi, | ||
"data": True, | ||
}) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""The tests for the Mi Band ble_parser.""" | ||
from ble_monitor.ble_parser import BleParser | ||
|
||
|
||
class TestMiBand: | ||
"""Tests for the Mi Band parser""" | ||
def test_miband(self): | ||
"""Test Mi Band parser.""" | ||
data_string = "043e390d011200004b0b893f09c50100ff7fb20000000000000000001f0201041bff5701020affffffffffffffffffffffffffffff03c5093f890b4b" | ||
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"] == "Mi Band" | ||
assert sensor_msg["type"] == "Mi Band" | ||
assert sensor_msg["mac"] == "C5093F890B4B" | ||
assert sensor_msg["packet"] == "no packet id" | ||
assert sensor_msg["data"] | ||
assert sensor_msg["heart rate"] == 10 | ||
assert sensor_msg["rssi"] == -78 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
manufacturer: xiaomi | ||
name: Mi Band | ||
model: Mi Band 4 and 5 | ||
image: Mi_Band.png | ||
physical_description: Oval Watch | ||
broadcasted_properties: | ||
- heart rate | ||
- rssi | ||
broadcasted_property_notes: | ||
broadcast_rate: | ||
active_scan: True | ||
encryption_key: | ||
custom_firmware: | ||
notes: | ||
- The Mi Band also reports `steps`, but this has not been implemented yet. If you want to help adding the steps, make a log with the BLE advertisements of the watch, with active scan turned on. | ||
--- |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters