[ble_device_base] Deprecate address_str in favor of address_str_to (#18092)

This commit is contained in:
J. Nick Koston
2026-08-05 18:33:32 +00:00
committed by GitHub
parent fc682dc75b
commit 202b31f711
8 changed files with 26 additions and 11 deletions
@@ -129,7 +129,7 @@ void ESPBTDevice::parse_scan_rst(const esp32_ble::BLEScanResult &scan_result) {
this->scan_result_ = &scan_result; this->scan_result_ = &scan_result;
// BLEScanResult's bda is most-significant octet first; the neutral ingest // BLEScanResult's bda is most-significant octet first; the neutral ingest
// takes the BLE controller (LSB-first) order, so reverse — address_uint64()/ // takes the BLE controller (LSB-first) order, so reverse — address_uint64()/
// address_str() then produce exactly the historical esp32 values. // address_str_to() then produce exactly the historical esp32 values.
uint8_t mac_lsb_first[6]; uint8_t mac_lsb_first[6];
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
mac_lsb_first[i] = scan_result.bda[5 - i]; mac_lsb_first[i] = scan_result.bda[5 - i];
@@ -346,6 +346,7 @@ void ESPBTDevice::from_scan_result(const uint8_t *mac, int rssi, uint8_t addr_ty
#endif // ESPHOME_LOG_HAS_VERY_VERBOSE #endif // ESPHOME_LOG_HAS_VERY_VERBOSE
} }
// Remove before 2027.2.0
std::string ESPBTDevice::address_str() const { std::string ESPBTDevice::address_str() const {
char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE]; char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
return std::string(this->address_str_to(buf)); return std::string(this->address_str_to(buf));
@@ -172,8 +172,9 @@ class ESPBTDevice {
static constexpr size_t MAC_ADDRESS_PRETTY_BUFFER_SIZE = esphome::MAC_ADDRESS_PRETTY_BUFFER_SIZE; static constexpr size_t MAC_ADDRESS_PRETTY_BUFFER_SIZE = esphome::MAC_ADDRESS_PRETTY_BUFFER_SIZE;
/// Return MAC as "XX:XX:XX:XX:XX:XX" string. /// Return MAC as "XX:XX:XX:XX:XX:XX" string.
ESPDEPRECATED("Use address_str_to() instead. Removed in 2027.2.0.", "2026.8.0")
std::string address_str() const; std::string address_str() const;
/// Buffer overload: writes "XX:XX:XX:XX:XX:XX\0" into buf (>= 18 bytes), returns buf. /// Writes "XX:XX:XX:XX:XX:XX\0" into buf (>= MAC_ADDRESS_PRETTY_BUFFER_SIZE bytes), returns buf.
const char *address_str_to(char *buf) const; const char *address_str_to(char *buf) const;
#if defined(__cpp_lib_span) #if defined(__cpp_lib_span)
const char *address_str_to(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) const { const char *address_str_to(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) const {
@@ -20,7 +20,7 @@ bk72xx_ble_tracker:
- AC:37:43:77:5F:4C - AC:37:43:77:5F:4C
- 11:22:33:44:55:66 - 11:22:33:44:55:66
then: then:
- lambda: 'ESP_LOGD("t", "%s", x.address_str().c_str());' - lambda: 'char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE]; ESP_LOGD("t", "%s", x.address_str_to(addr));'
on_ble_service_data_advertise: on_ble_service_data_advertise:
- service_uuid: ABCDABCD-ABCD-ABCD-ABCD-ABCDABCDABCD - service_uuid: ABCDABCD-ABCD-ABCD-ABCD-ABCDABCDABCD
mac_address: AC:37:43:77:5F:4C mac_address: AC:37:43:77:5F:4C
@@ -20,7 +20,7 @@ ln882h_ble_tracker:
- AC:37:43:77:5F:4C - AC:37:43:77:5F:4C
- 11:22:33:44:55:66 - 11:22:33:44:55:66
then: then:
- lambda: 'ESP_LOGD("t", "%s", x.address_str().c_str());' - lambda: 'char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE]; ESP_LOGD("t", "%s", x.address_str_to(addr));'
on_ble_service_data_advertise: on_ble_service_data_advertise:
- service_uuid: ABCDABCD-ABCD-ABCD-ABCD-ABCDABCDABCD - service_uuid: ABCDABCD-ABCD-ABCD-ABCD-ABCDABCDABCD
mac_address: AC:37:43:77:5F:4C mac_address: AC:37:43:77:5F:4C
@@ -15,13 +15,15 @@ bk72xx_ble_tracker:
- mac_address: AC:37:43:77:5F:4C - mac_address: AC:37:43:77:5F:4C
then: then:
- lambda: |- - lambda: |-
ESP_LOGD("main", "The device address is %s", x.address_str().c_str()); char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGD("main", "The device address is %s", x.address_str_to(addr));
- mac_address: - mac_address:
- AC:37:43:77:5F:4C - AC:37:43:77:5F:4C
- AC:37:43:77:5F:4D - AC:37:43:77:5F:4D
then: then:
- lambda: |- - lambda: |-
ESP_LOGD("main", "The device address is %s", x.address_str().c_str()); char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGD("main", "The device address is %s", x.address_str_to(addr));
on_ble_service_data_advertise: on_ble_service_data_advertise:
- service_uuid: ABCD - service_uuid: ABCD
# mac_address exercises the UUID triggers' set_address() codegen branch. # mac_address exercises the UUID triggers' set_address() codegen branch.
@@ -8,7 +8,7 @@ namespace esphome::ble_device_base::testing {
// from_scan_result() ingests BLE controller order (LSB-first); the public // from_scan_result() ingests BLE controller order (LSB-first); the public
// accessors must expose the historical esp32 semantics: address() in printable // accessors must expose the historical esp32 semantics: address() in printable
// (MSB-first) order, address_uint64() with byte 0 in the LSB, address_str() // (MSB-first) order, address_uint64() with byte 0 in the LSB, address_str_to()
// printed MSB-first. // printed MSB-first.
namespace { namespace {
// Device AA:BB:CC:DD:EE:FF — controller order delivers FF first. // Device AA:BB:CC:DD:EE:FF — controller order delivers FF first.
@@ -25,7 +25,14 @@ TEST(BleDeviceAddress, AccessorsMatchEsp32Semantics) {
EXPECT_EQ(device.address_uint64(), 0xAABBCCDDEEFFULL); EXPECT_EQ(device.address_uint64(), 0xAABBCCDDEEFFULL);
char buf[ESPBTDevice::MAC_ADDRESS_PRETTY_BUFFER_SIZE];
EXPECT_STREQ(device.address_str_to(buf), "AA:BB:CC:DD:EE:FF");
// The deprecated wrapper must keep returning the same string until its 2027.2.0 removal.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
EXPECT_EQ(device.address_str(), "AA:BB:CC:DD:EE:FF"); EXPECT_EQ(device.address_str(), "AA:BB:CC:DD:EE:FF");
#pragma GCC diagnostic pop
} }
// mac_lsb_first_to_uint64() packs the controller-order bytes a raw-advertisement // mac_lsb_first_to_uint64() packs the controller-order bytes a raw-advertisement
@@ -12,18 +12,21 @@ esp32_ble_tracker:
then: then:
# yamllint disable rule:line-length # yamllint disable rule:line-length
- lambda: !lambda |- - lambda: !lambda |-
ESP_LOGD("main", "The device address (%s) exists in list", x.address_str().c_str()); char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGD("main", "The device address (%s) exists in list", x.address_str_to(addr));
# yamllint enable rule:line-length # yamllint enable rule:line-length
- mac_address: AC:37:43:77:5F:4C - mac_address: AC:37:43:77:5F:4C
then: then:
# yamllint disable rule:line-length # yamllint disable rule:line-length
- lambda: !lambda |- - lambda: !lambda |-
ESP_LOGD("main", "The device address is %s", x.address_str().c_str()); char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGD("main", "The device address is %s", x.address_str_to(addr));
# yamllint enable rule:line-length # yamllint enable rule:line-length
- then: - then:
# yamllint disable rule:line-length # yamllint disable rule:line-length
- lambda: !lambda |- - lambda: !lambda |-
ESP_LOGD("main", "The device address is %s", x.address_str().c_str()); char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGD("main", "The device address is %s", x.address_str_to(addr));
# yamllint enable rule:line-length # yamllint enable rule:line-length
on_ble_service_data_advertise: on_ble_service_data_advertise:
- service_uuid: ABCD - service_uuid: ABCD
@@ -16,7 +16,8 @@ ln882h_ble_tracker:
- mac_address: AC:37:43:77:5F:4C - mac_address: AC:37:43:77:5F:4C
then: then:
- lambda: |- - lambda: |-
ESP_LOGD("main", "The device address is %s", x.address_str().c_str()); char addr[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
ESP_LOGD("main", "The device address is %s", x.address_str_to(addr));
on_ble_service_data_advertise: on_ble_service_data_advertise:
- service_uuid: ABCD - service_uuid: ABCD
then: then: