From 611eef06dd4e5bedfcb017ec7665d450b99fc9a0 Mon Sep 17 00:00:00 2001 From: Daniel Hou Date: Sun, 22 Nov 2026 01:43:56 -0500 Subject: [PATCH 1/3] feat: add support for ZenMeasure CO2 Monitor Lite (MHO-H413) Add product ID 0x6BDF and a parser for MiBeacon object 0x4C1D, which the MHO-H413 uses for CO2 concentration (uint16, ppm). Temperature (0x4C01) and humidity (0x4C08) already parse as floats. The device sends one encrypted object per advertisement with frame control 0x5948 (no MAC in the frame), plus unencrypted header-only beacons. Tests use synthetic frames built with a dummy bindkey. --- src/xiaomi_ble/devices.py | 5 ++ src/xiaomi_ble/parser.py | 13 +++++ tests/test_parser.py | 119 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) diff --git a/src/xiaomi_ble/devices.py b/src/xiaomi_ble/devices.py index 0e1276b..9490d62 100644 --- a/src/xiaomi_ble/devices.py +++ b/src/xiaomi_ble/devices.py @@ -396,6 +396,11 @@ class DeviceEntry: model="ES5BB", manufacturer="Linptech", ), + 0x6BDF: DeviceEntry( + name="CO2 Monitor Lite", + model="MHO-H413", + manufacturer="ZenMeasure", + ), } diff --git a/src/xiaomi_ble/parser.py b/src/xiaomi_ble/parser.py index f8c1476..2023dda 100644 --- a/src/xiaomi_ble/parser.py +++ b/src/xiaomi_ble/parser.py @@ -1552,6 +1552,18 @@ def obj4c14( return {"mode": mode} +def obj4c1d( + xobj: bytes, device: XiaomiBluetoothDeviceData, device_type: str +) -> dict[str, Any]: + """CO2""" + if len(xobj) == 2: + (co2,) = struct.unpack(" dict[str, Any]: @@ -2051,6 +2063,7 @@ def obj6e16( 0x4C03: obj4c03, 0x4C08: obj4c08, 0x4C14: obj4c14, + 0x4C1D: obj4c1d, 0x4E01: obj4e01, 0x4E16: obj4e16, 0x4E1C: obj4e1c, diff --git a/tests/test_parser.py b/tests/test_parser.py index 6157534..9fce3f7 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -176,6 +176,33 @@ def fwkge2_encrypted_service_info( return bytes_to_service_info(payload, address=address) +def mho_h413_encrypted_service_info( + objects: bytes, + *, + counter: int, +) -> BluetoothServiceInfo: + """Build a synthetic encrypted MHO-H413 MiBeacon v5 advertisement. + + The real device sends frame control 0x5948 (encrypted, object included, + no MAC in the frame), one object per advertisement. + """ + address = "A4:C1:38:12:34:56" + mac = bytes.fromhex(address.replace(":", "")) + bindkey = bytes.fromhex("00112233445566778899aabbccddeeff") + frame_control = 0x5948 + product_id = 0x6BDF + header = ( + frame_control.to_bytes(2, "little") + + product_id.to_bytes(2, "little") + + bytes([counter]) + ) + extended_counter = bytes([0, 0, 0]) + nonce = mac[::-1] + header[2:5] + extended_counter + encrypted = AESCCM(bindkey, tag_length=4).encrypt(nonce, objects, b"\x11") + payload = header + encrypted[:-4] + extended_counter + encrypted[-4:] + return bytes_to_service_info(payload, address=address) + + def test_blank_advertisements_then_encrypted(): """Test that we can reject empty payloads.""" device = XiaomiBluetoothDeviceData() @@ -7017,3 +7044,95 @@ def test_Xiaomi_K9BB_1BTN_double_press(): _assert_k9bb_1btn_event( bytes.fromhex("5859101c350537243cc218747156700400004f1a7bda"), "double_press" ) + + +MHO_H413_DEVICE = SensorDeviceInfo( + name="CO2 Monitor Lite 3456", + manufacturer="ZenMeasure", + model="MHO-H413", + hw_version=None, + sw_version="Xiaomi (MiBeacon V5 encrypted)", +) + + +@pytest.mark.parametrize( + ("objects", "key", "device_class", "unit", "name", "value"), + [ + ( + b"\x01\x4c\x04" + struct.pack(" None: + """Test Xiaomi parser for MHO-H413 (ZenMeasure CO2 Monitor Lite).""" + bindkey = "00112233445566778899aabbccddeeff" + advertisement = mho_h413_encrypted_service_info(objects, counter=0x1D) + + device = XiaomiBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) + assert device.supported(advertisement) + assert device.bindkey_verified + assert device.sleepy_device is False + assert device.update(advertisement) == SensorUpdate( + title="CO2 Monitor Lite 3456 (MHO-H413)", + devices={None: MHO_H413_DEVICE}, + entity_descriptions={ + DeviceKey(key=key, device_id=None): SensorDescription( + device_key=DeviceKey(key=key, device_id=None), + device_class=device_class, + native_unit_of_measurement=unit, + ), + KEY_SIGNAL_STRENGTH: SensorDescription( + device_key=KEY_SIGNAL_STRENGTH, + device_class=DeviceClass.SIGNAL_STRENGTH, + native_unit_of_measurement=Units.SIGNAL_STRENGTH_DECIBELS_MILLIWATT, + ), + }, + entity_values={ + DeviceKey(key=key, device_id=None): SensorValue( + name=name, + device_key=DeviceKey(key=key, device_id=None), + native_value=value, + ), + KEY_SIGNAL_STRENGTH: SensorValue( + name="Signal Strength", device_key=KEY_SIGNAL_STRENGTH, native_value=-60 + ), + }, + ) + + +def test_Xiaomi_MHO_H413_unencrypted_beacon() -> None: + """The MHO-H413 also sends header-only beacons (MAC + capability).""" + data_string = bytes.fromhex("3058df6b00563412" + "38c1a408") + advertisement = bytes_to_service_info(data_string, address="A4:C1:38:12:34:56") + + device = XiaomiBluetoothDeviceData() + assert device.supported(advertisement) + assert device.device_type == "MHO-H413" From 854f0d9e4c04adaacaa2e6babd1c5c44a15c9b86 Mon Sep 17 00:00:00 2001 From: Daniel Hou Date: Sun, 22 Nov 2026 19:05:16 -0800 Subject: [PATCH 2/3] fix: mark MHO-H413 as a sleepy device It advertises in bursts with multi-minute gaps, so Home Assistant's learned advertising interval marks it unavailable between bursts. --- src/xiaomi_ble/devices.py | 1 + tests/test_parser.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/xiaomi_ble/devices.py b/src/xiaomi_ble/devices.py index 9490d62..b91b12d 100644 --- a/src/xiaomi_ble/devices.py +++ b/src/xiaomi_ble/devices.py @@ -427,6 +427,7 @@ class DeviceEntry: "M2456B1", "M2457B1", "zhiee.flood.sj01", + "MHO-H413", } diff --git a/tests/test_parser.py b/tests/test_parser.py index 9fce3f7..9cbfd1b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -7099,7 +7099,7 @@ def test_Xiaomi_MHO_H413( device = XiaomiBluetoothDeviceData(bindkey=bytes.fromhex(bindkey)) assert device.supported(advertisement) assert device.bindkey_verified - assert device.sleepy_device is False + assert device.sleepy_device assert device.update(advertisement) == SensorUpdate( title="CO2 Monitor Lite 3456 (MHO-H413)", devices={None: MHO_H413_DEVICE}, From d3d4a459b00928be9a4fb73260fb74232c1afcb0 Mon Sep 17 00:00:00 2001 From: Daniel Hou Date: Mon, 23 Nov 2026 14:58:09 -0800 Subject: [PATCH 3/3] test: cover MHO-H413 CO2 object with unexpected length --- tests/test_parser.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_parser.py b/tests/test_parser.py index 9cbfd1b..eda0734 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -7128,6 +7128,21 @@ def test_Xiaomi_MHO_H413( ) +def test_Xiaomi_MHO_H413_co2_wrong_length_ignored() -> None: + """A CO2 object that is not 2 bytes long is ignored.""" + advertisement = mho_h413_encrypted_service_info( + b"\x1d\x4c\x03" + b"\xb7\x03\x00", counter=0x1E + ) + + device = XiaomiBluetoothDeviceData( + bindkey=bytes.fromhex("00112233445566778899aabbccddeeff") + ) + assert device.supported(advertisement) + assert device.bindkey_verified + update = device.update(advertisement) + assert DeviceKey(key="carbon_dioxide", device_id=None) not in update.entity_values + + def test_Xiaomi_MHO_H413_unencrypted_beacon() -> None: """The MHO-H413 also sends header-only beacons (MAC + capability).""" data_string = bytes.fromhex("3058df6b00563412" + "38c1a408")