binary_sensor.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2023 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 binary_sensor
  18. from esphome.const import (
  19. DEVICE_CLASS_CONNECTIVITY,
  20. ENTITY_CATEGORY_DIAGNOSTIC,
  21. )
  22. from . import ns, StreamServerComponent
  23. CONF_CONNECTED = "connected"
  24. CONF_STREAM_SERVER = "stream_server"
  25. CONFIG_SCHEMA = cv.Schema(
  26. {
  27. cv.GenerateID(CONF_STREAM_SERVER): cv.use_id(StreamServerComponent),
  28. cv.Required(CONF_CONNECTED): binary_sensor.binary_sensor_schema(
  29. device_class=DEVICE_CLASS_CONNECTIVITY,
  30. entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
  31. ),
  32. }
  33. )
  34. async def to_code(config):
  35. server = await cg.get_variable(config[CONF_STREAM_SERVER])
  36. sens = await binary_sensor.new_binary_sensor(config[CONF_CONNECTED])
  37. cg.add(server.set_connected_sensor(sens))