mirror of
https://github.com/esphome/esphome.git
synced 2025-09-14 17:22:20 +01:00
Adding last_reset_type to sensors that should support it. (#2039)
This commit is contained in:
@@ -17,6 +17,7 @@ from esphome.const import (
|
||||
CONF_ICON,
|
||||
CONF_ID,
|
||||
CONF_INTERNAL,
|
||||
CONF_LAST_RESET_TYPE,
|
||||
CONF_ON_RAW_VALUE,
|
||||
CONF_ON_VALUE,
|
||||
CONF_ON_VALUE_RANGE,
|
||||
@@ -30,6 +31,9 @@ from esphome.const import (
|
||||
CONF_NAME,
|
||||
CONF_MQTT_ID,
|
||||
CONF_FORCE_UPDATE,
|
||||
LAST_RESET_TYPE_AUTO,
|
||||
LAST_RESET_TYPE_NEVER,
|
||||
LAST_RESET_TYPE_NONE,
|
||||
UNIT_EMPTY,
|
||||
ICON_EMPTY,
|
||||
DEVICE_CLASS_EMPTY,
|
||||
@@ -79,6 +83,15 @@ STATE_CLASSES = {
|
||||
}
|
||||
validate_state_class = cv.enum(STATE_CLASSES, lower=True, space="_")
|
||||
|
||||
LastResetTypes = sensor_ns.enum("LastResetType")
|
||||
LAST_RESET_TYPES = {
|
||||
LAST_RESET_TYPE_NONE: LastResetTypes.LAST_RESET_TYPE_NONE,
|
||||
LAST_RESET_TYPE_NEVER: LastResetTypes.LAST_RESET_TYPE_NEVER,
|
||||
LAST_RESET_TYPE_AUTO: LastResetTypes.LAST_RESET_TYPE_AUTO,
|
||||
}
|
||||
validate_last_reset_type = cv.enum(LAST_RESET_TYPES, lower=True, space="_")
|
||||
|
||||
|
||||
IS_PLATFORM_COMPONENT = True
|
||||
|
||||
|
||||
@@ -168,6 +181,7 @@ SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend(
|
||||
cv.Optional(CONF_ACCURACY_DECIMALS): accuracy_decimals,
|
||||
cv.Optional(CONF_DEVICE_CLASS): device_class,
|
||||
cv.Optional(CONF_STATE_CLASS): validate_state_class,
|
||||
cv.Optional(CONF_LAST_RESET_TYPE): validate_last_reset_type,
|
||||
cv.Optional(CONF_FORCE_UPDATE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_EXPIRE_AFTER): cv.All(
|
||||
cv.requires_component("mqtt"),
|
||||
@@ -202,6 +216,7 @@ def sensor_schema(
|
||||
accuracy_decimals_: int,
|
||||
device_class_: Optional[str] = DEVICE_CLASS_EMPTY,
|
||||
state_class_: Optional[str] = STATE_CLASS_NONE,
|
||||
last_reset_type_: Optional[str] = LAST_RESET_TYPE_NONE,
|
||||
) -> cv.Schema:
|
||||
schema = SENSOR_SCHEMA
|
||||
if unit_of_measurement_ != UNIT_EMPTY:
|
||||
@@ -230,6 +245,14 @@ def sensor_schema(
|
||||
schema = schema.extend(
|
||||
{cv.Optional(CONF_STATE_CLASS, default=state_class_): validate_state_class}
|
||||
)
|
||||
if last_reset_type_ != LAST_RESET_TYPE_NONE:
|
||||
schema = schema.extend(
|
||||
{
|
||||
cv.Optional(
|
||||
CONF_LAST_RESET_TYPE, default=last_reset_type_
|
||||
): validate_last_reset_type
|
||||
}
|
||||
)
|
||||
return schema
|
||||
|
||||
|
||||
@@ -479,6 +502,8 @@ async def setup_sensor_core_(var, config):
|
||||
cg.add(var.set_icon(config[CONF_ICON]))
|
||||
if CONF_ACCURACY_DECIMALS in config:
|
||||
cg.add(var.set_accuracy_decimals(config[CONF_ACCURACY_DECIMALS]))
|
||||
if CONF_LAST_RESET_TYPE in config:
|
||||
cg.add(var.set_last_reset_type(config[CONF_LAST_RESET_TYPE]))
|
||||
cg.add(var.set_force_update(config[CONF_FORCE_UPDATE]))
|
||||
if config.get(CONF_FILTERS): # must exist and not be empty
|
||||
filters = await build_filters(config[CONF_FILTERS])
|
||||
|
Reference in New Issue
Block a user