Advertisement
penright

Untitled

Jan 11th, 2022
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. from voluptuous.schema_builder import Undefined
  2. import esphome.codegen as cg
  3. import esphome.config_validation as cv
  4. from esphome.components import binary_sensor, sensor, esp32_ble_tracker
  5. from esphome.const import (
  6.     CONF_BATTERY_LEVEL,
  7.     CONF_BATTERY_VOLTAGE,
  8.     CONF_BINARY_SENSOR,
  9.     CONF_MAC_ADDRESS,
  10.     CONF_HUMIDITY,
  11.     CONF_TEMPERATURE,
  12.     CONF_ID,
  13.     DEVICE_CLASS_BATTERY,
  14.     DEVICE_CLASS_HUMIDITY,
  15.     DEVICE_CLASS_TEMPERATURE,
  16.     DEVICE_CLASS_VOLTAGE,
  17.     ENTITY_CATEGORY_DIAGNOSTIC,
  18.     ICON_EMPTY,
  19.     STATE_CLASS_MEASUREMENT,
  20.     UNIT_CELSIUS,
  21.     UNIT_EMPTY,
  22.     UNIT_PERCENT,
  23.     UNIT_VOLT,
  24. )
  25. CONF_FLAG_VALUE = 'flag_value'
  26. CONF_REED_SWITCH = 'reed_switch'
  27.  
  28. CODEOWNERS = ["@pasiz"]
  29.  
  30. DEPENDENCIES = ["esp32_ble_tracker"]
  31.  
  32. pvvx_mithermometer_ns = cg.esphome_ns.namespace("pvvx_mithermometer")
  33. PVVXMiThermometer = pvvx_mithermometer_ns.class_(
  34.     "PVVXMiThermometer", esp32_ble_tracker.ESPBTDeviceListener, cg.Component
  35. )
  36.  
  37. CONFIG_SCHEMA = (
  38.     cv.Schema(
  39.         {
  40.             cv.GenerateID(): cv.declare_id(PVVXMiThermometer),
  41.             cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
  42.             cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
  43.                 unit_of_measurement=UNIT_CELSIUS,
  44.                 accuracy_decimals=2,
  45.                 device_class=DEVICE_CLASS_TEMPERATURE,
  46.                 state_class=STATE_CLASS_MEASUREMENT,
  47.             ),
  48.             cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
  49.                 unit_of_measurement=UNIT_PERCENT,
  50.                 accuracy_decimals=2,
  51.                 device_class=DEVICE_CLASS_HUMIDITY,
  52.                 state_class=STATE_CLASS_MEASUREMENT,
  53.             ),
  54.             cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema(
  55.                 unit_of_measurement=UNIT_PERCENT,
  56.                 accuracy_decimals=0,
  57.                 device_class=DEVICE_CLASS_BATTERY,
  58.                 state_class=STATE_CLASS_MEASUREMENT,
  59.                 entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
  60.             ),
  61.             cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema(
  62.                 unit_of_measurement=UNIT_VOLT,
  63.                 accuracy_decimals=3,
  64.                 device_class=DEVICE_CLASS_VOLTAGE,
  65.                 state_class=STATE_CLASS_MEASUREMENT,
  66.                 entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
  67.             ),
  68.             cv.Optional(CONF_FLAG_VALUE): sensor.sensor_schema(
  69.                 entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
  70.             ),
  71.             cv.Optional(CONF_REED_SWITCH): binary_sensor.BINARY_SENSOR_SCHEMA(
  72.             ),
  73.         }
  74.     )
  75.     .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
  76.     .extend(cv.COMPONENT_SCHEMA)
  77. )
  78.  
  79.  
  80. async def to_code(config):
  81.     var = cg.new_Pvariable(config[CONF_ID])
  82.     await cg.register_component(var, config)
  83.     await esp32_ble_tracker.register_ble_device(var, config)
  84.  
  85.     cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
  86.  
  87.     if CONF_TEMPERATURE in config:
  88.         sens = await sensor.new_sensor(config[CONF_TEMPERATURE])
  89.         cg.add(var.set_temperature(sens))
  90.     if CONF_HUMIDITY in config:
  91.         sens = await sensor.new_sensor(config[CONF_HUMIDITY])
  92.         cg.add(var.set_humidity(sens))
  93.     if CONF_BATTERY_LEVEL in config:
  94.         sens = await sensor.new_sensor(config[CONF_BATTERY_LEVEL])
  95.         cg.add(var.set_battery_level(sens))
  96.     if CONF_BATTERY_VOLTAGE in config:
  97.         sens = await sensor.new_sensor(config[CONF_BATTERY_VOLTAGE])
  98.         cg.add(var.set_battery_voltage(sens))
  99.     if CONF_FLAG_VALUE in config:
  100.         sens = await sensor.new_sensor(config[CONF_FLAG_VALUE])
  101.         cg.add(var.set_flag_value(sens))
  102.     if CONF_REED_SWITCH in config:
  103.         sens = await binary_sensor.new_binary_sensor(config[CONF_REED_SWITCH])
  104.         cg.add(var.set_reed_switch(sens))        
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement