From ff6bed54c66a01942c8881aeb849521959c91f0e Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 6 Sep 2021 08:30:47 +1200 Subject: [PATCH] Remove last_reset_type and convert all those sensors to TOTAL_INCREASING (#2233) --- esphome/components/api/api.proto | 3 +- esphome/components/api/api_connection.cpp | 1 - esphome/components/api/api_pb2.cpp | 8 +-- esphome/components/api/api_pb2.h | 2 +- esphome/components/atm90e32/sensor.py | 8 +-- esphome/components/demo/__init__.py | 6 +- esphome/components/demo/demo_sensor.h | 4 +- esphome/components/dsmr/sensor.py | 58 +++---------------- esphome/components/havells_solar/sensor.py | 13 ++--- esphome/components/hlw8012/sensor.py | 5 +- esphome/components/pulse_counter/sensor.py | 4 +- esphome/components/pulse_meter/sensor.py | 5 +- esphome/components/pzem004t/sensor.py | 5 +- esphome/components/pzemac/sensor.py | 5 +- esphome/components/sdm_meter/sensor.py | 14 ++--- esphome/components/selec_meter/sensor.py | 23 +++----- esphome/components/sensor/__init__.py | 28 +-------- esphome/components/sensor/sensor.cpp | 13 ----- esphome/components/sensor/sensor.h | 24 -------- .../components/total_daily_energy/sensor.py | 6 +- esphome/const.py | 8 --- script/api_protobuf/api_protobuf.py | 8 +-- 22 files changed, 60 insertions(+), 191 deletions(-) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index e3ef2d7c9e..7648ffeaa2 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -473,7 +473,8 @@ message ListEntitiesSensorResponse { bool force_update = 8; string device_class = 9; SensorStateClass state_class = 10; - SensorLastResetType last_reset_type = 11; + // Last reset type removed in 2021.9.0 + SensorLastResetType legacy_last_reset_type = 11; bool disabled_by_default = 12; } message SensorStateResponse { diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 2bf3af5f65..99e611be10 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -418,7 +418,6 @@ bool APIConnection::send_sensor_info(sensor::Sensor *sensor) { msg.force_update = sensor->get_force_update(); msg.device_class = sensor->get_device_class(); msg.state_class = static_cast(sensor->state_class); - msg.last_reset_type = static_cast(sensor->last_reset_type); msg.disabled_by_default = sensor->is_disabled_by_default(); return this->send_list_entities_sensor_response(msg); diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index f5860bee64..d6b85d257c 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -1817,7 +1817,7 @@ bool ListEntitiesSensorResponse::decode_varint(uint32_t field_id, ProtoVarInt va return true; } case 11: { - this->last_reset_type = value.as_enum(); + this->legacy_last_reset_type = value.as_enum(); return true; } case 12: { @@ -1879,7 +1879,7 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_bool(8, this->force_update); buffer.encode_string(9, this->device_class); buffer.encode_enum(10, this->state_class); - buffer.encode_enum(11, this->last_reset_type); + buffer.encode_enum(11, this->legacy_last_reset_type); buffer.encode_bool(12, this->disabled_by_default); } #ifdef HAS_PROTO_MESSAGE_DUMP @@ -1928,8 +1928,8 @@ void ListEntitiesSensorResponse::dump_to(std::string &out) const { out.append(proto_enum_to_string(this->state_class)); out.append("\n"); - out.append(" last_reset_type: "); - out.append(proto_enum_to_string(this->last_reset_type)); + out.append(" legacy_last_reset_type: "); + out.append(proto_enum_to_string(this->legacy_last_reset_type)); out.append("\n"); out.append(" disabled_by_default: "); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 93bfcd9b55..1371ab5248 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -510,7 +510,7 @@ class ListEntitiesSensorResponse : public ProtoMessage { bool force_update{false}; std::string device_class{}; enums::SensorStateClass state_class{}; - enums::SensorLastResetType last_reset_type{}; + enums::SensorLastResetType legacy_last_reset_type{}; bool disabled_by_default{false}; void encode(ProtoWriteBuffer buffer) const override; #ifdef HAS_PROTO_MESSAGE_DUMP diff --git a/esphome/components/atm90e32/sensor.py b/esphome/components/atm90e32/sensor.py index 28b49604ff..05e5250d89 100644 --- a/esphome/components/atm90e32/sensor.py +++ b/esphome/components/atm90e32/sensor.py @@ -19,8 +19,8 @@ from esphome.const import ( DEVICE_CLASS_VOLTAGE, ICON_LIGHTBULB, ICON_CURRENT_AC, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_HERTZ, UNIT_VOLT, UNIT_AMPERE, @@ -94,15 +94,13 @@ ATM90E32_PHASE_SCHEMA = cv.Schema( unit_of_measurement=UNIT_WATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_REVERSE_ACTIVE_ENERGY): sensor.sensor_schema( unit_of_measurement=UNIT_WATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_GAIN_VOLTAGE, default=7305): cv.uint16_t, cv.Optional(CONF_GAIN_CT, default=27961): cv.uint16_t, diff --git a/esphome/components/demo/__init__.py b/esphome/components/demo/__init__.py index b3ea47d869..fae8a2b07d 100644 --- a/esphome/components/demo/__init__.py +++ b/esphome/components/demo/__init__.py @@ -19,7 +19,6 @@ from esphome.const import ( CONF_ICON, CONF_ID, CONF_INVERTED, - CONF_LAST_RESET_TYPE, CONF_MAX_VALUE, CONF_MIN_VALUE, CONF_NAME, @@ -40,8 +39,8 @@ from esphome.const import ( ICON_BLUR, ICON_EMPTY, ICON_THERMOMETER, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_CELSIUS, UNIT_EMPTY, UNIT_PERCENT, @@ -336,8 +335,7 @@ CONFIG_SCHEMA = cv.Schema( CONF_UNIT_OF_MEASUREMENT: UNIT_WATT_HOURS, CONF_ACCURACY_DECIMALS: 0, CONF_DEVICE_CLASS: DEVICE_CLASS_ENERGY, - CONF_STATE_CLASS: STATE_CLASS_MEASUREMENT, - CONF_LAST_RESET_TYPE: LAST_RESET_TYPE_AUTO, + CONF_STATE_CLASS: STATE_CLASS_TOTAL_INCREASING, }, ], ): [ diff --git a/esphome/components/demo/demo_sensor.h b/esphome/components/demo/demo_sensor.h index 117468793b..344aaf26f8 100644 --- a/esphome/components/demo/demo_sensor.h +++ b/esphome/components/demo/demo_sensor.h @@ -11,8 +11,8 @@ class DemoSensor : public sensor::Sensor, public PollingComponent { public: void update() override { float val = random_float(); - bool is_auto = this->last_reset_type == sensor::LAST_RESET_TYPE_AUTO; - if (is_auto) { + bool increasing = this->state_class == sensor::STATE_CLASS_TOTAL_INCREASING; + if (increasing) { float base = isnan(this->state) ? 0.0f : this->state; this->publish_state(base + val * 10); } else { diff --git a/esphome/components/dsmr/sensor.py b/esphome/components/dsmr/sensor.py index 84b263c2d5..2c05651d67 100644 --- a/esphome/components/dsmr/sensor.py +++ b/esphome/components/dsmr/sensor.py @@ -9,9 +9,9 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, - LAST_RESET_TYPE_NEVER, STATE_CLASS_MEASUREMENT, STATE_CLASS_NONE, + STATE_CLASS_TOTAL_INCREASING, UNIT_AMPERE, UNIT_EMPTY, UNIT_VOLT, @@ -26,52 +26,22 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_DSMR_ID): cv.use_id(Dsmr), cv.Optional("energy_delivered_lux"): sensor.sensor_schema( - "kWh", - ICON_EMPTY, - 3, - DEVICE_CLASS_ENERGY, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "kWh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("energy_delivered_tariff1"): sensor.sensor_schema( - "kWh", - ICON_EMPTY, - 3, - DEVICE_CLASS_ENERGY, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "kWh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("energy_delivered_tariff2"): sensor.sensor_schema( - "kWh", - ICON_EMPTY, - 3, - DEVICE_CLASS_ENERGY, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "kWh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("energy_returned_lux"): sensor.sensor_schema( - "kWh", - ICON_EMPTY, - 3, - DEVICE_CLASS_ENERGY, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "kWh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("energy_returned_tariff1"): sensor.sensor_schema( - "kWh", - ICON_EMPTY, - 3, - DEVICE_CLASS_ENERGY, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "kWh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("energy_returned_tariff2"): sensor.sensor_schema( - "kWh", - ICON_EMPTY, - 3, - DEVICE_CLASS_ENERGY, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "kWh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("total_imported_energy"): sensor.sensor_schema( "kvarh", ICON_EMPTY, 3, DEVICE_CLASS_ENERGY, STATE_CLASS_NONE @@ -176,20 +146,10 @@ CONFIG_SCHEMA = cv.Schema( UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_NONE ), cv.Optional("gas_delivered"): sensor.sensor_schema( - "m³", - ICON_EMPTY, - 3, - DEVICE_CLASS_GAS, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "m³", ICON_EMPTY, 3, DEVICE_CLASS_GAS, STATE_CLASS_TOTAL_INCREASING ), cv.Optional("gas_delivered_be"): sensor.sensor_schema( - "m³", - ICON_EMPTY, - 3, - DEVICE_CLASS_GAS, - STATE_CLASS_MEASUREMENT, - LAST_RESET_TYPE_NEVER, + "m³", ICON_EMPTY, 3, DEVICE_CLASS_GAS, STATE_CLASS_TOTAL_INCREASING ), } ).extend(cv.COMPONENT_SCHEMA) diff --git a/esphome/components/havells_solar/sensor.py b/esphome/components/havells_solar/sensor.py index 1d685b9b2e..3ec12d5b83 100644 --- a/esphome/components/havells_solar/sensor.py +++ b/esphome/components/havells_solar/sensor.py @@ -13,9 +13,8 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_CURRENT_AC, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, - STATE_CLASS_NONE, + STATE_CLASS_TOTAL_INCREASING, UNIT_AMPERE, UNIT_DEGREES, UNIT_HERTZ, @@ -143,25 +142,23 @@ CONFIG_SCHEMA = ( unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_TOTAL_ENERGY_PRODUCTION): sensor.sensor_schema( unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=0, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_TOTAL_GENERATION_TIME): sensor.sensor_schema( unit_of_measurement=UNIT_HOURS, accuracy_decimals=0, - state_class=STATE_CLASS_NONE, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_TODAY_GENERATION_TIME): sensor.sensor_schema( unit_of_measurement=UNIT_MINUTE, accuracy_decimals=0, - state_class=STATE_CLASS_NONE, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_INVERTER_MODULE_TEMP): sensor.sensor_schema( unit_of_measurement=UNIT_DEGREES, diff --git a/esphome/components/hlw8012/sensor.py b/esphome/components/hlw8012/sensor.py index 75590f8572..11e9c8e4d4 100644 --- a/esphome/components/hlw8012/sensor.py +++ b/esphome/components/hlw8012/sensor.py @@ -18,8 +18,8 @@ from esphome.const import ( DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -78,8 +78,7 @@ CONFIG_SCHEMA = cv.Schema( unit_of_measurement=UNIT_WATT_HOURS, accuracy_decimals=1, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_CURRENT_RESISTOR, default=0.001): cv.resistance, cv.Optional(CONF_VOLTAGE_DIVIDER, default=2351): cv.positive_float, diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index 767728fc80..c7b89d41b0 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -13,7 +13,7 @@ from esphome.const import ( CONF_TOTAL, ICON_PULSE, STATE_CLASS_MEASUREMENT, - STATE_CLASS_NONE, + STATE_CLASS_TOTAL_INCREASING, UNIT_PULSES_PER_MINUTE, UNIT_PULSES, ) @@ -95,7 +95,7 @@ CONFIG_SCHEMA = ( unit_of_measurement=UNIT_PULSES, icon=ICON_PULSE, accuracy_decimals=0, - state_class=STATE_CLASS_NONE, + state_class=STATE_CLASS_TOTAL_INCREASING, ), } ) diff --git a/esphome/components/pulse_meter/sensor.py b/esphome/components/pulse_meter/sensor.py index 18da842bad..454cb3a69d 100644 --- a/esphome/components/pulse_meter/sensor.py +++ b/esphome/components/pulse_meter/sensor.py @@ -11,8 +11,8 @@ from esphome.const import ( CONF_TOTAL, CONF_VALUE, ICON_PULSE, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_PULSES, UNIT_PULSES_PER_MINUTE, ) @@ -64,8 +64,7 @@ CONFIG_SCHEMA = sensor.sensor_schema( unit_of_measurement=UNIT_PULSES, icon=ICON_PULSE, accuracy_decimals=0, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), } ) diff --git a/esphome/components/pzem004t/sensor.py b/esphome/components/pzem004t/sensor.py index 23502e849a..70dec82c3f 100644 --- a/esphome/components/pzem004t/sensor.py +++ b/esphome/components/pzem004t/sensor.py @@ -11,8 +11,8 @@ from esphome.const import ( DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -50,8 +50,7 @@ CONFIG_SCHEMA = ( unit_of_measurement=UNIT_WATT_HOURS, accuracy_decimals=0, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), } ) diff --git a/esphome/components/pzemac/sensor.py b/esphome/components/pzemac/sensor.py index 1616bf0ace..b6697e3d19 100644 --- a/esphome/components/pzemac/sensor.py +++ b/esphome/components/pzemac/sensor.py @@ -15,8 +15,8 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_ENERGY, ICON_CURRENT_AC, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_HERTZ, UNIT_VOLT, UNIT_AMPERE, @@ -55,8 +55,7 @@ CONFIG_SCHEMA = ( unit_of_measurement=UNIT_WATT_HOURS, accuracy_decimals=0, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_FREQUENCY): sensor.sensor_schema( unit_of_measurement=UNIT_HERTZ, diff --git a/esphome/components/sdm_meter/sensor.py b/esphome/components/sdm_meter/sensor.py index ffb88af5ac..8a0d9674a7 100644 --- a/esphome/components/sdm_meter/sensor.py +++ b/esphome/components/sdm_meter/sensor.py @@ -23,8 +23,8 @@ from esphome.const import ( DEVICE_CLASS_VOLTAGE, ICON_CURRENT_AC, ICON_FLASH, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_AMPERE, UNIT_DEGREES, UNIT_HERTZ, @@ -104,29 +104,25 @@ CONFIG_SCHEMA = ( unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_EXPORT_ACTIVE_ENERGY): sensor.sensor_schema( unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_IMPORT_REACTIVE_ENERGY): sensor.sensor_schema( unit_of_measurement=UNIT_KILOVOLT_AMPS_REACTIVE_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), cv.Optional(CONF_EXPORT_REACTIVE_ENERGY): sensor.sensor_schema( unit_of_measurement=UNIT_KILOVOLT_AMPS_REACTIVE_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), } ) diff --git a/esphome/components/selec_meter/sensor.py b/esphome/components/selec_meter/sensor.py index 2d05d00380..168d3a3db2 100644 --- a/esphome/components/selec_meter/sensor.py +++ b/esphome/components/selec_meter/sensor.py @@ -20,8 +20,8 @@ from esphome.const import ( DEVICE_CLASS_POWER_FACTOR, DEVICE_CLASS_VOLTAGE, ICON_CURRENT_AC, - LAST_RESET_TYPE_AUTO, STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL_INCREASING, UNIT_AMPERE, UNIT_HERTZ, UNIT_VOLT, @@ -54,50 +54,43 @@ SENSORS = { unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_IMPORT_ACTIVE_ENERGY: sensor.sensor_schema( unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_EXPORT_ACTIVE_ENERGY: sensor.sensor_schema( unit_of_measurement=UNIT_KILOWATT_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_TOTAL_REACTIVE_ENERGY: sensor.sensor_schema( unit_of_measurement=UNIT_KILOVOLT_AMPS_REACTIVE_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_IMPORT_REACTIVE_ENERGY: sensor.sensor_schema( unit_of_measurement=UNIT_KILOVOLT_AMPS_REACTIVE_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_EXPORT_REACTIVE_ENERGY: sensor.sensor_schema( unit_of_measurement=UNIT_KILOVOLT_AMPS_REACTIVE_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_APPARENT_ENERGY: sensor.sensor_schema( unit_of_measurement=UNIT_KILOVOLT_AMPS_HOURS, accuracy_decimals=2, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ), CONF_ACTIVE_POWER: sensor.sensor_schema( unit_of_measurement=UNIT_WATT, diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 1bb4e25a17..bf0dbf62c4 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -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]) diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 1a5c76db51..1dbc1c901a 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -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_; diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index f0d7ba4887..34b8b26a54 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -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 * diff --git a/esphome/components/total_daily_energy/sensor.py b/esphome/components/total_daily_energy/sensor.py index f1449432a0..46eaac98eb 100644 --- a/esphome/components/total_daily_energy/sensor.py +++ b/esphome/components/total_daily_energy/sensor.py @@ -5,9 +5,8 @@ from esphome.const import ( CONF_ID, CONF_TIME_ID, DEVICE_CLASS_ENERGY, - LAST_RESET_TYPE_AUTO, - STATE_CLASS_MEASUREMENT, CONF_METHOD, + STATE_CLASS_TOTAL_INCREASING, ) DEPENDENCIES = ["time"] @@ -28,8 +27,7 @@ TotalDailyEnergy = total_daily_energy_ns.class_( CONFIG_SCHEMA = ( sensor.sensor_schema( device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_MEASUREMENT, - last_reset_type=LAST_RESET_TYPE_AUTO, + state_class=STATE_CLASS_TOTAL_INCREASING, ) .extend( { diff --git a/esphome/const.py b/esphome/const.py index 7132cb1d1d..5c5037ceb2 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -323,7 +323,6 @@ CONF_KEY = "key" CONF_LAMBDA = "lambda" CONF_LAST_CONFIDENCE = "last_confidence" CONF_LAST_FINGER_ID = "last_finger_id" -CONF_LAST_RESET_TYPE = "last_reset_type" CONF_LATITUDE = "latitude" CONF_LENGTH = "length" CONF_LEVEL = "level" @@ -857,10 +856,3 @@ STATE_CLASS_MEASUREMENT = "measurement" # The state represents a total that only increases, a decrease is considered a reset. STATE_CLASS_TOTAL_INCREASING = "total_increasing" - -# This sensor does not support resetting. ie, it is not accumulative -LAST_RESET_TYPE_NONE = "" -# This sensor is expected to never reset its value -LAST_RESET_TYPE_NEVER = "never" -# This sensor may reset and Home Assistant will watch for this -LAST_RESET_TYPE_AUTO = "auto" diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 6983090fd9..7ccdc5a24e 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -778,9 +778,9 @@ def build_service_message_type(mt): hout += f"bool {func}(const {mt.name} &msg);\n" cout += f"bool {class_name}::{func}(const {mt.name} &msg) {{\n" if log: - cout += f'#ifdef HAS_PROTO_MESSAGE_DUMP\n' + cout += f"#ifdef HAS_PROTO_MESSAGE_DUMP\n" cout += f' ESP_LOGVV(TAG, "{func}: %s", msg.dump().c_str());\n' - cout += f'#endif\n' + cout += f"#endif\n" # cout += f' this->set_nodelay({str(nodelay).lower()});\n' cout += f" return this->send_message_<{mt.name}>(msg, {id_});\n" cout += f"}}\n" @@ -794,9 +794,9 @@ def build_service_message_type(mt): case += f"{mt.name} msg;\n" case += f"msg.decode(msg_data, msg_size);\n" if log: - case += f'#ifdef HAS_PROTO_MESSAGE_DUMP\n' + case += f"#ifdef HAS_PROTO_MESSAGE_DUMP\n" case += f'ESP_LOGVV(TAG, "{func}: %s", msg.dump().c_str());\n' - case += f'#endif\n' + case += f"#endif\n" case += f"this->{func}(msg);\n" if ifdef is not None: case += f"#endif\n"