Skip to content

Commit

Permalink
Added support for ESPHome 2021.10.x and older
Browse files Browse the repository at this point in the history
  • Loading branch information
myhomeiot committed Nov 30, 2021
1 parent 041bddd commit d3ee1d6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 29 deletions.
8 changes: 4 additions & 4 deletions components/myhomeiot_ble_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
26 changes: 13 additions & 13 deletions components/myhomeiot_ble_client/myhomeiot_ble_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
Expand All @@ -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;
}

Expand All @@ -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);
Expand All @@ -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: {
Expand All @@ -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: {
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion components/myhomeiot_ble_client/myhomeiot_ble_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions components/myhomeiot_ble_host/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions components/myhomeiot_ble_host/myhomeiot_ble_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
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);
if (status) {
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() {
Expand All @@ -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;
}

Expand Down
24 changes: 23 additions & 1 deletion components/myhomeiot_ble_host/myhomeiot_ble_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand All @@ -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:
Expand Down

0 comments on commit d3ee1d6

Please sign in to comment.