From 23a34545512bda95450973acbc8d2f572eebfe13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Filistovi=C4=8D?= Date: Fri, 7 Aug 2026 22:21:46 +0300 Subject: [PATCH] [ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 4: ruuvi_ble, ruuvitag, b_parasite) (#18161) --- esphome/components/b_parasite/b_parasite.cpp | 6 +-- esphome/components/b_parasite/b_parasite.h | 10 ++--- esphome/components/b_parasite/sensor.py | 13 ++++--- esphome/components/ruuvi_ble/__init__.py | 21 +++++----- esphome/components/ruuvi_ble/ruuvi_ble.cpp | 10 ++--- esphome/components/ruuvi_ble/ruuvi_ble.h | 12 ++---- esphome/components/ruuvitag/ruuvitag.cpp | 4 -- esphome/components/ruuvitag/ruuvitag.h | 10 ++--- esphome/components/ruuvitag/sensor.py | 14 +++---- tests/components/b_parasite/common-ln.yaml | 7 ++++ tests/components/b_parasite/common.yaml | 3 ++ .../b_parasite/test.ln882x-ard.yaml | 3 ++ .../b_parasite/validate.bk72xx-ard.yaml | 26 +++++++++++++ tests/components/ruuvi_ble/common-ln.yaml | 1 + tests/components/ruuvi_ble/common.yaml | 3 ++ .../components/ruuvi_ble/test.ln882x-ard.yaml | 3 ++ .../ruuvi_ble/validate.bk72xx-ard.yaml | 9 +++++ tests/components/ruuvitag/common-ln.yaml | 7 ++++ tests/components/ruuvitag/common.yaml | 3 ++ .../components/ruuvitag/test.ln882x-ard.yaml | 3 ++ .../ruuvitag/validate.bk72xx-ard.yaml | 38 +++++++++++++++++++ 21 files changed, 146 insertions(+), 60 deletions(-) create mode 100644 tests/components/b_parasite/common-ln.yaml create mode 100644 tests/components/b_parasite/test.ln882x-ard.yaml create mode 100644 tests/components/b_parasite/validate.bk72xx-ard.yaml create mode 100644 tests/components/ruuvi_ble/common-ln.yaml create mode 100644 tests/components/ruuvi_ble/test.ln882x-ard.yaml create mode 100644 tests/components/ruuvi_ble/validate.bk72xx-ard.yaml create mode 100644 tests/components/ruuvitag/common-ln.yaml create mode 100644 tests/components/ruuvitag/test.ln882x-ard.yaml create mode 100644 tests/components/ruuvitag/validate.bk72xx-ard.yaml diff --git a/esphome/components/b_parasite/b_parasite.cpp b/esphome/components/b_parasite/b_parasite.cpp index 160d22a5b6..e0ae824b0d 100644 --- a/esphome/components/b_parasite/b_parasite.cpp +++ b/esphome/components/b_parasite/b_parasite.cpp @@ -1,8 +1,6 @@ #include "b_parasite.h" #include "esphome/core/log.h" -#ifdef USE_ESP32 - namespace esphome::b_parasite { static const char *const TAG = "b_parasite"; @@ -16,7 +14,7 @@ void BParasite::dump_config() { LOG_SENSOR(" ", "Illuminance", this->illuminance_); } -bool BParasite::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { +bool BParasite::parse_device(const ble_device_base::ESPBTDevice &device) { if (device.address_uint64() != address_) { ESP_LOGVV(TAG, "parse_device(): unknown MAC address."); return false; @@ -113,5 +111,3 @@ bool BParasite::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { } } // namespace esphome::b_parasite - -#endif // USE_ESP32 diff --git a/esphome/components/b_parasite/b_parasite.h b/esphome/components/b_parasite/b_parasite.h index 1d5ac6e702..65540e82fb 100644 --- a/esphome/components/b_parasite/b_parasite.h +++ b/esphome/components/b_parasite/b_parasite.h @@ -2,18 +2,16 @@ #include "esphome/core/component.h" #include "esphome/components/sensor/sensor.h" -#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" - -#ifdef USE_ESP32 +#include "esphome/components/ble_device_base/ble_device.h" namespace esphome::b_parasite { -class BParasite final : public Component, public esp32_ble_tracker::ESPBTDeviceListener { +class BParasite final : public Component, public ble_device_base::ESPBTDeviceListener { public: void set_address(uint64_t address) { address_ = address; }; void set_bindkey(const std::string &bindkey); - bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override; + bool parse_device(const ble_device_base::ESPBTDevice &device) override; void dump_config() override; void set_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; } @@ -35,5 +33,3 @@ class BParasite final : public Component, public esp32_ble_tracker::ESPBTDeviceL }; } // namespace esphome::b_parasite - -#endif // USE_ESP32 diff --git a/esphome/components/b_parasite/sensor.py b/esphome/components/b_parasite/sensor.py index 041303ad8b..673c5981b7 100644 --- a/esphome/components/b_parasite/sensor.py +++ b/esphome/components/b_parasite/sensor.py @@ -1,5 +1,5 @@ import esphome.codegen as cg -from esphome.components import esp32_ble_tracker, sensor +from esphome.components import ble_device_base, sensor import esphome.config_validation as cv from esphome.const import ( CONF_BATTERY_VOLTAGE, @@ -23,14 +23,15 @@ from esphome.const import ( CODEOWNERS = ["@rbaron"] -DEPENDENCIES = ["esp32_ble_tracker"] +AUTO_LOAD = ["ble_device_base"] b_parasite_ns = cg.esphome_ns.namespace("b_parasite") BParasite = b_parasite_ns.class_( - "BParasite", esp32_ble_tracker.ESPBTDeviceListener, cg.Component + "BParasite", ble_device_base.ESPBTDeviceListener, cg.Component ) -CONFIG_SCHEMA = ( +CONFIG_SCHEMA = cv.All( + ble_device_base.rename_legacy_hub_id("b_parasite"), cv.Schema( { cv.GenerateID(): cv.declare_id(BParasite), @@ -68,15 +69,15 @@ CONFIG_SCHEMA = ( ), } ) - .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) .extend(cv.COMPONENT_SCHEMA) + .extend(ble_device_base.BLE_DEVICE_SCHEMA), ) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) - await esp32_ble_tracker.register_ble_device(var, config) + await ble_device_base.register_ble_device(var, config) cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) diff --git a/esphome/components/ruuvi_ble/__init__.py b/esphome/components/ruuvi_ble/__init__.py index 13d49d3cfe..8ab95dcb72 100644 --- a/esphome/components/ruuvi_ble/__init__.py +++ b/esphome/components/ruuvi_ble/__init__.py @@ -1,22 +1,25 @@ import esphome.codegen as cg -from esphome.components import esp32_ble_tracker +from esphome.components import ble_device_base import esphome.config_validation as cv from esphome.const import CONF_ID -DEPENDENCIES = ["esp32_ble_tracker"] +AUTO_LOAD = ["ble_device_base"] ruuvi_ble_ns = cg.esphome_ns.namespace("ruuvi_ble") RuuviListener = ruuvi_ble_ns.class_( - "RuuviListener", esp32_ble_tracker.ESPBTDeviceListener + "RuuviListener", ble_device_base.ESPBTDeviceListener ) -CONFIG_SCHEMA = cv.Schema( - { - cv.GenerateID(): cv.declare_id(RuuviListener), - } -).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) +CONFIG_SCHEMA = cv.All( + ble_device_base.rename_legacy_hub_id("ruuvi_ble"), + cv.Schema( + { + cv.GenerateID(): cv.declare_id(RuuviListener), + } + ).extend(ble_device_base.BLE_DEVICE_SCHEMA), +) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) - await esp32_ble_tracker.register_ble_device(var, config) + await ble_device_base.register_ble_device(var, config) diff --git a/esphome/components/ruuvi_ble/ruuvi_ble.cpp b/esphome/components/ruuvi_ble/ruuvi_ble.cpp index b73b73d56e..19753b3c9e 100644 --- a/esphome/components/ruuvi_ble/ruuvi_ble.cpp +++ b/esphome/components/ruuvi_ble/ruuvi_ble.cpp @@ -1,13 +1,11 @@ #include "ruuvi_ble.h" #include "esphome/core/log.h" -#ifdef USE_ESP32 - namespace esphome::ruuvi_ble { static const char *const TAG = "ruuvi_ble"; -bool parse_ruuvi_data_byte(const esp32_ble_tracker::adv_data_t &adv_data, RuuviParseResult &result) { +bool parse_ruuvi_data_byte(const ble_device_base::adv_data_t &adv_data, RuuviParseResult &result) { const uint8_t data_type = adv_data[0]; const auto *data = &adv_data[1]; switch (data_type) { @@ -80,7 +78,7 @@ bool parse_ruuvi_data_byte(const esp32_ble_tracker::adv_data_t &adv_data, RuuviP return false; } } -optional parse_ruuvi(const esp32_ble_tracker::ESPBTDevice &device) { +optional parse_ruuvi(const ble_device_base::ESPBTDevice &device) { bool success = false; RuuviParseResult result{}; for (auto &it : device.get_manufacturer_datas()) { @@ -96,7 +94,7 @@ optional parse_ruuvi(const esp32_ble_tracker::ESPBTDevice &dev return result; } -bool RuuviListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { +bool RuuviListener::parse_device(const ble_device_base::ESPBTDevice &device) { auto res = parse_ruuvi(device); if (!res.has_value()) return false; @@ -142,5 +140,3 @@ bool RuuviListener::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { } } // namespace esphome::ruuvi_ble - -#endif diff --git a/esphome/components/ruuvi_ble/ruuvi_ble.h b/esphome/components/ruuvi_ble/ruuvi_ble.h index e372b24944..d345790e3a 100644 --- a/esphome/components/ruuvi_ble/ruuvi_ble.h +++ b/esphome/components/ruuvi_ble/ruuvi_ble.h @@ -1,9 +1,7 @@ #pragma once #include "esphome/core/component.h" -#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" - -#ifdef USE_ESP32 +#include "esphome/components/ble_device_base/ble_device.h" namespace esphome::ruuvi_ble { @@ -23,13 +21,11 @@ struct RuuviParseResult { bool parse_ruuvi_data_byte(uint8_t data_type, const uint8_t *data, uint8_t data_length, RuuviParseResult &result); -optional parse_ruuvi(const esp32_ble_tracker::ESPBTDevice &device); +optional parse_ruuvi(const ble_device_base::ESPBTDevice &device); -class RuuviListener final : public esp32_ble_tracker::ESPBTDeviceListener { +class RuuviListener final : public ble_device_base::ESPBTDeviceListener { public: - bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override; + bool parse_device(const ble_device_base::ESPBTDevice &device) override; }; } // namespace esphome::ruuvi_ble - -#endif diff --git a/esphome/components/ruuvitag/ruuvitag.cpp b/esphome/components/ruuvitag/ruuvitag.cpp index 99c6b8ae26..1536befb1b 100644 --- a/esphome/components/ruuvitag/ruuvitag.cpp +++ b/esphome/components/ruuvitag/ruuvitag.cpp @@ -1,8 +1,6 @@ #include "ruuvitag.h" #include "esphome/core/log.h" -#ifdef USE_ESP32 - namespace esphome::ruuvitag { static const char *const TAG = "ruuvitag"; @@ -23,5 +21,3 @@ void RuuviTag::dump_config() { } } // namespace esphome::ruuvitag - -#endif diff --git a/esphome/components/ruuvitag/ruuvitag.h b/esphome/components/ruuvitag/ruuvitag.h index 9602b82afc..fc2d05a642 100644 --- a/esphome/components/ruuvitag/ruuvitag.h +++ b/esphome/components/ruuvitag/ruuvitag.h @@ -2,18 +2,16 @@ #include "esphome/core/component.h" #include "esphome/components/sensor/sensor.h" -#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" +#include "esphome/components/ble_device_base/ble_device.h" #include "esphome/components/ruuvi_ble/ruuvi_ble.h" -#ifdef USE_ESP32 - namespace esphome::ruuvitag { -class RuuviTag final : public Component, public esp32_ble_tracker::ESPBTDeviceListener { +class RuuviTag final : public Component, public ble_device_base::ESPBTDeviceListener { public: void set_address(uint64_t address) { address_ = address; } - bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override { + bool parse_device(const ble_device_base::ESPBTDevice &device) override { if (device.address_uint64() != this->address_) return false; @@ -77,5 +75,3 @@ class RuuviTag final : public Component, public esp32_ble_tracker::ESPBTDeviceLi }; } // namespace esphome::ruuvitag - -#endif diff --git a/esphome/components/ruuvitag/sensor.py b/esphome/components/ruuvitag/sensor.py index af262b2950..e58d38ca84 100644 --- a/esphome/components/ruuvitag/sensor.py +++ b/esphome/components/ruuvitag/sensor.py @@ -1,5 +1,5 @@ import esphome.codegen as cg -from esphome.components import esp32_ble_tracker, sensor +from esphome.components import ble_device_base, sensor import esphome.config_validation as cv from esphome.const import ( CONF_ACCELERATION, @@ -35,15 +35,15 @@ from esphome.const import ( UNIT_VOLT, ) -DEPENDENCIES = ["esp32_ble_tracker"] -AUTO_LOAD = ["ruuvi_ble"] +AUTO_LOAD = ["ble_device_base", "ruuvi_ble"] ruuvitag_ns = cg.esphome_ns.namespace("ruuvitag") RuuviTag = ruuvitag_ns.class_( - "RuuviTag", esp32_ble_tracker.ESPBTDeviceListener, cg.Component + "RuuviTag", ble_device_base.ESPBTDeviceListener, cg.Component ) -CONFIG_SCHEMA = ( +CONFIG_SCHEMA = cv.All( + ble_device_base.rename_legacy_hub_id("ruuvitag"), cv.Schema( { cv.GenerateID(): cv.declare_id(RuuviTag), @@ -116,15 +116,15 @@ CONFIG_SCHEMA = ( ), } ) - .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) .extend(cv.COMPONENT_SCHEMA) + .extend(ble_device_base.BLE_DEVICE_SCHEMA), ) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) - await esp32_ble_tracker.register_ble_device(var, config) + await ble_device_base.register_ble_device(var, config) cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) diff --git a/tests/components/b_parasite/common-ln.yaml b/tests/components/b_parasite/common-ln.yaml new file mode 100644 index 0000000000..797b94c76f --- /dev/null +++ b/tests/components/b_parasite/common-ln.yaml @@ -0,0 +1,7 @@ +sensor: + - platform: b_parasite + mac_address: F0:CA:F0:CA:01:01 + humidity: + name: b-parasite Air Humidity + temperature: + name: b-parasite Air Temperature diff --git a/tests/components/b_parasite/common.yaml b/tests/components/b_parasite/common.yaml index 262e891bb2..e603d058b7 100644 --- a/tests/components/b_parasite/common.yaml +++ b/tests/components/b_parasite/common.yaml @@ -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: b_parasite + ble_hub_id: ble_tracker_hub mac_address: F0:CA:F0:CA:01:01 humidity: name: b-parasite Air Humidity diff --git a/tests/components/b_parasite/test.ln882x-ard.yaml b/tests/components/b_parasite/test.ln882x-ard.yaml new file mode 100644 index 0000000000..f711f63aa7 --- /dev/null +++ b/tests/components/b_parasite/test.ln882x-ard.yaml @@ -0,0 +1,3 @@ +packages: + ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml + b_parasite: !include common-ln.yaml diff --git a/tests/components/b_parasite/validate.bk72xx-ard.yaml b/tests/components/b_parasite/validate.bk72xx-ard.yaml new file mode 100644 index 0000000000..fab4895d7b --- /dev/null +++ b/tests/components/b_parasite/validate.bk72xx-ard.yaml @@ -0,0 +1,26 @@ +# 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: b_parasite + ble_hub_id: ble_tracker_hub + mac_address: F0:CA:F0:CA:01:01 + humidity: + name: b-parasite Air Humidity + temperature: + name: b-parasite Air Temperature + moisture: + name: b-parasite Soil Moisture + battery_voltage: + name: b-parasite Battery Voltage + illuminance: + name: b-parasite Illuminance + # No ble_hub_id: exercises the generated binding real configs use. + - platform: b_parasite + mac_address: F0:CA:F0:CA:01:02 + temperature: + name: BK b-parasite Implicit Temperature diff --git a/tests/components/ruuvi_ble/common-ln.yaml b/tests/components/ruuvi_ble/common-ln.yaml new file mode 100644 index 0000000000..39e578f349 --- /dev/null +++ b/tests/components/ruuvi_ble/common-ln.yaml @@ -0,0 +1 @@ +ruuvi_ble: diff --git a/tests/components/ruuvi_ble/common.yaml b/tests/components/ruuvi_ble/common.yaml index 1f155fd8e1..0221d865ef 100644 --- a/tests/components/ruuvi_ble/common.yaml +++ b/tests/components/ruuvi_ble/common.yaml @@ -1,3 +1,6 @@ esp32_ble_tracker: + id: ble_tracker_hub +# Explicit ble_hub_id: pins the neutral binding as a declared key. ruuvi_ble: + ble_hub_id: ble_tracker_hub diff --git a/tests/components/ruuvi_ble/test.ln882x-ard.yaml b/tests/components/ruuvi_ble/test.ln882x-ard.yaml new file mode 100644 index 0000000000..5cc6a112ce --- /dev/null +++ b/tests/components/ruuvi_ble/test.ln882x-ard.yaml @@ -0,0 +1,3 @@ +packages: + ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml + ruuvi_ble: !include common-ln.yaml diff --git a/tests/components/ruuvi_ble/validate.bk72xx-ard.yaml b/tests/components/ruuvi_ble/validate.bk72xx-ard.yaml new file mode 100644 index 0000000000..c0436c0031 --- /dev/null +++ b/tests/components/ruuvi_ble/validate.bk72xx-ard.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. +ruuvi_ble: + ble_hub_id: ble_tracker_hub diff --git a/tests/components/ruuvitag/common-ln.yaml b/tests/components/ruuvitag/common-ln.yaml new file mode 100644 index 0000000000..3219624340 --- /dev/null +++ b/tests/components/ruuvitag/common-ln.yaml @@ -0,0 +1,7 @@ +sensor: + - platform: ruuvitag + mac_address: FF:56:D3:2F:7D:E8 + humidity: + name: RuuviTag Humidity + temperature: + name: RuuviTag Temperature diff --git a/tests/components/ruuvitag/common.yaml b/tests/components/ruuvitag/common.yaml index 7990617710..ce6abf5bb5 100644 --- a/tests/components/ruuvitag/common.yaml +++ b/tests/components/ruuvitag/common.yaml @@ -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: ruuvitag + ble_hub_id: ble_tracker_hub mac_address: FF:56:D3:2F:7D:E8 humidity: name: RuuviTag Humidity diff --git a/tests/components/ruuvitag/test.ln882x-ard.yaml b/tests/components/ruuvitag/test.ln882x-ard.yaml new file mode 100644 index 0000000000..9b0d8c2c58 --- /dev/null +++ b/tests/components/ruuvitag/test.ln882x-ard.yaml @@ -0,0 +1,3 @@ +packages: + ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml + ruuvitag: !include common-ln.yaml diff --git a/tests/components/ruuvitag/validate.bk72xx-ard.yaml b/tests/components/ruuvitag/validate.bk72xx-ard.yaml new file mode 100644 index 0000000000..967df8e53f --- /dev/null +++ b/tests/components/ruuvitag/validate.bk72xx-ard.yaml @@ -0,0 +1,38 @@ +# 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: ruuvitag + ble_hub_id: ble_tracker_hub + mac_address: FF:56:D3:2F:7D:E8 + humidity: + name: RuuviTag Humidity + temperature: + name: RuuviTag Temperature + pressure: + name: RuuviTag Pressure + acceleration: + name: RuuviTag Acceleration + acceleration_x: + name: RuuviTag Acceleration X + acceleration_y: + name: RuuviTag Acceleration Y + acceleration_z: + name: RuuviTag Acceleration Z + battery_voltage: + name: RuuviTag Battery Voltage + tx_power: + name: RuuviTag TX Power + movement_counter: + name: RuuviTag Movement Counter + measurement_sequence_number: + name: RuuviTag Measurement Sequence Number + # No ble_hub_id: exercises the generated binding real configs use. + - platform: ruuvitag + mac_address: FF:56:D3:2F:7D:E9 + temperature: + name: BK RuuviTag Implicit Temperature