__init__.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Copyright (C) 2021-2022 Oxan van Leeuwen
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. import esphome.codegen as cg
  16. import esphome.config_validation as cv
  17. from esphome.components import uart
  18. from esphome.const import CONF_ID, CONF_PORT
  19. # ESPHome doesn't know the Stream abstraction yet, so hardcode to use a UART for now.
  20. AUTO_LOAD = ["socket"]
  21. DEPENDENCIES = ["uart", "network"]
  22. MULTI_CONF = True
  23. StreamServerComponent = cg.global_ns.class_("StreamServerComponent", cg.Component)
  24. CONFIG_SCHEMA = (
  25. cv.Schema(
  26. {
  27. cv.GenerateID(): cv.declare_id(StreamServerComponent),
  28. cv.Optional(CONF_PORT): cv.port,
  29. }
  30. )
  31. .extend(cv.COMPONENT_SCHEMA)
  32. .extend(uart.UART_DEVICE_SCHEMA)
  33. )
  34. def to_code(config):
  35. var = cg.new_Pvariable(config[CONF_ID])
  36. if CONF_PORT in config:
  37. cg.add(var.set_port(config[CONF_PORT]))
  38. yield cg.register_component(var, config)
  39. yield uart.register_uart_device(var, config)