espHome-NBS-files/external_components/analog_orp/sensor.py

50 lines
1.5 KiB
Python

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.components.adc import sensor as adc
from esphome.const import (
CONF_ID,
DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT
)
chlorine_sensor_ns = cg.esphome_ns.namespace('analog_orp')
ChlorineSensor = chlorine_sensor_ns.class_('ChlorineSensor', cg.PollingComponent, sensor.Sensor)
CONF_SENSOR_PIN = "pin"
CONF_INVERTED = "inverted"
CONF_PRINT_RAW = "print_raw"
CONF_ZERO_POINT = "zero_point"
UNIT_MILLI_VOLT = "mV"
ICON_LIGHTNING_BOLT="mdi:lightning-bolt"
CONFIG_SCHEMA = cv.All(
sensor.sensor_schema(
ChlorineSensor,
unit_of_measurement=UNIT_MILLI_VOLT,
accuracy_decimals=2,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
icon=ICON_LIGHTNING_BOLT
).extend({
cv.Required(CONF_SENSOR_PIN): adc.validate_adc_pin,
cv.Required(CONF_ZERO_POINT): cv.Range(min=0, max=1023),
cv.Optional(CONF_INVERTED, False): cv.boolean,
cv.Optional(CONF_PRINT_RAW, False): cv.boolean
}
).extend(cv.polling_component_schema("60s"))
)
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield sensor.register_sensor(var, config)
sensor_pin = yield cg.gpio_pin_expression(config[CONF_SENSOR_PIN])
cg.add(var.set_pin(sensor_pin))
cg.add(var.set_inverted(config[CONF_INVERTED]))
cg.add(var.set_print_raw(config[CONF_PRINT_RAW]))
cg.add(var.set_zero_point(config[CONF_ZERO_POINT]))