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

63 lines
2.1 KiB
Python
Raw Normal View History

import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import sensor, i2c
from esphome.components.adc import sensor as adc
from esphome.const import (
CONF_ID,
DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT,
)
DEPENDENCIES = ["i2c"]
chlorine_sensor_ns = cg.esphome_ns.namespace('ezo_orp_i2c')
ChlorineSensor = chlorine_sensor_ns.class_('EzoOrpSensor', cg.PollingComponent, sensor.Sensor, i2c.I2CDevice)
UNIT_MILLI_VOLT = "mV"
ICON_TEST_TUBE="mdi:test-tube"
ACTION_CONF_CALIBRATE_TARGET = "calibrate_target"
CalibrateAction = chlorine_sensor_ns.class_("EzoCalibrateAction", automation.Action)
DeviceInfoAction = chlorine_sensor_ns.class_("DeviceInfoAction", automation.Action)
CONFIG_SCHEMA = cv.All(
sensor.sensor_schema(
ChlorineSensor,
unit_of_measurement=UNIT_MILLI_VOLT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
icon=ICON_TEST_TUBE
)
.extend(i2c.i2c_device_schema(0x62))
.extend(cv.polling_component_schema("60s"))
)
ACTION_SCHEMA = cv.Schema({
cv.GenerateID(): cv.use_id(ChlorineSensor)
})
@automation.register_action("sensor.ezo_orp_i2c.calibrate", CalibrateAction, ACTION_SCHEMA.extend({
cv.Required(ACTION_CONF_CALIBRATE_TARGET): cv.int_range(100, 999),
}))
async def calibrate_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[ACTION_CONF_CALIBRATE_TARGET], args, float)
cg.add(var.set_value(template_))
return var
@automation.register_action("sensor.ezo_orp_i2c.print_device_info", DeviceInfoAction, ACTION_SCHEMA)
async def info_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
return cg.new_Pvariable(action_id, template_arg, paren)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
await i2c.register_i2c_device(var, config)