8000
Skip to content

[ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 11: xiaomi_lywsdcgq, xiaomi_mhoc303, xiaomi_mhoc401) - #18178

Merged
bdraco merged 1 commit into
esphome:devfrom
Bl00d-B0b:ble-sensors-batch11
Aug 8, 2026
Merged

[ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 11: xiaomi_lywsdcgq, xiaomi_mhoc303, xiaomi_mhoc401)#18178
bdraco merged 1 commit into
esphome:devfrom
Bl00d-B0b:ble-sensors-batch11

Conversation

@Bl00d-B0b
Copy link
Copy Markdown
Contributor

What does this implement/fix?

Batch 11 of 13 of the neutral-layer BLE sensor migration — the series is defined and reviewed in #17716 (batch 1, merged). The shared ble_device_base enablers landed in #18081. This branch is cut against merged dev and carries only the three platform migrations. Per review direction: batches of up to 3 similar platforms, one PR open for review at a time.

What migrates in this batch

xiaomi_lywsdcgq, xiaomi_mhoc303 and xiaomi_mhoc401 — the original LYWSDCGQ hygrometer plus the two Miaomiaoce clock-thermometers, all decoding through the shared xiaomi_ble parser migrated in #18168.

Same uniform recipe as #17716: python DEPENDENCIESAUTO_LOAD ["ble_device_base"], schema extends ble_device_base.BLE_DEVICE_SCHEMA with rename_legacy_hub_id prepended, registration via ble_device_base.register_ble_device, C++ esp32_ble_tracker::ble_device_base::, USE_ESP32 guards dropped.

No crypto or parser changes in this batch — xiaomi_ble already carries the portable AES-CCM path from #18168, and these three are consumers of it.

Fixtures: all three common.yaml files gain id: ble_tracker_hub and pin ble_hub_id: on the sensor entry; new per-platform common-ln.yaml + test.ln882x-ard.yaml compile the guard-free C++ on LN882H and exercise the generated binding (no explicit hub id); the config-only validate.bk72xx-ard.yaml fixtures pair the pinned entry with a second ble_hub_id-less entry, matching the merged batches. All nine fixture combinations validate.

Also fixes the xiaomi_mhoc401 fixtures, which named their entities MHO-C303 (wrong device, pre-existing on dev) - now MHO-C401. Fixture text only, in files this batch already touches.

Docs: esphome/esphome.io#7132 (opened for batch 7) documents the ble_hub_id tracker binding on the shared Xiaomi BLE page and is worded to cover both migrated and not-yet-migrated platforms, so this batch needs no further docs change.

Batch roadmap

Batch Where Platforms
1 #17716 (merged) ble_presence, ble_rssi, ble_scanner + shared ble_device_base enablers
2 #17950 (merged) atc_mithermometer, pvvx_mithermometer, bthome_mithermometer
3 #17951 (merged) mopeka_ble, mopeka_pro_check, mopeka_std_check
4 #18161 (merged) ruuvi_ble, ruuvitag, b_parasite
5 #18165 (merged) airthings_ble, inkbird_ibsth1_mini, radon_eye_ble
6 #18168 (merged) thermopro_ble, exposure_notifications, xiaomi_ble (shared hub; portable AES-CCM)
7 #18170 (merged) xiaomi_cgd1, xiaomi_cgdk2, xiaomi_cgg1
8 #18171 (merged) xiaomi_cgpr1, xiaomi_gcls002, xiaomi_hhccjcy01
9 #18172 (merged) xiaomi_hhccjcy10, xiaomi_hhccpot002, xiaomi_jqjcy01ym
10 #18174 (merged) xiaomi_lywsd02, xiaomi_lywsd02mmc, xiaomi_lywsd03mmc
11 this PR xiaomi_lywsdcgq, xiaomi_mhoc303, xiaomi_mhoc401
12 branch ble-sensors-batch12 xiaomi_miscale, xiaomi_mjyd02yla, xiaomi_mue4094rt
13 branch ble-sensors-batch13 xiaomi_rtcgq02lm, xiaomi_wx08zm, xiaomi_xmwsdj04mmc

Each batch is a single commit cut from the same validated full-migration tree, so the series is complete by construction (batch 1 + batches 2–13 = all 39 platforms). A batch PR opens only when the previous one merges — one PR open for review at a time per the direction on #17716.

Breaking changes and migration

Same as the rest of the series: the auto-generated per-sensor tracker reference esp32_ble_id: becomes ble_hub_id: on the migrated platforms. Most configurations are unaffected: the key is auto-generated and only written explicitly to disambiguate multiple trackers. Configurations that do set it rename the key:

# before
sensor:
  - platform: xiaomi_lywsdcgq
    esp32_ble_id: my_tracker
    mac_address: "7A:80:8E:19:36:BA"
    temperature:
      name: "Xiaomi LYWSDCGQ Temperature"

# after
sensor:
  - platform: xiaomi_lywsdcgq
    ble_hub_id: my_tracker
    mac_address: "7A:80:8E:19:36:BA"
    temperature:
      name: "Xiaomi LYWSDCGQ Temperature"

The transitional alias (ble_device_base.rename_legacy_hub_id) warns and auto-migrates an explicit esp32_ble_id: until 2027.2.0, so existing configurations keep validating through the rename window.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • New developer-facing feature (adds functionality for component developers; no end-user configuration change)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — policy

Example entry for config.yaml:

sensor:
  - platform: xiaomi_lywsdcgq
    mac_address: "7A:80:8E:19:36:BA"
    temperature:
      name: "Xiaomi LYWSDCGQ Temperature"
    humidity:
      name: "Xiaomi LYWSDCGQ Humidity"

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

…batch 11: xiaomi_lywsdcgq, xiaomi_mhoc303, xiaomi_mhoc401)
@esphome
esphome Bot commented Aug 8, 2026
Copy link
Copy Markdown
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#18178
    components: [xiaomi_lywsdcgq, xiaomi_mhoc303, xiaomi_mhoc401]
    refresh: 1h

(Added by the PR bot)

@esphome
esphome Bot commented Aug 8, 2026
Copy link
Copy Markdown
Contributor

👋 Hi there! This PR modifies 6 file(s) with codeowners.

@drug123, @vevsvevs - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

@codecov
codecov Bot commented Aug 8, 2026
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.28%. Comparing base (413a4c5) to head (8937d7a).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##              dev   #18178   +/-   ##
=======================================
  Coverage   87.28%   87.28%           
=======================================
  Files          64       64           
  Lines       14697    14697           
  Branches     2217     2217           
=======================================
  Hits        12829    12829           
  Misses       1558     1558           
  Partials      310      310           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bluetoothbot
Copy link
Copy Markdown
Contributor

PR Review — [ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 11: xiaomi_lywsdcgq, xiaomi_mhoc303, xiaomi_mhoc401)

Clean, mechanical batch that matches the merged batch-10 recipe exactly. Merge-ready.

Specific things done well:

  • The three sensor.py migrations are byte-for-byte the same shape as xiaomi_lywsd03mmc/sensor.py from the merged [ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 10: xiaomi_lywsd02, xiaomi_lywsd02mmc, xiaomi_lywsd03mmc) #18174cv.All(rename_legacy_hub_id(...), Schema(...).extend(cv.COMPONENT_SCHEMA).extend(ble_device_base.BLE_DEVICE_SCHEMA)) — so the transitional esp32_ble_id: alias and the declared-key requirement (needed because PREVENT_EXTRA would otherwise reject an explicit ble_hub_id:) are both handled correctly.
  • The USE_ESP32 guard removal is safe: I checked all three .cpp/.h files and none touch an ESP32-only API. xiaomi_ble already selects its AES-CCM backend portably (xiaomi_ble.cpp:11-20), so xiaomi_mhoc401's decrypt_xiaomi_payload path builds off-ESP32.
  • truncf in xiaomi_mhoc401.cpp:52 was a plausible off-ESP32 compile risk, but xiaomi_lywsd03mmc.cpp:50 uses it identically and already ships an ln882x-ard compile test that merged green — verified, not a problem.
  • Fixture coverage is complete across the three files: test.esp32-idf.yaml (explicit ble_hub_id), test.ln882x-ard.yaml (implicit binding, compiled), validate.bk72xx-ard.yaml (both forms, config-only). ln882h_ble_tracker calls register_hub_provider() at import, so the implicit binding on LN882H resolves.
  • The MHO-C303 -> MHO-C401 fixture rename is a genuine fix, not scope creep — it removes an entity-name collision with xiaomi_mhoc303's fixture in grouped builds, and it is declared in the PR description.
  • No leftover esp32_ble_tracker references remain in the three component directories, and nothing outside them references these platforms.

No blocking issues found. The id: ble_tracker_hub addition to common.yaml follows 30 existing fixtures on dev, and the added validate.*.yaml entries are never compiled, so the memory-impact report is unaffected.



Checklist

  • Migration is complete — no residual esp32_ble_tracker references in the three components
  • USE_ESP32 guard removal is safe (no ESP32-only APIs in the migrated C++)
  • Backward compatibility — esp32_ble_id: still validates via rename_legacy_hub_id until 2027.2.0
  • Test fixtures cover explicit and implicit hub binding on esp32, ln882x, bk72xx
  • Diff matches PR description; no scope creep
  • No hardcoded secrets (bindkey values are existing public test vectors)

Automated review by Kōan (Claude) HEAD=8937d7a 2 min 37s

@bluetoothbot bluetoothbot left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

No blocking issues found — ready to merge.

bdraco
bdraco 8000 approved these changes Aug 8, 2026
@bdraco bdraco left a comment
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Bl00d-B0b

@bdraco
bdraco merged commit 98f4854 into esphome:dev Aug 8, 2026
51 of 52 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 10, 2026
@Bl00d-B0b
Bl00d-B0b deleted the ble-sensors-batch11 branch August 20, 2026 09:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0