sensor.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. STATE_CLASS_MEASUREMENT,
  20. ENTITY_CATEGORY_DIAGNOSTIC,
  21. )
  22. from . import ns, StreamServerComponent
  23. CONF_CONNECTION_COUNT = "connection_count"
  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_CONNECTION_COUNT): sensor.sensor_schema(
  29. accuracy_decimals=0,
  30. state_class=STATE_CLASS_MEASUREMENT,
  31. entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
  32. ),
  33. }
  34. )
  35. async def to_code(config):
  36. server = await cg.get_variable(config[CONF_STREAM_SERVER])
  37. sens = await sensor.new_sensor(config[CONF_CONNECTION_COUNT])
  38. cg.add(server.set_connection_count_sensor(sens))