diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index a6b23d9255..1ace22455c 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -7,10 +7,11 @@ from esphome import automation from esphome.automation import maybe_simple_id from esphome.core import CORE, EsphomeError, Lambda, coroutine, coroutine_with_priority 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 IS_PLATFORM_COMPONENT = True +MULTI_CONF = True CONF_ON_RECEIVE = 'on_receive' CONF_CANBUS_ID = 'canbus_id' @@ -36,7 +37,7 @@ CONFIG_SCHEMA = cv.Schema({ }).extend(cv.COMPONENT_SCHEMA) # Actions -SendAction = canbus_ns.class_('SendAction', automation.Action) +CanbusSendAction = canbus_ns.class_('CanbusSendAction', automation.Action) CANBUS_ACTION_SCHEMA = maybe_simple_id({ cv.Required(CONF_CANBUS_ID): cv.use_id(CanbusComponent), @@ -62,17 +63,26 @@ def register_canbus(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): - canbus = yield cg.get_variable(config[CONF_CANBUS_ID]) - var = yield cg.new_Pvariable(action_id, template_arg, canbus) - lambda_ = yield cg.process_lambda(config[CONF_CAN_DATA], args, return_type=cg.float_) + var = cg.new_Pvariable(action_id, template_arg) + yield cg.register_parented(var, config[CONF_CANBUS_ID]) + can_id = yield cg.templatable(config[CONF_CAN_ID], args, cg.uint16) 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 + @coroutine_with_priority(100.0) def to_code(config): cg.add_global(canbus_ns.using) diff --git a/esphome/components/canbus/automation.h b/esphome/components/canbus/automation.h index 45506c1c51..037f0a005d 100644 --- a/esphome/components/canbus/automation.h +++ b/esphome/components/canbus/automation.h @@ -7,22 +7,42 @@ namespace esphome { namespace canbus { -template class SendAction : public Action { +template class CanbusSendAction : public Action, public Parented { public: - explicit SendAction(Canbus *parent) : parent_(parent){} + void set_data_template(std::function(Ts...)> func) { + this->data_func_ = func; + this->static_ = false; + } + void set_data_static(const std::vector &data) { + this->data_static_ = data; + this->static_ = true; + } + void set_can_id(int can_id) {this->can_id_ = can_id;} TEMPLATABLE_VALUE(float, data) - void play(Ts... x) override { - auto call = this->parent_->make_call(this->can_id_); - //unsigned uint const * p = reinterpret_cast(&f); - call.set_data(this->data_.optional_value(x...)); -// call.perform(this->parent_, this->can_id_); +// void play(Ts... x) override { +// auto call = this->parent_->make_call(this->can_id_); +// //unsigned uint const * p = reinterpret_cast(&f); +// call.set_data(this->data_.optional_value(x...)); +// // call.perform(this->parent_, this->can_id_); +// } + void play(Ts... x) override { + if (this->static_) { + this->parent_->write_array(this->data_static_); + } else { + auto val = this->data_func_(x...); + this->parent_->write_array(val); + } } protected: Canbus *parent_; int can_id_; + + bool static_{false}; + std::function(Ts...)> data_func_{}; + std::vector data_static_{}; }; diff --git a/esphome/components/canbus/binary_sensor.py b/esphome/components/canbus/binary_sensor.py deleted file mode 100644 index c6d5b1043d..0000000000 --- a/esphome/components/canbus/binary_sensor.py +++ /dev/null @@ -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)) diff --git a/esphome/components/uart/automation.h b/esphome/components/uart/automation.h index 9686f94413..91e936ff97 100644 --- a/esphome/components/uart/automation.h +++ b/esphome/components/uart/automation.h @@ -19,10 +19,10 @@ template class UARTWriteAction : public Action, public Pa void play(Ts... x) override { if (this->static_) { - this->parent_->write_array(this->data_static_); + this->parent_->send_data(this->data_static_); } else { auto val = this->data_func_(x...); - this->parent_->write_array(val); + this->parent_->send_data(val); } }