mirror of
https://github.com/oxan/esphome-stream-server.git
synced 2026-05-16 14:27:03 +00:00
This prevents dropped data if a write to a client is cut short. Move default port number to config schema
61 lines
1.9 KiB
Python
61 lines
1.9 KiB
Python
# 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 <https://www.gnu.org/licenses/>.
|
|
|
|
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
|
|
|
|
StreamServerComponent = cg.global_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.Schema(
|
|
{
|
|
cv.GenerateID(): cv.declare_id(StreamServerComponent),
|
|
cv.Optional(CONF_PORT): 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])
|
|
if CONF_PORT in config:
|
|
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)
|