[ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 6: thermopro_ble, exposure_notifications, xiaomi_ble) (#18168)

This commit is contained in:
Edvard Filistovič
2026-08-07 20:47:52 -05:00
committed by GitHub
parent 8d494a84c5
commit 1d184b43eb
22 changed files with 196 additions and 85 deletions
@@ -19,6 +19,33 @@ const uint8_t TAG[4] = {0x48, 0x4d, 0xaa, 0x56};
const uint8_t PLAINTEXT[7] = {0x02, 0x01, 0x64, 0x03, 0x10, 0x8a, 0x01};
} // namespace
// Xiaomi's parameters differ from BTHome's: a 12-byte nonce and a 1-byte AAD
// (0x11). Both the AAD block and the l = 3 length encoding are only reachable
// through this shape, so they need their own vector. Generated the same way.
namespace {
const uint8_t NONCE_XIAOMI[12] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b};
const uint8_t AAD_XIAOMI[1] = {0x11};
const uint8_t CIPHERTEXT_XIAOMI[5] = {0xc3, 0x7e, 0x0a, 0x1d, 0x23};
const uint8_t TAG_XIAOMI[4] = {0x98, 0x79, 0x87, 0xc6};
const uint8_t PLAINTEXT_XIAOMI[5] = {0x04, 0x10, 0x02, 0xd4, 0x00};
} // namespace
TEST(BleAesCcm, DecryptsXiaomiShapedVector) {
uint8_t out[sizeof(PLAINTEXT_XIAOMI)] = {};
EXPECT_TRUE(aes_ccm_auth_decrypt(KEY, NONCE_XIAOMI, sizeof(NONCE_XIAOMI), AAD_XIAOMI, sizeof(AAD_XIAOMI),
CIPHERTEXT_XIAOMI, sizeof(CIPHERTEXT_XIAOMI), out, TAG_XIAOMI, sizeof(TAG_XIAOMI)));
EXPECT_EQ(0, memcmp(out, PLAINTEXT_XIAOMI, sizeof(PLAINTEXT_XIAOMI)));
}
TEST(BleAesCcm, RejectsWrongAssociatedData) {
uint8_t bad_aad[sizeof(AAD_XIAOMI)];
memcpy(bad_aad, AAD_XIAOMI, sizeof(AAD_XIAOMI));
bad_aad[0] ^= 0x01;
uint8_t out[sizeof(PLAINTEXT_XIAOMI)] = {};
EXPECT_FALSE(aes_ccm_auth_decrypt(KEY, NONCE_XIAOMI, sizeof(NONCE_XIAOMI), bad_aad, sizeof(bad_aad),
CIPHERTEXT_XIAOMI, sizeof(CIPHERTEXT_XIAOMI), out, TAG_XIAOMI, sizeof(TAG_XIAOMI)));
}
TEST(BleAesCcm, DecryptsAndAuthenticatesKnownVector) {
uint8_t out[sizeof(PLAINTEXT)] = {};
EXPECT_TRUE(aes_ccm_auth_decrypt(KEY, NONCE, sizeof(NONCE), nullptr, 0, CIPHERTEXT, sizeof(CIPHERTEXT), out, TAG,
@@ -0,0 +1,5 @@
exposure_notifications:
on_exposure_notification:
then:
- lambda: |
ESP_LOGD("main", "RSSI: %d", x.rssi);
@@ -1,7 +1,10 @@
esp32_ble_tracker:
id: ble_tracker_hub
exposure_notifications:
on_exposure_notification:
# Explicit ble_hub_id: pins the neutral binding as a declared key.
ble_hub_id: ble_tracker_hub
then:
- lambda: |
ESP_LOGD("main", "Got notification:");
@@ -0,0 +1,3 @@
packages:
ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml
exposure_notifications: !include common-ln.yaml
@@ -0,0 +1,15 @@
# Config-only: the CI base board (generic-bk7252, BLE 4.2) cannot compile the
# BLE 5.x tracker, so this fixture proves validation (schema + neutral binding)
# on a non-esp32 platform; codegen and compilation are not exercised here.
bk72xx_ble_tracker:
id: ble_tracker_hub
exposure_notifications:
on_exposure_notification:
# Explicit ble_hub_id: pins the neutral binding as a declared key.
ble_hub_id: ble_tracker_hub
then:
- lambda: |
ESP_LOGD("main", "Got notification:");
ESP_LOGD("main", " RPI: %s", format_hex(x.rolling_proximity_identifier).c_str());
ESP_LOGD("main", " RSSI: %d", x.rssi);
@@ -0,0 +1,7 @@
sensor:
- platform: thermopro_ble
mac_address: FE:74:B8:6A:97:B7
temperature:
name: ThermoPro Temperature
humidity:
name: ThermoPro Humidity
@@ -1,7 +1,10 @@
esp32_ble_tracker:
id: ble_tracker_hub
sensor:
# Explicit ble_hub_id: pins the neutral binding as a declared key.
- platform: thermopro_ble
ble_hub_id: ble_tracker_hub
mac_address: FE:74:B8:6A:97:B7
temperature:
name: "ThermoPro Temperature"
@@ -0,0 +1,3 @@
packages:
ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml
thermopro_ble: !include common-ln.yaml
@@ -0,0 +1,24 @@
# Config-only: the CI base board (generic-bk7252, BLE 4.2) cannot compile the
# BLE 5.x tracker, so this fixture proves validation (schema + neutral binding)
# on a non-esp32 platform; codegen and compilation are not exercised here.
bk72xx_ble_tracker:
id: ble_tracker_hub
sensor:
# Explicit ble_hub_id: pins the neutral binding as a declared key.
- platform: thermopro_ble
ble_hub_id: ble_tracker_hub
mac_address: FE:74:B8:6A:97:B7
temperature:
name: "ThermoPro Temperature"
humidity:
name: "ThermoPro Humidity"
battery_level:
name: "ThermoPro Battery Level"
signal_strength:
name: "ThermoPro Signal Strength"
# No ble_hub_id: exercises the generated binding real configs use.
- platform: thermopro_ble
mac_address: FE:74:B8:6A:97:B8
temperature:
name: BK ThermoPro Implicit Temperature
@@ -0,0 +1 @@
xiaomi_ble:
+3
View File
@@ -1,3 +1,6 @@
esp32_ble_tracker:
id: ble_tracker_hub
# Explicit ble_hub_id: pins the neutral binding as a declared key.
xiaomi_ble:
ble_hub_id: ble_tracker_hub
@@ -0,0 +1,3 @@
packages:
ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml
xiaomi_ble: !include common-ln.yaml
@@ -0,0 +1,9 @@
# Config-only: the CI base board (generic-bk7252, BLE 4.2) cannot compile the
# BLE 5.x tracker, so this fixture proves validation (schema + neutral binding)
# on a non-esp32 platform; codegen and compilation are not exercised here.
bk72xx_ble_tracker:
id: ble_tracker_hub
# Explicit ble_hub_id: pins the neutral binding as a declared key.
xiaomi_ble:
ble_hub_id: ble_tracker_hub