diff --git a/components/myhomeiot_ble_client/__init__.py b/components/myhomeiot_ble_client/__init__.py index b8ec006..8fd1573 100644 --- a/components/myhomeiot_ble_client/__init__.py +++ b/components/myhomeiot_ble_client/__init__.py @@ -50,7 +50,7 @@ def versiontuple(v): return tuple(map(int, (v.split(".")))) -def to_code(config): +async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) reversed = versiontuple(const.__version__) >= versiontuple("2021.9.0") @@ -70,10 +70,10 @@ def to_code(config): uuid128 = esp32_ble_tracker.as_reversed_hex_array(config[CONF_CHARACTERISTIC_UUID]) if reversed else esp32_ble_tracker.as_hex_array(config[CONF_CHARACTERISTIC_UUID]) cg.add(var.set_char_uuid128(uuid128)) - yield cg.register_component(var, config) - yield myhomeiot_ble_host.register_ble_client(var, config) + await cg.register_component(var, config) + await myhomeiot_ble_host.register_ble_client(var, config) cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) for conf in config.get(CONF_ON_VALUE, []): trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var) - yield automation.build_automation(trigger, [(cg.std_vector.template(cg.uint8), "x")], conf) + await automation.build_automation(trigger, [(cg.std_vector.template(cg.uint8), "x")], conf) diff --git a/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp b/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp index 10a43b0..aa55e23 100644 --- a/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp +++ b/components/myhomeiot_ble_client/myhomeiot_ble_client.cpp @@ -8,10 +8,10 @@ namespace esphome { namespace myhomeiot_ble_client { -static const char *TAG = "myhomeiot_ble_client"; +static const char *const TAG = "myhomeiot_ble_client"; void MyHomeIOT_BLEClient::setup() { - this->state_ = esp32_ble_tracker::ClientState::Idle; + this->state_ = MYHOMEIOT_IDLE; } void MyHomeIOT_BLEClient::dump_config() { @@ -23,25 +23,25 @@ void MyHomeIOT_BLEClient::dump_config() { } void MyHomeIOT_BLEClient::loop() { - if (this->state_ == esp32_ble_tracker::ClientState::Discovered) + if (this->state_ == MYHOMEIOT_DISCOVERED) this->connect(); - else if (this->state_ == esp32_ble_tracker::ClientState::Established) + else if (this->state_ == MYHOMEIOT_ESTABLISHED) this->disconnect(); } void MyHomeIOT_BLEClient::connect() { ESP_LOGI(TAG, "[%s] Connecting", to_string(this->address).c_str()); - this->state_ = esp32_ble_tracker::ClientState::Connecting; + this->state_ = MYHOMEIOT_CONNECTING; if (auto status = esp_ble_gattc_open(ble_host_->gattc_if, this->remote_bda, BLE_ADDR_TYPE_PUBLIC, true)) { ESP_LOGW(TAG, "[%s] open error, status (%d)", to_string(this->address).c_str(), status); - report_error(esp32_ble_tracker::ClientState::Idle); + report_error(MYHOMEIOT_IDLE); } } void MyHomeIOT_BLEClient::disconnect() { ESP_LOGI(TAG, "[%s] Disconnecting", to_string(this->address).c_str()); - this->state_ = esp32_ble_tracker::ClientState::Idle; + this->state_ = MYHOMEIOT_IDLE; if (auto status = esp_ble_gattc_close(ble_host_->gattc_if, this->conn_id)) ESP_LOGW(TAG, "[%s] close error, status (%d)", to_string(this->address).c_str(), status); } @@ -63,13 +63,13 @@ void MyHomeIOT_BLEClient::report_error(esp32_ble_tracker::ClientState state) { } bool MyHomeIOT_BLEClient::parse_device(const esp32_ble_tracker::ESPBTDevice &device) { - if (!this->is_update_requested || this->state_ != esp32_ble_tracker::ClientState::Idle + if (!this->is_update_requested || this->state_ != MYHOMEIOT_IDLE || device.address_uint64() != this->address) return false; ESP_LOGD(TAG, "[%s] Found device", device.address_str().c_str()); memcpy(this->remote_bda, device.address(), sizeof(this->remote_bda)); - this->state_ = esp32_ble_tracker::ClientState::Discovered; + this->state_ = MYHOMEIOT_DISCOVERED; return true; } @@ -82,7 +82,7 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga if (param->open.status != ESP_GATT_OK) { ESP_LOGW(TAG, "[%s] OPEN_EVT failed, status (%d), app_id (%d)", to_string(this->address).c_str(), param->open.status, ble_host_->app_id); - report_error(esp32_ble_tracker::ClientState::Idle); + report_error(MYHOMEIOT_IDLE); break; } ESP_LOGI(TAG, "[%s] Connected successfully, app_id (%d)", to_string(this->address).c_str(), ble_host_->app_id); @@ -94,7 +94,7 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga break; } this->start_handle = this->end_handle = this->char_handle = ESP_GATT_ILLEGAL_HANDLE; - this->state_ = esp32_ble_tracker::ClientState::Connected; + this->state_ = MYHOMEIOT_CONNECTED; break; } case ESP_GATTC_CFG_MTU_EVT: { @@ -117,7 +117,7 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga if (memcmp(param->disconnect.remote_bda, this->remote_bda, sizeof(this->remote_bda)) != 0) break; ESP_LOGD(TAG, "[%s] DISCONNECT_EVT", to_string(this->address).c_str()); - this->state_ = esp32_ble_tracker::ClientState::Idle; + this->state_ = MYHOMEIOT_IDLE; break; } case ESP_GATTC_SEARCH_RES_EVT: { @@ -198,7 +198,7 @@ void MyHomeIOT_BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga } report_results(param->read.value, param->read.value_len); - this->state_ = esp32_ble_tracker::ClientState::Established; + this->state_ = MYHOMEIOT_ESTABLISHED; break; } default: diff --git a/components/myhomeiot_ble_client/myhomeiot_ble_client.h b/components/myhomeiot_ble_client/myhomeiot_ble_client.h index cbc54d9..de87381 100644 --- a/components/myhomeiot_ble_client/myhomeiot_ble_client.h +++ b/components/myhomeiot_ble_client/myhomeiot_ble_client.h @@ -49,7 +49,7 @@ class MyHomeIOT_BLEClient : public PollingComponent, public myhomeiot_ble_host:: void disconnect(); void update() override; void report_results(uint8_t *data, uint16_t len); - void report_error(esp32_ble_tracker::ClientState state = esp32_ble_tracker::ClientState::Established); + void report_error(esp32_ble_tracker::ClientState state = MYHOMEIOT_ESTABLISHED); }; } // namespace myhomeiot_ble_client diff --git a/components/myhomeiot_ble_host/__init__.py b/components/myhomeiot_ble_host/__init__.py index 85e34be..673a91a 100644 --- a/components/myhomeiot_ble_host/__init__.py +++ b/components/myhomeiot_ble_host/__init__.py @@ -32,13 +32,11 @@ } ) -@coroutine -def register_ble_client(var, config): - parent = yield cg.get_variable(config[CONF_BLE_HOST_ID]) +async def register_ble_client(var, config): + parent = await cg.get_variable(config[CONF_BLE_HOST_ID]) cg.add(parent.register_ble_client(var)) -def to_code(config): +async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) - - yield cg.register_component(var, config) - yield esp32_ble_tracker.register_client(var, config) + await cg.register_component(var, config) + await esp32_ble_tracker.register_client(var, config) diff --git a/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp b/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp index 6218cb8..cc3b37b 100644 --- a/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp +++ b/components/myhomeiot_ble_host/myhomeiot_ble_host.cpp @@ -8,7 +8,7 @@ namespace esphome { namespace myhomeiot_ble_host { -static const char *TAG = "myhomeiot_ble_host"; +static const char *const TAG = "myhomeiot_ble_host"; void MyHomeIOT_BLEHost::setup() { auto status = esp_ble_gattc_app_register(this->app_id); @@ -16,7 +16,7 @@ void MyHomeIOT_BLEHost::setup() { ESP_LOGE(TAG, "app_register failed, app_id (%d) status (%d)", this->app_id, status); this->mark_failed(); } - this->set_state(esp32_ble_tracker::ClientState::Idle); + this->set_state(MYHOMEIOT_IDLE); } void MyHomeIOT_BLEHost::dump_config() { @@ -30,7 +30,7 @@ void MyHomeIOT_BLEHost::loop() { if (this->state() != current->state()) this->set_state(current->state()); } - if (this->state() == esp32_ble_tracker::ClientState::Idle) + if (this->state() == MYHOMEIOT_IDLE) current = nullptr; } diff --git a/components/myhomeiot_ble_host/myhomeiot_ble_host.h b/components/myhomeiot_ble_host/myhomeiot_ble_host.h index f0c851d..3fb859c 100644 --- a/components/myhomeiot_ble_host/myhomeiot_ble_host.h +++ b/components/myhomeiot_ble_host/myhomeiot_ble_host.h @@ -5,6 +5,26 @@ #ifdef ARDUINO_ARCH_ESP32 +#include "esphome/core/version.h" +#if __has_include("esphome/core/macros.h") +#include "esphome/core/macros.h" // VERSION_CODE +#else +#define VERSION_CODE(major, minor, patch) ((major) << 16 | (minor) << 8 | (patch)) +#endif +#if ESPHOME_VERSION_CODE >= VERSION_CODE(2021, 10, 0) +#define MYHOMEIOT_IDLE esp32_ble_tracker::ClientState::IDLE +#define MYHOMEIOT_DISCOVERED esp32_ble_tracker::ClientState::DISCOVERED +#define MYHOMEIOT_CONNECTING esp32_ble_tracker::ClientState::CONNECTING +#define MYHOMEIOT_CONNECTED esp32_ble_tracker::ClientState::CONNECTED +#define MYHOMEIOT_ESTABLISHED esp32_ble_tracker::ClientState::ESTABLISHED +#else +#define MYHOMEIOT_IDLE esp32_ble_tracker::ClientState::Idle +#define MYHOMEIOT_DISCOVERED esp32_ble_tracker::ClientState::Discovered +#define MYHOMEIOT_CONNECTING esp32_ble_tracker::ClientState::Connecting +#define MYHOMEIOT_CONNECTED esp32_ble_tracker::ClientState::Connected +#define MYHOMEIOT_ESTABLISHED esp32_ble_tracker::ClientState::Established +#endif + namespace esphome { namespace myhomeiot_ble_host { @@ -26,6 +46,7 @@ class MyHomeIOT_BLEClientNode { class MyHomeIOT_BLEHost : public Component, public esp32_ble_tracker::ESPBTClient { public: + float get_setup_priority() const override { return setup_priority::AFTER_BLUETOOTH; } void setup() override; void dump_config() override; void loop() override; @@ -38,7 +59,8 @@ class MyHomeIOT_BLEHost : public Component, public esp32_ble_tracker::ESPBTClien int gattc_if; bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override; - void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param); + void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, + esp_ble_gattc_cb_param_t *param) override; void on_scan_end() override {} protected: