mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-31 10:08:27 +02:00
Deprecate virtual methods to set entity properties (#3021)
This commit is contained in:
@@ -2,7 +2,14 @@ import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import sensor
|
||||
from esphome.const import CONF_ICON, CONF_ID, CONF_SENSOR, CONF_RESTORE
|
||||
from esphome.const import (
|
||||
CONF_ICON,
|
||||
CONF_ID,
|
||||
CONF_SENSOR,
|
||||
CONF_RESTORE,
|
||||
CONF_UNIT_OF_MEASUREMENT,
|
||||
CONF_ACCURACY_DECIMALS,
|
||||
)
|
||||
from esphome.core.entity_helpers import inherit_property_from
|
||||
|
||||
integration_ns = cg.esphome_ns.namespace("integration")
|
||||
@@ -30,6 +37,18 @@ CONF_TIME_UNIT = "time_unit"
|
||||
CONF_INTEGRATION_METHOD = "integration_method"
|
||||
CONF_MIN_SAVE_INTERVAL = "min_save_interval"
|
||||
|
||||
|
||||
def inherit_unit_of_measurement(uom, config):
|
||||
suffix = config[CONF_TIME_UNIT]
|
||||
if uom.endswith("/" + suffix):
|
||||
return uom[0 : -len("/" + suffix)]
|
||||
return uom + suffix
|
||||
|
||||
|
||||
def inherit_accuracy_decimals(decimals, config):
|
||||
return decimals + 2
|
||||
|
||||
|
||||
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(IntegrationSensor),
|
||||
@@ -51,11 +70,19 @@ FINAL_VALIDATE_SCHEMA = cv.All(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.use_id(IntegrationSensor),
|
||||
cv.Optional(CONF_ICON): cv.icon,
|
||||
cv.Optional(CONF_UNIT_OF_MEASUREMENT): sensor.validate_unit_of_measurement,
|
||||
cv.Optional(CONF_ACCURACY_DECIMALS): sensor.validate_accuracy_decimals,
|
||||
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
|
||||
},
|
||||
extra=cv.ALLOW_EXTRA,
|
||||
),
|
||||
inherit_property_from(CONF_ICON, CONF_SENSOR),
|
||||
inherit_property_from(
|
||||
CONF_UNIT_OF_MEASUREMENT, CONF_SENSOR, transform=inherit_unit_of_measurement
|
||||
),
|
||||
inherit_property_from(
|
||||
CONF_ACCURACY_DECIMALS, CONF_SENSOR, transform=inherit_accuracy_decimals
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user