mirror of
https://github.com/esphome/esphome.git
synced 2025-03-15 15:18:16 +00:00
fixing the automation for data arrays
This commit is contained in:
parent
aaee40ce19
commit
a47e795591
@ -7,10 +7,11 @@ from esphome import automation
|
|||||||
from esphome.automation import maybe_simple_id
|
from esphome.automation import maybe_simple_id
|
||||||
from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority
|
from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority
|
||||||
from esphome.components import sensor
|
from esphome.components import sensor
|
||||||
from esphome.py_compat import text_type
|
from esphome.py_compat import text_type, binary_type, char_to_byte
|
||||||
from esphome.const import CONF_ID, CONF_TRIGGER_ID
|
from esphome.const import CONF_ID, CONF_TRIGGER_ID
|
||||||
|
|
||||||
IS_PLATFORM_COMPONENT = True
|
IS_PLATFORM_COMPONENT = True
|
||||||
|
MULTI_CONF = True
|
||||||
|
|
||||||
CONF_ON_RECEIVE = 'on_receive'
|
CONF_ON_RECEIVE = 'on_receive'
|
||||||
CONF_CANBUS_ID = 'canbus_id'
|
CONF_CANBUS_ID = 'canbus_id'
|
||||||
@ -36,7 +37,7 @@ CONFIG_SCHEMA = cv.Schema({
|
|||||||
}).extend(cv.COMPONENT_SCHEMA)
|
}).extend(cv.COMPONENT_SCHEMA)
|
||||||
|
|
||||||
# Actions
|
# Actions
|
||||||
SendAction = canbus_ns.class_('SendAction', automation.Action)
|
CanbusSendAction = canbus_ns.class_('CanbusSendAction', automation.Action)
|
||||||
|
|
||||||
CANBUS_ACTION_SCHEMA = maybe_simple_id({
|
CANBUS_ACTION_SCHEMA = maybe_simple_id({
|
||||||
cv.Required(CONF_CANBUS_ID): cv.use_id(CanbusComponent),
|
cv.Required(CONF_CANBUS_ID): cv.use_id(CanbusComponent),
|
||||||
@ -62,17 +63,26 @@ def register_canbus(var, config):
|
|||||||
yield setup_canbus_core_(var, config)
|
yield setup_canbus_core_(var, config)
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action(CONF_CANBUS_SEND_ACTION, SendAction, CANBUS_ACTION_SCHEMA)
|
@automation.register_action(CONF_CANBUS_SEND_ACTION, CanbusSendAction, CANBUS_ACTION_SCHEMA)
|
||||||
def canbus_action_to_code(config, action_id, template_arg, args):
|
def canbus_action_to_code(config, action_id, template_arg, args):
|
||||||
canbus = yield cg.get_variable(config[CONF_CANBUS_ID])
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
var = yield cg.new_Pvariable(action_id, template_arg, canbus)
|
yield cg.register_parented(var, config[CONF_CANBUS_ID])
|
||||||
lambda_ = yield cg.process_lambda(config[CONF_CAN_DATA], args, return_type=cg.float_)
|
|
||||||
can_id = yield cg.templatable(config[CONF_CAN_ID], args, cg.uint16)
|
can_id = yield cg.templatable(config[CONF_CAN_ID], args, cg.uint16)
|
||||||
cg.add(var.set_can_id(can_id))
|
cg.add(var.set_can_id(can_id))
|
||||||
cg.add(var.set_data(lambda_))
|
data = config[CONF_CAN_DATA]
|
||||||
|
if isinstance(data, binary_type):
|
||||||
|
data = [char_to_byte(x) for x in data]
|
||||||
|
|
||||||
|
if cg.is_template(data):
|
||||||
|
templ = yield cg.templatable(data, args, cg.std_vector.template(cg.uint8))
|
||||||
|
cg.add(var.set_data_template(templ))
|
||||||
|
else:
|
||||||
|
cg.add(var.set_data_static(data))
|
||||||
yield var
|
yield var
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@coroutine_with_priority(100.0)
|
@coroutine_with_priority(100.0)
|
||||||
def to_code(config):
|
def to_code(config):
|
||||||
cg.add_global(canbus_ns.using)
|
cg.add_global(canbus_ns.using)
|
||||||
|
@ -7,22 +7,42 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace canbus {
|
namespace canbus {
|
||||||
|
|
||||||
template<typename... Ts> class SendAction : public Action<Ts...> {
|
template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public Parented<Canbus> {
|
||||||
public:
|
public:
|
||||||
explicit SendAction(Canbus *parent) : parent_(parent){}
|
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
|
||||||
|
this->data_func_ = func;
|
||||||
|
this->static_ = false;
|
||||||
|
}
|
||||||
|
void set_data_static(const std::vector<uint8_t> &data) {
|
||||||
|
this->data_static_ = data;
|
||||||
|
this->static_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
void set_can_id(int can_id) {this->can_id_ = can_id;}
|
void set_can_id(int can_id) {this->can_id_ = can_id;}
|
||||||
|
|
||||||
TEMPLATABLE_VALUE(float, data)
|
TEMPLATABLE_VALUE(float, data)
|
||||||
|
|
||||||
|
// void play(Ts... x) override {
|
||||||
|
// auto call = this->parent_->make_call(this->can_id_);
|
||||||
|
// //unsigned uint const * p = reinterpret_cast<unsigned char const *>(&f);
|
||||||
|
// call.set_data(this->data_.optional_value(x...));
|
||||||
|
// // call.perform(this->parent_, this->can_id_);
|
||||||
|
// }
|
||||||
void play(Ts... x) override {
|
void play(Ts... x) override {
|
||||||
auto call = this->parent_->make_call(this->can_id_);
|
if (this->static_) {
|
||||||
//unsigned uint const * p = reinterpret_cast<unsigned char const *>(&f);
|
this->parent_->write_array(this->data_static_);
|
||||||
call.set_data(this->data_.optional_value(x...));
|
} else {
|
||||||
// call.perform(this->parent_, this->can_id_);
|
auto val = this->data_func_(x...);
|
||||||
|
this->parent_->write_array(val);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
Canbus *parent_;
|
Canbus *parent_;
|
||||||
int can_id_;
|
int can_id_;
|
||||||
|
|
||||||
|
bool static_{false};
|
||||||
|
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
|
||||||
|
std::vector<uint8_t> data_static_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
import esphome.codegen as cg
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome.components import binary_sensor
|
|
||||||
from esphome.const import CONF_ID
|
|
||||||
from . import canbus_ns, CanbusComponent, CONF_CANBUS_ID, CONF_CAN_ID
|
|
||||||
|
|
||||||
print("canbus.binary_sensor.py")
|
|
||||||
DEPENDENCIES = ['canbus']
|
|
||||||
|
|
||||||
CanbusBinarySensor = canbus_ns.class_('CanbusBinarySensor', binary_sensor.BinarySensor)
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
|
||||||
cv.GenerateID(): cv.declare_id(CanbusBinarySensor),
|
|
||||||
cv.GenerateID(CONF_CANBUS_ID): cv.use_id(CanbusComponent),
|
|
||||||
cv.Required(CONF_CAN_ID): cv.int_range(min=1, max=255)
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
def to_code(config):
|
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
|
||||||
yield binary_sensor.register_binary_sensor(var, config)
|
|
||||||
|
|
||||||
hub = yield cg.get_variable(config[CONF_CANBUS_ID])
|
|
||||||
cg.add(var.set_can_id(config[CONF_CAN_ID]))
|
|
||||||
cg.add(hub.register_can_device(var))
|
|
@ -19,10 +19,10 @@ template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Pa
|
|||||||
|
|
||||||
void play(Ts... x) override {
|
void play(Ts... x) override {
|
||||||
if (this->static_) {
|
if (this->static_) {
|
||||||
this->parent_->write_array(this->data_static_);
|
this->parent_->send_data(this->data_static_);
|
||||||
} else {
|
} else {
|
||||||
auto val = this->data_func_(x...);
|
auto val = this->data_func_(x...);
|
||||||
this->parent_->write_array(val);
|
this->parent_->send_data(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user