You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1.2 KiB

  1. import esphome.codegen as cg
  2. import esphome.config_validation as cv
  3. from esphome.components import sensor
  4. from esphome.const import CONF_ID, CONF_FORCE_UPDATE, CONF_RANGE_FROM, CONF_RANGE_TO
  5. from .. import (
  6. bslamp2_ns, CODEOWNERS,
  7. CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
  8. )
  9. AUTO_LOAD = ["xiaomi_bslamp2"]
  10. XiaomiBslamp2SliderSensor = bslamp2_ns.class_(
  11. "XiaomiBslamp2SliderSensor", sensor.Sensor, cg.Component)
  12. CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
  13. {
  14. cv.GenerateID(): cv.declare_id(XiaomiBslamp2SliderSensor),
  15. cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
  16. cv.Optional(CONF_FORCE_UPDATE, default=True): cv.boolean,
  17. cv.Optional(CONF_RANGE_FROM, default=0.01): cv.float_,
  18. cv.Optional(CONF_RANGE_TO, default=1.00): cv.float_,
  19. }
  20. ).extend(cv.COMPONENT_SCHEMA)
  21. async def to_code(config):
  22. var = cg.new_Pvariable(config[CONF_ID])
  23. await cg.register_component(var, config)
  24. await sensor.register_sensor(var, config)
  25. front_panel_hal_var = await cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
  26. cg.add(var.set_parent(front_panel_hal_var))
  27. cg.add(var.set_range_from(config[CONF_RANGE_FROM]))
  28. cg.add(var.set_range_to(config[CONF_RANGE_TO]))