mirror of
https://github.com/esphome/esphome.git
synced 2025-03-20 17:48:17 +00:00
canbus triggers and actions working
for binary_sensor
This commit is contained in:
parent
3d54ac6c3b
commit
51f7155d32
@ -2,7 +2,8 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.automation import maybe_simple_id
|
||||
from esphome.core import coroutine_with_priority
|
||||
from esphome.core import CORE, coroutine, coroutine_with_priority
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
|
||||
@ -26,6 +27,22 @@ CANBUS_ACTION_SCHEMA = maybe_simple_id({
|
||||
cv.Required(CONF_CAN_DATA): cv.All(),
|
||||
})
|
||||
|
||||
@coroutine
|
||||
def setup_canbus_core_(var, config):
|
||||
yield cg.register_component(var, config)
|
||||
if CONF_CANBUS_ID in config:
|
||||
cg.add(var.set_canbus_id(config[CONF_CANBUS_ID]))
|
||||
if CONF_CAN_ID in config:
|
||||
cg.add(var.set_can_id([config[CONF_CAN_ID]]))
|
||||
if CONF_CAN_DATA in config:
|
||||
cg.add(var.set_can_data([config[CONF_CAN_DATA]]))
|
||||
|
||||
@coroutine
|
||||
def register_canbus(var, config):
|
||||
if not CORE.has_id(config[CONF_ID]):
|
||||
var = cg.Pvariable(config[CONF_ID], var)
|
||||
yield setup_canbus_core_(var, config)
|
||||
|
||||
@automation.register_action('canbus.send', SendAction, CANBUS_ACTION_SCHEMA)
|
||||
def canbus_send_to_code(config, action_id, template_arg, args):
|
||||
canbus = yield cg.get_variable(config[CONF_CANBUS_ID])
|
||||
|
@ -7,18 +7,16 @@
|
||||
namespace esphome {
|
||||
namespace canbus {
|
||||
|
||||
|
||||
class CanbusSensor {
|
||||
public:
|
||||
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 {
|
||||
class CanbusBinarySensor : public CanbusSensor, public binary_sensor::BinarySensor {
|
||||
friend class Canbus;
|
||||
|
||||
};
|
||||
|
||||
class Canbus : public Component {
|
||||
@ -27,6 +25,10 @@ class Canbus : public Component {
|
||||
Canbus(const std::string &name){};
|
||||
virtual void send(int can_id, uint8_t *data);
|
||||
void register_can_device(CanbusSensor *component){};
|
||||
void set_can_id(int can_id) { this->can_id_ = can_id; }
|
||||
|
||||
protected:
|
||||
int can_id_{0};
|
||||
};
|
||||
} // namespace canbus
|
||||
} // namespace esphome
|
@ -1,29 +1,26 @@
|
||||
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.components import spi, canbus
|
||||
from esphome.const import CONF_CS_PIN, CONF_ID
|
||||
from esphome.components.canbus import CONF_CAN_ID
|
||||
from esphome.components.canbus import CanbusComponent, CONF_CAN_ID
|
||||
|
||||
print("mcp2515.canbus.py")
|
||||
|
||||
AUTO_LOAD = ['canbus']
|
||||
DEPENDENCIES = ['spi']
|
||||
|
||||
mcp2515_ns = cg.esphome_ns.namespace('mcp2515')
|
||||
mcp2515 = mcp2515_ns.class_('MCP2515', cg.Component, canbus.CanbusComponent, spi.SPIDevice)
|
||||
mcp2515 = mcp2515_ns.class_('MCP2515', canbus.CanbusComponent, spi.SPIDevice)
|
||||
|
||||
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({
|
||||
CONFIG_SCHEMA = canbus.CONFIG_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(mcp2515),
|
||||
cv.Required(CONF_CS_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max= 999),
|
||||
cv.Required(CONF_CAN_ID): cv.int_range(min=0, max=999),
|
||||
}).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 canbus.register_canbus(var, config)
|
||||
yield spi.register_spi_device(var, config)
|
||||
cs = yield cg.gpio_pin_expression(config[CONF_CS_PIN])
|
||||
cg.add(var.set_cs_pin(cs))
|
||||
|
@ -3,16 +3,18 @@
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
#include "esphome/components/canbus/canbus.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace mcp2515 {
|
||||
static const char *TAG = "mcp2515";
|
||||
class MCP2515 : public canbus::Canbus,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_8MHZ> {
|
||||
public:
|
||||
MCP2515() {};
|
||||
MCP2515(const std::string &name){};
|
||||
void send(int can_id, uint8_t *data) {};
|
||||
void send(int can_id, uint8_t *data) { ESP_LOGD(TAG, "send: id=%d, data=%d",can_id,data[0]); };
|
||||
|
||||
void set_cs_pin(GPIOPin *cs_pin) { cs_pin_ = cs_pin; }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user