Add ESPHome Python integration for usage as external component

This commit is contained in:
Oxan van Leeuwen
2021-05-19 19:05:21 +02:00
parent 65ab7895af
commit c92e5a1ce2
3 changed files with 34 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart
from esphome.const import CONF_ID, CONF_PORT
# ESPHome doesn't know the Stream abstraction yet, so hardcode to use a UART for now.
DEPENDENCIES = ["uart"]
MULTI_CONF = True
StreamServerComponent = cg.global_ns.class_("StreamServerComponent", cg.Component)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(StreamServerComponent),
cv.Optional(CONF_PORT): cv.port,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA)
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
if CONF_PORT in config:
cg.add(var.set_port(config[CONF_PORT]))
yield cg.register_component(var, config)
yield uart.register_uart_device(var, config)

View File

@@ -17,6 +17,7 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include <memory>
#include <string>
@@ -31,7 +32,9 @@
class StreamServerComponent : public esphome::Component {
public:
StreamServerComponent() = default;
explicit StreamServerComponent(Stream *stream) : stream_{stream} {}
void set_uart_parent(esphome::uart::UARTComponent *parent) { this->stream_ = parent; }
void setup() override;
void loop() override;