sensor.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 sensor
  18. from esphome.const import (
  19. ENTITY_CATEGORY_DIAGNOSTIC,
  20. )
  21. from . import ns, StreamServerComponent
  22. CONF_CONNECTION_COUNT = "connection_count"
  23. CONF_STREAM_SERVER = "stream_server"
  24. CONFIG_SCHEMA = cv.Schema(
  25. {
  26. cv.GenerateID(CONF_STREAM_SERVER): cv.use_id(StreamServerComponent),
  27. cv.Required(CONF_CONNECTION_COUNT): sensor.sensor_schema(
  28. accuracy_decimals=0,
  29. entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
  30. )
  31. }
  32. )
  33. async def to_code(config):
  34. server = await cg.get_variable(config[CONF_STREAM_SERVER])
  35. sens = await sensor.new_sensor(config[CONF_CONNECTION_COUNT])
  36. cg.add(server.set_connection_count_sensor(sens))