mirror of
https://github.com/esphome/esphome.git
synced 2024-11-24 16:08:10 +01:00
Fix linting errors
This commit is contained in:
parent
7942ec4e70
commit
4be588c749
8 changed files with 78 additions and 44 deletions
|
@ -106,12 +106,10 @@ 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(
|
||||
cv.GenerateID(CONF_VALUE_ACTION_ID_): cv.declare_id(
|
||||
BLECharacteristicSetValueAction
|
||||
),
|
||||
cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list(
|
||||
DESCRIPTOR_SCHEMA
|
||||
),
|
||||
cv.Optional(CONF_DESCRIPTORS, default=[]): cv.ensure_list(DESCRIPTOR_SCHEMA),
|
||||
cv.Optional(CONF_ON_WRITE): automation.validate_automation(
|
||||
{cv.GenerateID(): cv.declare_id(BLECharacteristic)}, single=True
|
||||
),
|
||||
|
@ -262,7 +260,9 @@ async def to_code(config):
|
|||
if CONF_ON_WRITE in char_conf:
|
||||
on_write_conf = char_conf[CONF_ON_WRITE]
|
||||
if not char_conf[CONF_WRITE] and not char_conf[CONF_WRITE_NO_RESPONSE]:
|
||||
raise cv.Invalid(f"on_write requires the {CONF_WRITE} or {CONF_WRITE_NO_RESPONSE} property to be set")
|
||||
raise cv.Invalid(
|
||||
f"on_write requires the {CONF_WRITE} or {CONF_WRITE_NO_RESPONSE} property to be set"
|
||||
)
|
||||
await automation.build_automation(
|
||||
BLETriggers_ns.create_on_write_trigger(char_var),
|
||||
[(cg.std_vector.template(cg.uint8), "x")],
|
||||
|
@ -273,10 +273,17 @@ 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])
|
||||
descriptor_value, max_length = parse_descriptor_value(
|
||||
descriptor_conf[CONF_VALUE]
|
||||
)
|
||||
desc_var = cg.new_Pvariable(
|
||||
descriptor_conf[CONF_ID],
|
||||
parse_uuid(descriptor_conf[CONF_UUID]),
|
||||
|
@ -284,6 +291,7 @@ async def to_code(config):
|
|||
)
|
||||
if CONF_VALUE in descriptor_conf:
|
||||
cg.add(desc_var.set_value(descriptor_value))
|
||||
cg.add(char_var.add_descriptor(desc_var))
|
||||
cg.add(var.enqueue_start_service(service_var))
|
||||
cg.add_define("USE_ESP32_BLE_SERVER")
|
||||
if CORE.using_esp_idf:
|
||||
|
@ -292,7 +300,12 @@ async def to_code(config):
|
|||
|
||||
async def parse_characteristic_value(value, args):
|
||||
if isinstance(value, cv.Lambda):
|
||||
return await cg.templatable(value, args, cg.std_vector.template(cg.uint8), cg.std_vector.template(cg.uint8))
|
||||
return await cg.templatable(
|
||||
value,
|
||||
args,
|
||||
cg.std_vector.template(cg.uint8),
|
||||
cg.std_vector.template(cg.uint8),
|
||||
)
|
||||
if isinstance(value, list):
|
||||
return cg.std_vector.template(cg.uint8)(value)
|
||||
# Transform the value into a vector of bytes
|
||||
|
|
|
@ -32,7 +32,7 @@ enum VectorEvt {
|
|||
enum EmptyEvt {
|
||||
ON_READ,
|
||||
};
|
||||
}
|
||||
} // namespace BLECharacteristicEvt
|
||||
|
||||
class BLECharacteristic : public EventEmitter<BLECharacteristicEvt::VectorEvt, std::vector<uint8_t>>,
|
||||
public EventEmitter<BLECharacteristicEvt::EmptyEvt> {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#pragma once
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
|
@ -11,27 +11,22 @@ using EventEmitterListenerID = uint32_t;
|
|||
|
||||
// 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 {
|
||||
template <typename EvtType, 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...); });
|
||||
return current_id_;
|
||||
}
|
||||
EventEmitterListenerID on(EvtType event, std::function<void(Args...)> listener) {
|
||||
EventEmitterListenerID listener_id = get_next_id(event);
|
||||
listeners_[event][listener_id] = listener;
|
||||
return listener_id;
|
||||
}
|
||||
|
||||
void off(EvtNames event, EventEmitterListenerID id) {
|
||||
if (this->listeners_.count(event) == 0)
|
||||
void off(EvtType event, EventEmitterListenerID id) {
|
||||
if (listeners_.count(event) == 0)
|
||||
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());
|
||||
listeners_[event].erase(id);
|
||||
}
|
||||
|
||||
protected:
|
||||
void emit(EvtNames event, Args... args) {
|
||||
void emit(EvtType event, Args... args) {
|
||||
if (listeners_.count(event) == 0)
|
||||
return;
|
||||
for (const auto &listener : listeners_[event]) {
|
||||
|
@ -39,8 +34,26 @@ class EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
EventEmitterListenerID get_next_id(EvtType event) {
|
||||
// Check if the map is full
|
||||
if (listeners_[event].size() == std::numeric_limits<EventEmitterListenerID>::max()) {
|
||||
// Raise an error if the map is full
|
||||
ESP_LOGE("event_emitter", "EventEmitter has reached the maximum number of listeners for event %d", event);
|
||||
ESP_LOGW("event_emitter", "Removing listener with ID %d for event %d", 0, event);
|
||||
off(event, 0);
|
||||
return 0;
|
||||
}
|
||||
// Get the next ID for the given event.
|
||||
EventEmitterListenerID next_id = (current_id_ + 1) % std::numeric_limits<EventEmitterListenerID>::max();
|
||||
while (listeners_[event].count(next_id) > 0) {
|
||||
next_id = (next_id + 1) % std::numeric_limits<EventEmitterListenerID>::max();
|
||||
}
|
||||
current_id_ = next_id;
|
||||
return current_id_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<EvtNames, std::vector<std::pair<EventEmitterListenerID, std::function<void(Args...)>>>> listeners_;
|
||||
std::unordered_map<EvtType, std::unordered_map<EventEmitterListenerID, std::function<void(Args...)>>> listeners_;
|
||||
EventEmitterListenerID current_id_ = 0;
|
||||
};
|
||||
|
||||
|
|
|
@ -534,7 +534,7 @@ std::vector<uint8_t> base64_decode(const std::string &encoded_string) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> to_vector(bool value) { return {value ? (uint8_t)1 : (uint8_t)0}; }
|
||||
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(uint32_t value) {
|
||||
|
@ -546,8 +546,16 @@ std::vector<uint8_t> to_vector(uint64_t value) {
|
|||
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(float value) {
|
||||
uint32_t val;
|
||||
memcpy(&val, &value, sizeof(val));
|
||||
return to_vector(val);
|
||||
}
|
||||
std::vector<uint8_t> to_vector(double value) {
|
||||
uint64_t val;
|
||||
memcpy(&val, &value, sizeof(val));
|
||||
return to_vector(val);
|
||||
}
|
||||
std::vector<uint8_t> to_vector(const std::string &value) { return std::vector<uint8_t>(value.begin(), value.end()); }
|
||||
|
||||
// Colors
|
||||
|
|
Loading…
Reference in a new issue