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

99 lines
3.6 KiB
Python
Raw Normal View History

2024-05-10 10:37:19 +00:00
import esphome.codegen as cg
import esphome.config_validation as cv
2024-05-11 08:54:14 +00:00
from esphome import automation
2024-05-10 10:37:19 +00:00
from esphome.components import sensor
from esphome.components.adc import sensor as adc
from esphome.const import (
CONF_ID,
DEVICE_CLASS_VOLTAGE,
2024-05-11 08:54:14 +00:00
STATE_CLASS_MEASUREMENT,
CONF_TRIGGER_ID
2024-05-10 10:37:19 +00:00
)
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"
2024-05-11 08:54:14 +00:00
CONF_AVERAGE = "average"
CONF_AVERAGE_MESUREMENTS="mesurements"
CONF_SEND_STATE_EVERY="send_state_every"
CONF_ON_READ_VALUE = "on_value_read"
CONF_AVERAGE_ON_VALUE = "on_value"
CONF_AVERAGE_NEW = "on_completely_new_value"
2024-05-10 10:37:19 +00:00
UNIT_MILLI_VOLT = "mV"
ICON_LIGHTNING_BOLT="mdi:lightning-bolt"
2024-05-11 08:54:14 +00:00
ChlorineValueRead = chlorine_sensor_ns.class_(
"ChlorineValueReadTrigger", automation.Trigger.template(cg.float_)
)
ChlorineAverageChange = chlorine_sensor_ns.class_(
"ChlorineAverageChangeTrigger", automation.Trigger.template(cg.float_)
)
ChlorineNewValue = chlorine_sensor_ns.class_(
"ChlorineNewValueTrigger", automation.Trigger.template(cg.float_)
)
2024-05-10 10:37:19 +00:00
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,
2024-05-11 08:54:14 +00:00
cv.Optional(CONF_PRINT_RAW, False): cv.boolean,
cv.Optional(CONF_AVERAGE): cv.Schema({
cv.Required(CONF_AVERAGE_MESUREMENTS): cv.int_range(min=1),
cv.Optional(CONF_SEND_STATE_EVERY): cv.int_range(min=1),
cv.Optional(CONF_ON_READ_VALUE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorineValueRead),
}),
cv.Optional(CONF_AVERAGE_ON_VALUE): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorineAverageChange),
}),
cv.Optional(CONF_AVERAGE_NEW): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChlorineNewValue),
}),
}),
2024-05-10 10:37:19 +00:00
}
).extend(cv.polling_component_schema("60s"))
)
2024-05-11 08:54:14 +00:00
async def to_code(config):
2024-05-10 10:37:19 +00:00
var = cg.new_Pvariable(config[CONF_ID])
2024-05-11 08:54:14 +00:00
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
2024-05-10 10:37:19 +00:00
2024-05-11 08:54:14 +00:00
sensor_pin = await cg.gpio_pin_expression(config[CONF_SENSOR_PIN])
2024-05-10 10:37:19 +00:00
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]))
2024-05-11 08:54:14 +00:00
cg.add(var.set_zero_point(config[CONF_ZERO_POINT]))
if averager_config := config.get(CONF_AVERAGE):
update_each = -1
if CONF_SEND_STATE_EVERY in averager_config:
update_each = averager_config[CONF_SEND_STATE_EVERY]
cg.add(var.init_averager(averager_config[CONF_AVERAGE_MESUREMENTS], update_each))
for conf in averager_config.get(CONF_ON_READ_VALUE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(float, "value")], conf)
for conf in averager_config.get(CONF_AVERAGE_ON_VALUE, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(float, "value")], conf)
for conf in averager_config.get(CONF_AVERAGE_NEW, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(float, "value")], conf)