diff --git a/esphome/components/canbus/__init__.py b/esphome/components/canbus/__init__.py new file mode 100644 index 0000000000..46e82ae641 --- /dev/null +++ b/esphome/components/canbus/__init__.py @@ -0,0 +1,39 @@ +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) + +IS_PLATFORM_COMPONENT = True + +CONF_CANBUS_ID = 'canbus_id' +CONF_CAN_ID = 'can_id' +CONF_DATA = 'data' + +SendAction = canbus_ns.class_('SendAction', automation.Action) + +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) + +CANBUS_ACTION_SCHEMA = maybe_simple_id({ + cv.Required(CONF_ID): cv.use_id(binary_sensor), +}) + +@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) + +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])) diff --git a/esphome/components/canbus/binary_sensor.py b/esphome/components/canbus/binary_sensor.py new file mode 100644 index 0000000000..672c0a6439 --- /dev/null +++ b/esphome/components/canbus/binary_sensor.py @@ -0,0 +1,24 @@ +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 = 'can_id' + +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=0, 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/esp32_can/__init__.py b/esphome/components/esp32_can/__init__.py new file mode 100644 index 0000000000..5cdb2de744 --- /dev/null +++ b/esphome/components/esp32_can/__init__.py @@ -0,0 +1 @@ +print("esp32_can_module") \ No newline at end of file diff --git a/esphome/components/esp32_can/canbus.py b/esphome/components/esp32_can/canbus.py new file mode 100644 index 0000000000..c74bac75b4 --- /dev/null +++ b/esphome/components/esp32_can/canbus.py @@ -0,0 +1,20 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import canbus +from esphome.const import CONF_ID + + +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), +}).extend(cv.COMPONENT_SCHEMA) + + +def to_code(config): + rhs = esp32_can.new() + var = cg.Pvariable(config[CONF_ID], rhs) + + yield cg.register_component(var, config) + diff --git a/esphome/components/mcp2515/__init__.py b/esphome/components/mcp2515/__init__.py new file mode 100644 index 0000000000..59c23900fa --- /dev/null +++ b/esphome/components/mcp2515/__init__.py @@ -0,0 +1 @@ +print "hello" \ No newline at end of file diff --git a/esphome/components/mcp2515/canbus.py b/esphome/components/mcp2515/canbus.py new file mode 100644 index 0000000000..4f1d2647e4 --- /dev/null +++ b/esphome/components/mcp2515/canbus.py @@ -0,0 +1,28 @@ +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 + +DEPENDENCIES = ['spi'] + +mcp2515_ns = cg.esphome_ns.namespace('mcp2515') +mcp2515 = mcp2515_ns.class_('MCP2515', cg.Component, canbus.CanbusComponent, spi.SPIDevice) + +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, +}).extend(spi.SPI_DEVICE_SCHEMA) + + +def to_code(config): + rhs = mcp2515.new() + var = cg.Pvariable(config[CONF_ID], rhs) + + 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))