1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-17 18:52:19 +01:00

Remove last_reset_type and convert all those sensors to TOTAL_INCREASING (#2233)

This commit is contained in:
Jesse Hills
2021-09-06 08:30:47 +12:00
committed by GitHub
parent f9b0666adf
commit ff6bed54c6
22 changed files with 60 additions and 191 deletions

View File

@@ -17,7 +17,6 @@ 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,
@@ -31,9 +30,6 @@ from esphome.const import (
CONF_NAME,
CONF_MQTT_ID,
CONF_FORCE_UPDATE,
LAST_RESET_TYPE_AUTO,
LAST_RESET_TYPE_NEVER,
LAST_RESET_TYPE_NONE,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CARBON_MONOXIDE,
@@ -85,15 +81,6 @@ 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
@@ -183,7 +170,9 @@ SENSOR_SCHEMA = cv.NAMEABLE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend(
cv.Optional(CONF_ACCURACY_DECIMALS): validate_accuracy_decimals,
cv.Optional(CONF_DEVICE_CLASS): validate_device_class,
cv.Optional(CONF_STATE_CLASS): validate_state_class,
cv.Optional(CONF_LAST_RESET_TYPE): validate_last_reset_type,
cv.Optional("last_reset_type"): cv.invalid(
"last_reset_type has been removed since 2021.9.0. state_class: total_increasing should be used for total values."
),
cv.Optional(CONF_FORCE_UPDATE, default=False): cv.boolean,
cv.Optional(CONF_EXPIRE_AFTER): cv.All(
cv.requires_component("mqtt"),
@@ -220,7 +209,6 @@ def sensor_schema(
accuracy_decimals: int = _UNDEF,
device_class: str = _UNDEF,
state_class: str = _UNDEF,
last_reset_type: str = _UNDEF,
) -> cv.Schema:
schema = SENSOR_SCHEMA
if unit_of_measurement is not _UNDEF:
@@ -253,14 +241,6 @@ def sensor_schema(
schema = schema.extend(
{cv.Optional(CONF_STATE_CLASS, default=state_class): validate_state_class}
)
if last_reset_type is not _UNDEF:
schema = schema.extend(
{
cv.Optional(
CONF_LAST_RESET_TYPE, default=last_reset_type
): validate_last_reset_type
}
)
return schema
@@ -511,8 +491,6 @@ 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])

View File

@@ -18,18 +18,6 @@ const char *state_class_to_string(StateClass state_class) {
}
}
const char *last_reset_type_to_string(LastResetType last_reset_type) {
switch (last_reset_type) {
case LAST_RESET_TYPE_NEVER:
return "never";
case LAST_RESET_TYPE_AUTO:
return "auto";
case LAST_RESET_TYPE_NONE:
default:
return "";
}
}
void Sensor::publish_state(float state) {
this->raw_state = state;
this->raw_callback_.call(state);
@@ -80,7 +68,6 @@ void Sensor::set_state_class(const std::string &state_class) {
ESP_LOGW(TAG, "'%s' - Unrecognized state class %s", this->get_name().c_str(), state_class.c_str());
}
}
void Sensor::set_last_reset_type(LastResetType last_reset_type) { this->last_reset_type = last_reset_type; }
std::string Sensor::get_unit_of_measurement() {
if (this->unit_of_measurement_.has_value())
return *this->unit_of_measurement_;

View File

@@ -14,10 +14,6 @@ namespace sensor {
ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
} \
ESP_LOGCONFIG(TAG, "%s State Class: '%s'", prefix, state_class_to_string((obj)->state_class)); \
if ((obj)->state_class == sensor::STATE_CLASS_MEASUREMENT && \
(obj)->last_reset_type != sensor::LAST_RESET_TYPE_NONE) { \
ESP_LOGCONFIG(TAG, "%s Last Reset Type: '%s'", prefix, last_reset_type_to_string((obj)->last_reset_type)); \
} \
ESP_LOGCONFIG(TAG, "%s Unit of Measurement: '%s'", prefix, (obj)->get_unit_of_measurement().c_str()); \
ESP_LOGCONFIG(TAG, "%s Accuracy Decimals: %d", prefix, (obj)->get_accuracy_decimals()); \
if (!(obj)->get_icon().empty()) { \
@@ -42,20 +38,6 @@ enum StateClass : uint8_t {
const char *state_class_to_string(StateClass state_class);
/**
* Sensor last reset types
*/
enum LastResetType : uint8_t {
/// This sensor does not support resetting. ie, it is not accumulative
LAST_RESET_TYPE_NONE = 0,
/// This sensor is expected to never reset its value
LAST_RESET_TYPE_NEVER = 1,
/// This sensor may reset and Home Assistant will watch for this
LAST_RESET_TYPE_AUTO = 2,
};
const char *last_reset_type_to_string(LastResetType last_reset_type);
/** Base-class for all sensors.
*
* A sensor has unit of measurement and can use publish_state to send out a new value with the specified accuracy.
@@ -174,12 +156,6 @@ class Sensor : public Nameable {
*/
virtual std::string device_class();
// The Last reset type of this sensor
LastResetType last_reset_type{LAST_RESET_TYPE_NONE};
/// Manually set the Home Assistant last reset type for this sensor.
void set_last_reset_type(LastResetType last_reset_type);
/** A unique ID for this sensor, empty for no unique id. See unique ID requirements:
* https://developers.home-assistant.io/docs/en/entity_registry_index.html#unique-id-requirements
*