# Copyright (C) 2021-2023 Oxan van Leeuwen # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import uart from esphome.const import CONF_ID, CONF_PORT, CONF_BUFFER_SIZE # ESPHome doesn't know the Stream abstraction yet, so hardcode to use a UART for now. AUTO_LOAD = ["socket"] DEPENDENCIES = ["uart", "network"] MULTI_CONF = True ns = cg.global_ns StreamServerComponent = ns.class_("StreamServerComponent", cg.Component) def validate_buffer_size(buffer_size): if buffer_size & (buffer_size - 1) != 0: raise cv.Invalid("Buffer size must be a power of two.") return buffer_size CONFIG_SCHEMA = cv.All( cv.require_esphome_version(2022, 3, 0), cv.Schema( { cv.GenerateID(): cv.declare_id(StreamServerComponent), cv.Optional(CONF_PORT, default=6638): cv.port, cv.Optional(CONF_BUFFER_SIZE, default=128): cv.All( cv.positive_int, validate_buffer_size ), } ) .extend(cv.COMPONENT_SCHEMA) .extend(uart.UART_DEVICE_SCHEMA), ) async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) cg.add(var.set_port(config[CONF_PORT])) cg.add(var.set_buffer_size(config[CONF_BUFFER_SIZE])) await cg.register_component(var, config) await uart.register_uart_device(var, config)