mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 08:41:59 +00:00
[xiaomi_ble] Simplify set_bindkey using parse_hex and const char*
This commit is contained in:
@@ -63,17 +63,7 @@ bool XiaomiCGD1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiCGD1::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiCGD1::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_cgd1
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_cgd1 {
|
||||
class XiaomiCGD1 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; };
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
|
||||
@@ -63,17 +63,7 @@ bool XiaomiCGDK2::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiCGDK2::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiCGDK2::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_cgdk2
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_cgdk2 {
|
||||
class XiaomiCGDK2 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; };
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
|
||||
@@ -63,17 +63,7 @@ bool XiaomiCGG1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiCGG1::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiCGG1::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_cgg1
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_cgg1 {
|
||||
class XiaomiCGG1 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; }
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "xiaomi_cgpr1.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
@@ -59,17 +60,7 @@ bool XiaomiCGPR1::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiCGPR1::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiCGPR1::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_cgpr1
|
||||
} // namespace esphome
|
||||
|
||||
@@ -16,7 +16,7 @@ class XiaomiCGPR1 : public Component,
|
||||
public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; }
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
|
||||
|
||||
@@ -63,17 +63,7 @@ bool XiaomiLYWSD02MMC::parse_device(const esp32_ble_tracker::ESPBTDevice &device
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiLYWSD02MMC::set_bindkey(const std::string &bindkey) {
|
||||
memset(this->bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
this->bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiLYWSD02MMC::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_lywsd02mmc
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_lywsd02mmc {
|
||||
class XiaomiLYWSD02MMC : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { this->address_ = address; }
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
|
||||
|
||||
@@ -67,17 +67,7 @@ bool XiaomiLYWSD03MMC::parse_device(const esp32_ble_tracker::ESPBTDevice &device
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiLYWSD03MMC::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiLYWSD03MMC::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_lywsd03mmc
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_lywsd03mmc {
|
||||
class XiaomiLYWSD03MMC : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; };
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
|
||||
@@ -67,17 +67,7 @@ bool XiaomiMHOC401::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiMHOC401::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiMHOC401::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_mhoc401
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_mhoc401 {
|
||||
class XiaomiMHOC401 : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; };
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "xiaomi_mjyd02yla.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
@@ -62,17 +63,7 @@ bool XiaomiMJYD02YLA::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiMJYD02YLA::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiMJYD02YLA::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_mjyd02yla
|
||||
} // namespace esphome
|
||||
|
||||
@@ -16,7 +16,7 @@ class XiaomiMJYD02YLA : public Component,
|
||||
public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; }
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
|
||||
|
||||
@@ -79,17 +79,7 @@ bool XiaomiRTCGQ02LM::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiRTCGQ02LM::set_bindkey(const std::string &bindkey) {
|
||||
memset(bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiRTCGQ02LM::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_rtcgq02lm
|
||||
} // namespace esphome
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace xiaomi_rtcgq02lm {
|
||||
class XiaomiRTCGQ02LM : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { address_ = address; };
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
void dump_config() override;
|
||||
|
||||
@@ -67,17 +67,7 @@ bool XiaomiXMWSDJ04MMC::parse_device(const esp32_ble_tracker::ESPBTDevice &devic
|
||||
return success;
|
||||
}
|
||||
|
||||
void XiaomiXMWSDJ04MMC::set_bindkey(const std::string &bindkey) {
|
||||
memset(this->bindkey_, 0, 16);
|
||||
if (bindkey.size() != 32) {
|
||||
return;
|
||||
}
|
||||
char temp[3] = {0};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
strncpy(temp, &(bindkey.c_str()[i * 2]), 2);
|
||||
this->bindkey_[i] = std::strtoul(temp, nullptr, 16);
|
||||
}
|
||||
}
|
||||
void XiaomiXMWSDJ04MMC::set_bindkey(const char *bindkey) { parse_hex(bindkey, this->bindkey_, sizeof(this->bindkey_)); }
|
||||
|
||||
} // namespace xiaomi_xmwsdj04mmc
|
||||
} // namespace esphome
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace xiaomi_xmwsdj04mmc {
|
||||
class XiaomiXMWSDJ04MMC : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
|
||||
public:
|
||||
void set_address(uint64_t address) { this->address_ = address; }
|
||||
void set_bindkey(const std::string &bindkey);
|
||||
void set_bindkey(const char *bindkey);
|
||||
|
||||
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user