mirror of
https://github.com/esphome/esphome.git
synced 2024-12-12 08:24:55 +01:00
11fb54c74e
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
import esphome.codegen as cg
|
|
import esphome.config_validation as cv
|
|
from esphome.components import sensor, modbus
|
|
from esphome.const import (
|
|
CONF_CURRENT,
|
|
CONF_ID,
|
|
CONF_POWER,
|
|
CONF_VOLTAGE,
|
|
DEVICE_CLASS_CURRENT,
|
|
DEVICE_CLASS_POWER,
|
|
DEVICE_CLASS_VOLTAGE,
|
|
ICON_EMPTY,
|
|
STATE_CLASS_MEASUREMENT,
|
|
UNIT_VOLT,
|
|
UNIT_AMPERE,
|
|
UNIT_WATT,
|
|
)
|
|
|
|
AUTO_LOAD = ["modbus"]
|
|
|
|
pzemdc_ns = cg.esphome_ns.namespace("pzemdc")
|
|
PZEMDC = pzemdc_ns.class_("PZEMDC", cg.PollingComponent, modbus.ModbusDevice)
|
|
|
|
CONFIG_SCHEMA = (
|
|
cv.Schema(
|
|
{
|
|
cv.GenerateID(): cv.declare_id(PZEMDC),
|
|
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
|
|
UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT
|
|
),
|
|
cv.Optional(CONF_CURRENT): sensor.sensor_schema(
|
|
UNIT_AMPERE,
|
|
ICON_EMPTY,
|
|
3,
|
|
DEVICE_CLASS_CURRENT,
|
|
STATE_CLASS_MEASUREMENT,
|
|
),
|
|
cv.Optional(CONF_POWER): sensor.sensor_schema(
|
|
UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT
|
|
),
|
|
}
|
|
)
|
|
.extend(cv.polling_component_schema("60s"))
|
|
.extend(modbus.modbus_device_schema(0x01))
|
|
)
|
|
|
|
|
|
async def to_code(config):
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
|
await cg.register_component(var, config)
|
|
await modbus.register_modbus_device(var, config)
|
|
|
|
if CONF_VOLTAGE in config:
|
|
conf = config[CONF_VOLTAGE]
|
|
sens = await sensor.new_sensor(conf)
|
|
cg.add(var.set_voltage_sensor(sens))
|
|
if CONF_CURRENT in config:
|
|
conf = config[CONF_CURRENT]
|
|
sens = await sensor.new_sensor(conf)
|
|
cg.add(var.set_current_sensor(sens))
|
|
if CONF_POWER in config:
|
|
conf = config[CONF_POWER]
|
|
sens = await sensor.new_sensor(conf)
|
|
cg.add(var.set_power_sensor(sens))
|