diff --git a/.gitignore b/.gitignore index 6002612c13..ac9e0d7b1e 100644 --- a/.gitignore +++ b/.gitignore @@ -113,3 +113,5 @@ config/ tests/build/ tests/.esphome/ /.temp-clang-tidy.cpp +.pio/ +.gitignore diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py index 46e82ae641..7317656deb 100644 --- a/esphome/components/canbus/__init__.py +++ b/esphome/components/canbus/__init__.py @@ -1,39 +1,37 @@ import esphome.codegen as cg import esphome.config_validation as cv -from esphome.automation import Condition, maybe_simple_id -from esphome.components import binary_sensor from esphome import automation -from esphome import pins -from esphome.const import CONF_BUFFER_SIZE, CONF_DUMP, CONF_FILTER, CONF_ID, CONF_IDLE, \ - CONF_PIN, CONF_TOLERANCE - -canbus_ns = cg.esphome_ns.namespace('canbus') -CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component) +from esphome.automation import maybe_simple_id +from esphome.core import coroutine_with_priority IS_PLATFORM_COMPONENT = True CONF_CANBUS_ID = 'canbus_id' CONF_CAN_ID = 'can_id' -CONF_DATA = 'data' +CONF_CAN_DATA = 'can_data' -SendAction = canbus_ns.class_('SendAction', automation.Action) +canbus_ns = cg.esphome_ns.namespace('canbus') +CanbusComponent = canbus_ns.class_('CanbusComponent', cg.Component) CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(CanbusComponent), - cv.SplitDefault(CONF_BUFFER_SIZE, esp32='10000b', esp8266='1000b'): cv.validate_bytes, }).extend(cv.COMPONENT_SCHEMA) +# Actions +SendAction = canbus_ns.class_('SendAction', automation.Action) + CANBUS_ACTION_SCHEMA = maybe_simple_id({ - cv.Required(CONF_ID): cv.use_id(binary_sensor), + cv.Required(CONF_CANBUS_ID): cv.use_id(CanbusComponent), + cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=999), + cv.Required(CONF_CAN_DATA): cv.All(), }) @automation.register_action('canbus.send', SendAction, CANBUS_ACTION_SCHEMA) def canbus_send_to_code(config, action_id, template_arg, args): - paren = yield cg.get_variable(config[CONF_ID]) - yield cg.new_Pvariable(action_id, template_arg, paren) + canbus = yield cg.get_variable(config[CONF_CANBUS_ID]) + #paren = yield cg.get_variable(config[CONF_ID]) + yield cg.new_Pvariable(action_id, template_arg, canbus, config[CONF_CAN_ID]) +@coroutine_with_priority(100.0) def to_code(config): - print("canbus to_code") - #var = cg.new_Pvariable(config[CONF_ID]) - # yield cg.register_component(var, config) - # cg.add(var.set_buffer_size(config[CONF_BUFFER_SIZE])) + cg.add_global(canbus_ns.using) diff --git a/esphome/components/canbus/automation.cpp b/esphome/components/canbus/automation.cpp new file mode 100644 index 0000000000..ba9bb7e05a --- /dev/null +++ b/esphome/components/canbus/automation.cpp @@ -0,0 +1,10 @@ +#include "automation.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace switch_ { + +static const char *TAG = "switch.automation"; + +} // namespace switch_ +} // namespace esphome diff --git a/esphome/components/canbus/automation.h b/esphome/components/canbus/automation.h new file mode 100644 index 0000000000..2ede6cec58 --- /dev/null +++ b/esphome/components/canbus/automation.h @@ -0,0 +1,35 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" +#include "esphome/components/canbus/canbus.h" + +namespace esphome { +namespace canbus { + +template class SendAction : public Action { + public: + explicit SendAction(Canbus *a_canbus, int can_id) : canbus_(a_canbus), can_id_(can_id) { } + + void play(Ts... x) override { + uint8_t data[8]={0}; + data[1] = 10; + this->canbus_->send(this->can_id_,data); } + + protected: + Canbus *canbus_; + int can_id_; +}; + +template class SendStateAction : public Action { + public: + SendStateAction(Canbus *a_canbus) : canbus_(a_canbus) {} + TEMPLATABLE_VALUE(bool, state) + void play(Ts... x) override { this->canbus_->send_state(this->state_.value(x...)); } + + protected: + Canbus *canbus_; +}; + +} // namespace canbus +} // namespace esphome diff --git a/esphome/components/canbus/binary_sensor.py b/esphome/components/canbus/binary_sensor.py index 672c0a6439..1e13e5ae9f 100644 --- a/esphome/components/canbus/binary_sensor.py +++ b/esphome/components/canbus/binary_sensor.py @@ -2,9 +2,10 @@ 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 +from . import canbus_ns, CanbusComponent, CONF_CANBUS_ID, CONF_CAN_ID -CONF_CAN_ID = 'can_id' +print("canbus.binary_sensor.py") +DEPENDENCIES = ['canbus'] CanbusBinarySensor = canbus_ns.class_('CanbusBinarySensor', binary_sensor.BinarySensor) @@ -18,7 +19,7 @@ CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({ 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/canbus/canbus.h b/esphome/components/canbus/canbus.h new file mode 100644 index 0000000000..1d4989ea89 --- /dev/null +++ b/esphome/components/canbus/canbus.h @@ -0,0 +1,32 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" +#include "esphome/components/binary_sensor/binary_sensor.h" + +namespace esphome { +namespace canbus { + + +class CanbusSensor { +public: + void set_can_id(int can_id) { this->can_id_ = can_id; } + + private: + int can_id_{0}; +}; + +class CanbusBinarySensor : public CanbusSensor , public binary_sensor::BinarySensor { + friend class Canbus; + +}; + +class Canbus : public Component { + public: + Canbus(){}; + Canbus(const std::string &name){}; + virtual void send(int can_id, uint8_t *data); + void register_can_device(CanbusSensor *component){}; +}; +} // namespace canbus +} // namespace esphome \ No newline at end of file diff --git a/esphome/components/esp32_can/canbus.py b/esphome/components/esp32_can/canbus.py index c74bac75b4..fe7a03c23d 100644 --- a/esphome/components/esp32_can/canbus.py +++ b/esphome/components/esp32_can/canbus.py @@ -1,14 +1,21 @@ import esphome.codegen as cg import esphome.config_validation as cv +from esphome import pins from esphome.components import canbus from esphome.const import CONF_ID +from esphome.components.canbus import CONF_CAN_ID +CONF_RX_PIN = 'rx_pin' +CONF_TX_PIN = 'tx_pin' esp32_can_ns = cg.esphome_ns.namespace('esp32_can') esp32_can = esp32_can_ns.class_('ESP32Can', cg.Component, canbus.CanbusComponent) CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(esp32_can), + cv.Required(CONF_RX_PIN): pins.gpio_output_pin_schema, + cv.Required(CONF_TX_PIN): pins.gpio_output_pin_schema, + cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=999), }).extend(cv.COMPONENT_SCHEMA) @@ -17,4 +24,3 @@ def to_code(config): var = cg.Pvariable(config[CONF_ID], rhs) yield cg.register_component(var, config) - diff --git a/esphome/components/esp32_can/esp32_can.h b/esphome/components/esp32_can/esp32_can.h new file mode 100644 index 0000000000..b9201ba710 --- /dev/null +++ b/esphome/components/esp32_can/esp32_can.h @@ -0,0 +1,16 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/automation.h" +#include "esphome/components/canbus/canbus.h" + +namespace esphome { +namespace esp32_can { +class ESP32Can : public canbus::Canbus { + public: + ESP32Can() {}; + ESP32Can(const std::string &name){}; + void send(int can_id, uint8_t *data){}; +}; +} // namespace esp32_can +} // namespace esphome \ No newline at end of file diff --git a/esphome/components/mcp2515/__init__.py b/esphome/components/mcp2515/__init__.py index 59c23900fa..46299f1d04 100644 --- a/esphome/components/mcp2515/__init__.py +++ b/esphome/components/mcp2515/__init__.py @@ -1 +1 @@ -print "hello" \ No newline at end of file +print("mcp2515.__init__.py") \ No newline at end of file diff --git a/esphome/components/mcp2515/canbus.py b/esphome/components/mcp2515/canbus.py index 4f1d2647e4..987cde5cde 100644 --- a/esphome/components/mcp2515/canbus.py +++ b/esphome/components/mcp2515/canbus.py @@ -2,7 +2,10 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins from esphome.components import spi, binary_sensor, canbus -from esphome.const import CONF_DC_PIN, CONF_CS_PIN, CONF_ID +from esphome.const import CONF_CS_PIN, CONF_ID +from esphome.components.canbus import CONF_CAN_ID + +print("mcp2515.canbus.py") DEPENDENCIES = ['spi'] @@ -11,8 +14,8 @@ mcp2515 = mcp2515_ns.class_('MCP2515', cg.Component, canbus.CanbusComponent, spi CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({ cv.GenerateID(): cv.declare_id(mcp2515), - cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema, cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema, + cv.Required(CONF_CAN_ID): cv.int_range(min=0, max= 999), }).extend(spi.SPI_DEVICE_SCHEMA) @@ -22,7 +25,5 @@ def to_code(config): yield cg.register_component(var, config) yield spi.register_spi_device(var, config) - dc = yield cg.gpio_pin_expression(config[CONF_DC_PIN]) - cg.add(var.set_dc_pin(dc)) cs = yield cg.gpio_pin_expression(config[CONF_CS_PIN]) cg.add(var.set_cs_pin(cs)) diff --git a/esphome/components/mcp2515/mcp2515.h b/esphome/components/mcp2515/mcp2515.h new file mode 100644 index 0000000000..ea2b59bb5b --- /dev/null +++ b/esphome/components/mcp2515/mcp2515.h @@ -0,0 +1,23 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/spi/spi.h" +#include "esphome/components/canbus/canbus.h" + +namespace esphome { +namespace mcp2515 { +class MCP2515 : public canbus::Canbus, + public spi::SPIDevice { + public: + MCP2515() {}; + MCP2515(const std::string &name){}; + void send(int can_id, uint8_t *data) {}; + + void set_cs_pin(GPIOPin *cs_pin) { cs_pin_ = cs_pin; } + + protected: + GPIOPin *cs_pin_; +}; +} // namespace mcp2515 +} // namespace esphome \ No newline at end of file