__init__.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2021 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. DEPENDENCIES = ["uart"]
  21. MULTI_CONF = True
  22. StreamServerComponent = cg.global_ns.class_("StreamServerComponent", cg.Component)
  23. CONFIG_SCHEMA = (
  24. cv.Schema(
  25. {
  26. cv.GenerateID(): cv.declare_id(StreamServerComponent),
  27. cv.Optional(CONF_PORT): cv.port,
  28. }
  29. )
  30. .extend(cv.COMPONENT_SCHEMA)
  31. .extend(uart.UART_DEVICE_SCHEMA)
  32. )
  33. def to_code(config):
  34. var = cg.new_Pvariable(config[CONF_ID])
  35. if CONF_PORT in config:
  36. cg.add(var.set_port(config[CONF_PORT]))
  37. yield cg.register_component(var, config)
  38. yield uart.register_uart_device(var, config)