Files
esphome-dev/esphome/components/mcp2515/canbus.py
T
Guillermo Ruffino 1e227e8051 Schema dump (#1564)
* schema dump idea

accept boolean or anything default

accept null also for full dicts

added some common validators

more simple validators

support multi_conf

better handle automations

updates

updates

handle lists

removed not needed class

move to own folder

generalized for automations lists, etc

updates

updates

clean up

clean up

fix automations

made comment optional

basic docs support

added more docs

fixes docs handling

updates

updates

fix components parent

updates

updates

updates

Fix inkplate 6 registration

updates

Disable logging for vscode add on

better handle buses

keep extended order as in CONFIGs

updates

updates

updates

disable comments

moved to scripts/build_jsonschema

added configurable decorators

path handling

fix handle list_schema

fixes and cleanup

add jschema_extractor to maybe

updates

lint

no schema in git

add generated loggers list

* lint
2021-03-07 21:05:08 -03:00

50 lines
1.5 KiB
Python

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import spi, canbus
from esphome.const import CONF_ID, CONF_MODE
from esphome.components.canbus import CanbusComponent
CODEOWNERS = ["@mvturnho", "@danielschramm"]
DEPENDENCIES = ["spi"]
CONF_CLOCK = "clock"
mcp2515_ns = cg.esphome_ns.namespace("mcp2515")
mcp2515 = mcp2515_ns.class_("MCP2515", CanbusComponent, spi.SPIDevice)
CanClock = mcp2515_ns.enum("CAN_CLOCK")
McpMode = mcp2515_ns.enum("CANCTRL_REQOP_MODE")
CAN_CLOCK = {
"8MHZ": CanClock.MCP_8MHZ,
"16MHZ": CanClock.MCP_16MHZ,
"20MHZ": CanClock.MCP_20MHZ,
}
MCP_MODE = {
"NORMAL": McpMode.CANCTRL_REQOP_NORMAL,
"LOOPBACK": McpMode.CANCTRL_REQOP_LOOPBACK,
"LISTENONLY": McpMode.CANCTRL_REQOP_LISTENONLY,
}
CONFIG_SCHEMA = canbus.CANBUS_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(mcp2515),
cv.Optional(CONF_CLOCK, default="8MHZ"): cv.enum(CAN_CLOCK, upper=True),
cv.Optional(CONF_MODE, default="NORMAL"): cv.enum(MCP_MODE, upper=True),
}
).extend(spi.spi_device_schema(True))
def to_code(config):
rhs = mcp2515.new()
var = cg.Pvariable(config[CONF_ID], rhs)
yield canbus.register_canbus(var, config)
if CONF_CLOCK in config:
canclock = CAN_CLOCK[config[CONF_CLOCK]]
cg.add(var.set_mcp_clock(canclock))
if CONF_MODE in config:
mode = MCP_MODE[config[CONF_MODE]]
cg.add(var.set_mcp_mode(mode))
yield spi.register_spi_device(var, config)