mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 09:44:12 +01:00
Fix linting issues
This commit is contained in:
parent
4e4cdc92a1
commit
7942ec4e70
12 changed files with 74 additions and 69 deletions
|
@ -10,6 +10,7 @@ from esphome.const import (
|
|||
DEVICE_CLASS_SIGNAL_STRENGTH,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_DECIBEL_MILLIWATT,
|
||||
CONF_NOTIFY,
|
||||
)
|
||||
from esphome import automation
|
||||
from .. import ble_client_ns
|
||||
|
@ -18,7 +19,6 @@ DEPENDENCIES = ["ble_client"]
|
|||
|
||||
CONF_DESCRIPTOR_UUID = "descriptor_uuid"
|
||||
|
||||
CONF_NOTIFY = "notify"
|
||||
CONF_ON_NOTIFY = "on_notify"
|
||||
TYPE_CHARACTERISTIC = "characteristic"
|
||||
TYPE_RSSI = "rssi"
|
||||
|
|
|
@ -6,6 +6,7 @@ from esphome.const import (
|
|||
CONF_ID,
|
||||
CONF_TRIGGER_ID,
|
||||
CONF_SERVICE_UUID,
|
||||
CONF_NOTIFY,
|
||||
)
|
||||
from esphome import automation
|
||||
from .. import ble_client_ns
|
||||
|
@ -14,7 +15,6 @@ DEPENDENCIES = ["ble_client"]
|
|||
|
||||
CONF_DESCRIPTOR_UUID = "descriptor_uuid"
|
||||
|
||||
CONF_NOTIFY = "notify"
|
||||
CONF_ON_NOTIFY = "on_notify"
|
||||
|
||||
adv_data_t = cg.std_vector.template(cg.uint8)
|
||||
|
|
|
@ -7,6 +7,7 @@ from esphome.const import (
|
|||
CONF_UUID,
|
||||
CONF_SERVICES,
|
||||
CONF_VALUE,
|
||||
CONF_NOTIFY,
|
||||
)
|
||||
from esphome.components import esp32_ble
|
||||
from esphome.core import CORE
|
||||
|
@ -25,12 +26,11 @@ CONF_ON_WRITE = "on_write"
|
|||
CONF_CHARACTERISTICS = "characteristics"
|
||||
CONF_READ = "read"
|
||||
CONF_WRITE = "write"
|
||||
CONF_NOTIFY = "notify"
|
||||
CONF_BROADCAST = "broadcast"
|
||||
CONF_INDICATE = "indicate"
|
||||
CONF_WRITE_NO_RESPONSE = "write_no_response"
|
||||
CONF_DESCRIPTORS = "descriptors"
|
||||
CONF_VALUE_ACTION_ID = "value_action_id_"
|
||||
CONF_VALUE_ACTION_ID_ = "value_action_id_"
|
||||
|
||||
esp32_ble_server_ns = cg.esphome_ns.namespace("esp32_ble_server")
|
||||
ESPBTUUID_ns = cg.esphome_ns.namespace("esp32_ble").namespace("ESPBTUUID")
|
||||
|
@ -106,7 +106,9 @@ SERVICE_CHARACTERISTIC_SCHEMA = cv.Schema(
|
|||
cv.Optional(CONF_INDICATE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_WRITE_NO_RESPONSE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_VALUE): CHARACTERISTIC_VALUE_SCHEMA,
|
||||
cv.GenerateID(CONF_VALUE_ACTION_ID): cv.declare_id(BLECharacteristicSetValueAction),
|
||||
cv.GenerateID(CONF_VALUE_ACTION_ID): cv.declare_id(
|
||||
BLECharacteristicSetValueAction
|
||||
),
|
||||
cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list(
|
||||
DESCRIPTOR_SCHEMA
|
||||
),
|
||||
|
@ -205,7 +207,10 @@ def parse_descriptor_value(value):
|
|||
return value, value_len
|
||||
except cv.Invalid:
|
||||
pass
|
||||
return cg.std_vector.template(cg.uint8)(value), len(value) # Assume it's a list of bytes
|
||||
return (
|
||||
cg.std_vector.template(cg.uint8)(value),
|
||||
len(value),
|
||||
) # Assume it's a list of bytes
|
||||
|
||||
|
||||
def calculate_num_handles(service_config):
|
||||
|
@ -268,7 +273,7 @@ async def to_code(config):
|
|||
CONF_ID: char_conf[CONF_ID],
|
||||
CONF_VALUE: char_conf[CONF_VALUE],
|
||||
}
|
||||
value_action = await ble_server_characteristic_set_value(action_conf, char_conf[CONF_VALUE_ACTION_ID], cg.TemplateArguments(None), {})
|
||||
value_action = await ble_server_characteristic_set_value(action_conf, char_conf[CONF_VALUE_ACTION_ID_], cg.TemplateArguments(None), {})
|
||||
cg.add(value_action.play())
|
||||
for descriptor_conf in char_conf[CONF_DESCRIPTORS]:
|
||||
descriptor_value, max_length = parse_descriptor_value(descriptor_conf[CONF_VALUE])
|
||||
|
@ -291,20 +296,19 @@ async def parse_characteristic_value(value, args):
|
|||
if isinstance(value, list):
|
||||
return cg.std_vector.template(cg.uint8)(value)
|
||||
# Transform the value into a vector of bytes
|
||||
exp_value = cg.RawExpression(f'to_vector({value})')
|
||||
try:
|
||||
bool_value = cv.boolean(value)
|
||||
return cg.RawExpression(f'to_vector({"true" if bool_value else "false"})')
|
||||
return cg.RawExpression(f"to_vector({'true' if bool_value else 'false'})")
|
||||
except cv.Invalid:
|
||||
pass
|
||||
try:
|
||||
int_ = cv.uint64_t(value)
|
||||
return cg.RawExpression(f'to_vector({int_})')
|
||||
return cg.RawExpression(f"to_vector({int_})")
|
||||
except cv.Invalid:
|
||||
pass
|
||||
try:
|
||||
float_ = cv.float_(value)
|
||||
return cg.RawExpression(f'to_vector({float_})')
|
||||
return cg.RawExpression(f"to_vector({float_})")
|
||||
except cv.Invalid:
|
||||
pass
|
||||
try:
|
||||
|
@ -312,7 +316,7 @@ async def parse_characteristic_value(value, args):
|
|||
return cg.RawExpression(f'to_vector("{string_}")')
|
||||
except cv.Invalid:
|
||||
pass
|
||||
raise cv.Invalid(f"Invalid value {value}")
|
||||
raise cv.Invalid(f"Could not find type for value: {value}")
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
|
@ -350,7 +354,9 @@ async def ble_server_characteristic_notify(config, action_id, template_arg, args
|
|||
for char_conf in service_config[CONF_CHARACTERISTICS]:
|
||||
if char_conf[CONF_ID] == config[CONF_ID]:
|
||||
if not char_conf[CONF_NOTIFY]:
|
||||
raise cv.Invalid(f'Characteristic "{char_conf[CONF_ID]}" does not have the NOTIFY property set')
|
||||
raise cv.Invalid(
|
||||
f'Characteristic "{char_conf[CONF_ID]}" does not have the NOTIFY property set'
|
||||
)
|
||||
break
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
return var
|
||||
|
|
|
@ -248,7 +248,8 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
|
|||
}
|
||||
|
||||
if (!param->write.is_prep) {
|
||||
this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit(BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_);
|
||||
this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit(
|
||||
BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_);
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -259,7 +260,8 @@ void BLECharacteristic::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt
|
|||
break;
|
||||
this->write_event_ = false;
|
||||
if (param->exec_write.exec_write_flag == ESP_GATT_PREP_WRITE_EXEC) {
|
||||
this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit(BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_);
|
||||
this->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::emit(
|
||||
BLECharacteristicEvt::VectorEvt::ON_WRITE, this->value_);
|
||||
}
|
||||
esp_err_t err =
|
||||
esp_ble_gatts_send_response(gatts_if, param->write.conn_id, param->write.trans_id, ESP_GATT_OK, nullptr);
|
||||
|
|
|
@ -25,16 +25,17 @@ using namespace esp32_ble;
|
|||
class BLEService;
|
||||
|
||||
namespace BLECharacteristicEvt {
|
||||
enum VectorEvt {
|
||||
ON_WRITE,
|
||||
};
|
||||
enum VectorEvt {
|
||||
ON_WRITE,
|
||||
};
|
||||
|
||||
enum EmptyEvt {
|
||||
ON_READ,
|
||||
};
|
||||
enum EmptyEvt {
|
||||
ON_READ,
|
||||
};
|
||||
}
|
||||
|
||||
class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>, public EventEmitter<BLECharacteristicEvt::EmptyEvt> {
|
||||
class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>,
|
||||
public EventEmitter<BLECharacteristicEvt::EmptyEvt> {
|
||||
public:
|
||||
BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
|
||||
~BLECharacteristic();
|
||||
|
|
|
@ -10,18 +10,21 @@ namespace esp32_ble_server_automations {
|
|||
using namespace esp32_ble;
|
||||
|
||||
Trigger<std::vector<uint8_t>> *BLETriggers::create_on_write_trigger(BLECharacteristic *characteristic) {
|
||||
Trigger<std::vector<uint8_t>> *on_write_trigger = new Trigger<std::vector<uint8_t>>(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
characteristic->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on(BLECharacteristicEvt::VectorEvt::ON_WRITE, [on_write_trigger](const std::vector<uint8_t> &data) {
|
||||
on_write_trigger->trigger(data);
|
||||
});
|
||||
Trigger<std::vector<uint8_t>> *on_write_trigger =
|
||||
new Trigger<std::vector<uint8_t>>(); // NOLINT(cppcoreguidelines-owning-memory)
|
||||
characteristic->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on(
|
||||
BLECharacteristicEvt::VectorEvt::ON_WRITE,
|
||||
[on_write_trigger](const std::vector<uint8_t> &data) { on_write_trigger->trigger(data); });
|
||||
return on_write_trigger;
|
||||
}
|
||||
|
||||
void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *characteristic, EventEmitterListenerID listener_id) {
|
||||
void BLECharacteristicSetValueActionManager::set_listener(BLECharacteristic *characteristic,
|
||||
EventEmitterListenerID listener_id) {
|
||||
// Check if there is already a listener for this characteristic
|
||||
if (this->listeners_.find(characteristic) != this->listeners_.end()) {
|
||||
// Remove the previous listener
|
||||
characteristic->EventEmitter<BLECharacteristicEvt::EmptyEvt>::off(BLECharacteristicEvt::EmptyEvt::ON_READ, this->listeners_[characteristic]);
|
||||
characteristic->EventEmitter<BLECharacteristicEvt::EmptyEvt>::off(BLECharacteristicEvt::EmptyEvt::ON_READ,
|
||||
this->listeners_[characteristic]);
|
||||
}
|
||||
this->listeners_[characteristic] = listener_id;
|
||||
}
|
||||
|
|
|
@ -39,26 +39,27 @@ class BLECharacteristicSetValueActionManager {
|
|||
|
||||
template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
|
||||
public:
|
||||
BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
|
||||
TEMPLATABLE_VALUE(std::vector<uint8_t>, value)
|
||||
void play(Ts... x) override {
|
||||
BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
|
||||
TEMPLATABLE_VALUE(std::vector<uint8_t>, value)
|
||||
void play(Ts... x) override {
|
||||
// If the listener is already set, do nothing
|
||||
if (BLECharacteristicSetValueActionManager::get_instance()->get_listener(this->parent_) == this->listener_id_)
|
||||
return;
|
||||
// Set initial value
|
||||
this->parent_->set_value(this->value_.value(x...));
|
||||
// Set the listener for read events
|
||||
this->listener_id_ = this->parent_->EventEmitter<BLECharacteristicEvt::EmptyEvt>::on(BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](void) {
|
||||
// Set the value of the characteristic every time it is read
|
||||
this->parent_->set_value(this->value_.value(x...));
|
||||
this->listener_id_ = this->parent_->EventEmitter<BLECharacteristicEvt::EmptyEvt>::on(
|
||||
BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](void) {
|
||||
// Set the value of the characteristic every time it is read
|
||||
this->parent_->set_value(this->value_.value(x...));
|
||||
});
|
||||
// Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
|
||||
BLECharacteristicSetValueActionManager::get_instance()->set_listener(this->parent_, this->listener_id_);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
BLECharacteristic *parent_;
|
||||
EventEmitterListenerID listener_id_;
|
||||
BLECharacteristic *parent_;
|
||||
EventEmitterListenerID listener_id_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
|
||||
|
|
|
@ -40,10 +40,11 @@ void ESP32ImprovComponent::setup_characteristics() {
|
|||
this->error_->add_descriptor(error_descriptor);
|
||||
|
||||
this->rpc_ = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE);
|
||||
this->rpc_->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on(BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &data) {
|
||||
if (!data.empty()) {
|
||||
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
|
||||
}
|
||||
this->rpc_->EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>::on(
|
||||
BLECharacteristicEvt::VectorEvt::ON_WRITE, [this](const std::vector<uint8_t> &data) {
|
||||
if (!data.empty()) {
|
||||
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
|
||||
}
|
||||
});
|
||||
BLEDescriptor *rpc_descriptor = new BLE2902();
|
||||
this->rpc_->add_descriptor(rpc_descriptor);
|
||||
|
|
|
@ -509,6 +509,7 @@ CONF_NEC = "nec"
|
|||
CONF_NETWORKS = "networks"
|
||||
CONF_NEW_PASSWORD = "new_password"
|
||||
CONF_NOISE_LEVEL = "noise_level"
|
||||
CONF_NOTIFY = "notify"
|
||||
CONF_NUM_ATTEMPTS = "num_attempts"
|
||||
CONF_NUM_CHANNELS = "num_channels"
|
||||
CONF_NUM_CHIPS = "num_chips"
|
||||
|
|
|
@ -9,33 +9,33 @@ namespace esphome {
|
|||
|
||||
using EventEmitterListenerID = uint32_t;
|
||||
|
||||
// EventEmitter class that can emit events with a specific name (usually an enum) and a list of arguments.
|
||||
// EventEmitter class that can emit events with a specific name (it is highly recommended to use an enum class for this) and a list of arguments.
|
||||
// Supports multiple listeners for each event.
|
||||
template <typename EvtNames, typename... Args>
|
||||
class EventEmitter {
|
||||
public:
|
||||
EventEmitterListenerID on(EvtNames event, std::function<void(Args...)> listener) {
|
||||
listeners_[event].emplace_back(++current_id_, [listener](Args... args) {
|
||||
listener(args...);
|
||||
});
|
||||
listeners_[event].emplace_back(++current_id_, [listener](Args... args) { listener(args...); });
|
||||
return current_id_;
|
||||
}
|
||||
|
||||
void off(EvtNames event, EventEmitterListenerID id) {
|
||||
if (this->listeners_.count(event) == 0)
|
||||
return;
|
||||
return;
|
||||
auto &vec = this->listeners_[event];
|
||||
vec.erase(std::remove_if(vec.begin(), vec.end(), [id](const std::pair<EventEmitterListenerID, std::function<void(Args...)>> &pair) {
|
||||
return pair.first == id;
|
||||
}), vec.end());
|
||||
vec.erase(std::remove_if(vec.begin(), vec.end(),
|
||||
[id](const std::pair<EventEmitterListenerID, std::function<void(Args...)>> &pair) {
|
||||
return pair.first == id;
|
||||
}),
|
||||
vec.end());
|
||||
}
|
||||
|
||||
protected:
|
||||
void emit(EvtNames event, Args... args) {
|
||||
if (listeners_.count(event) == 0)
|
||||
return;
|
||||
return;
|
||||
for (const auto &listener : listeners_[event]) {
|
||||
listener.second(args...);
|
||||
listener.second(args...);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -536,29 +536,19 @@ std::vector<uint8_t> base64_decode(const std::string &encoded_string) {
|
|||
|
||||
std::vector<uint8_t> to_vector(bool value) { return {value ? (uint8_t)1 : (uint8_t)0}; }
|
||||
std::vector<uint8_t> to_vector(uint8_t value) { return {value}; }
|
||||
std::vector<uint8_t> to_vector(uint16_t value) {
|
||||
return {uint8_t(value >> 8), uint8_t(value & 0xFF)};
|
||||
}
|
||||
std::vector<uint8_t> to_vector(uint16_t value) { return {uint8_t(value >> 8), uint8_t(value & 0xFF)}; }
|
||||
std::vector<uint8_t> to_vector(uint32_t value) {
|
||||
return {uint8_t(value >> 24), uint8_t((value >> 16) & 0xFF), uint8_t((value >> 8) & 0xFF), uint8_t(value & 0xFF)};
|
||||
}
|
||||
std::vector<uint8_t> to_vector(uint64_t value) {
|
||||
return {uint8_t(value >> 56), uint8_t((value >> 48) & 0xFF), uint8_t((value >> 40) & 0xFF),
|
||||
return {uint8_t(value >> 56), uint8_t((value >> 48) & 0xFF), uint8_t((value >> 40) & 0xFF),
|
||||
uint8_t((value >> 32) & 0xFF), uint8_t((value >> 24) & 0xFF), uint8_t((value >> 16) & 0xFF),
|
||||
uint8_t((value >> 8) & 0xFF), uint8_t(value & 0xFF)};
|
||||
}
|
||||
std::vector<uint8_t> to_vector(int value) {
|
||||
return to_vector(static_cast<uint32_t>(value));
|
||||
}
|
||||
std::vector<uint8_t> to_vector(float value) {
|
||||
return to_vector(*reinterpret_cast<uint32_t *>(&value));
|
||||
}
|
||||
std::vector<uint8_t> to_vector(double value) {
|
||||
return to_vector(*reinterpret_cast<uint64_t *>(&value));
|
||||
}
|
||||
std::vector<uint8_t> to_vector(const std::string &value) {
|
||||
return std::vector<uint8_t>(value.begin(), value.end());
|
||||
uint8_t((value >> 8) & 0xFF), uint8_t(value & 0xFF)};
|
||||
}
|
||||
std::vector<uint8_t> to_vector(int value) { return to_vector(static_cast<uint32_t>(value)); }
|
||||
std::vector<uint8_t> to_vector(float value) { return to_vector(*reinterpret_cast<uint32_t *>(&value)); }
|
||||
std::vector<uint8_t> to_vector(double value) { return to_vector(*reinterpret_cast<uint64_t *>(&value)); }
|
||||
std::vector<uint8_t> to_vector(const std::string &value) { return std::vector<uint8_t>(value.begin(), value.end()); }
|
||||
|
||||
// Colors
|
||||
|
||||
|
|
|
@ -441,7 +441,7 @@ std::string base64_encode(const std::vector<uint8_t> &buf);
|
|||
std::vector<uint8_t> base64_decode(const std::string &encoded_string);
|
||||
size_t base64_decode(std::string const &encoded_string, uint8_t *buf, size_t buf_len);
|
||||
|
||||
/// Create a byte vector multiple types of values.
|
||||
/// Create a byte vector from multiple types of values.
|
||||
std::vector<uint8_t> to_vector(bool value);
|
||||
std::vector<uint8_t> to_vector(uint8_t value);
|
||||
std::vector<uint8_t> to_vector(uint16_t value);
|
||||
|
|
Loading…
Reference in a new issue