mirror of
https://github.com/esphome/esphome.git
synced 2026-08-22 22:26:21 +00:00
[ble_device_base] Migrate BLE sensor platforms to the neutral layer (batch 4: ruuvi_ble, ruuvitag, b_parasite) (#18161)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<RuuviParseResult> parse_ruuvi(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
optional<RuuviParseResult> 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<RuuviParseResult> 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
|
||||
|
||||
@@ -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<RuuviParseResult> parse_ruuvi(const esp32_ble_tracker::ESPBTDevice &device);
|
||||
optional<RuuviParseResult> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml
|
||||
b_parasite: !include common-ln.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
|
||||
@@ -0,0 +1 @@
|
||||
ruuvi_ble:
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml
|
||||
ruuvi_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.
|
||||
ruuvi_ble:
|
||||
ble_hub_id: ble_tracker_hub
|
||||
@@ -0,0 +1,7 @@
|
||||
sensor:
|
||||
- platform: ruuvitag
|
||||
mac_address: FF:56:D3:2F:7D:E8
|
||||
humidity:
|
||||
name: RuuviTag Humidity
|
||||
temperature:
|
||||
name: RuuviTag Temperature
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
ln882h_ble_tracker: !include ../ln882h_ble_tracker/common.yaml
|
||||
ruuvitag: !include common-ln.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
|
||||
Reference in New Issue
Block a user