From 11fb54c74e6258e0e770d945c9632494683e77af Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 3 Jun 2021 03:49:56 +0200 Subject: [PATCH] Add support for Sensor state class (#1835) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/adc/sensor.py | 5 +- esphome/components/ade7953/sensor.py | 19 +++++-- esphome/components/ads1115/sensor.py | 5 +- esphome/components/aht10/sensor.py | 13 ++++- esphome/components/am2320/sensor.py | 13 ++++- esphome/components/apds9960/sensor.py | 10 +++- esphome/components/api/api.proto | 6 ++ esphome/components/api/api_connection.cpp | 2 + esphome/components/api/api_pb2.cpp | 19 +++++++ esphome/components/api/api_pb2.h | 5 ++ esphome/components/as3935/sensor.py | 9 ++- .../components/atc_mithermometer/sensor.py | 21 +++++-- esphome/components/atm90e32/sensor.py | 39 ++++++++++--- esphome/components/b_parasite/sensor.py | 21 +++++-- esphome/components/bh1750/sensor.py | 5 +- .../components/binary_sensor_map/sensor.py | 7 ++- .../components/ble_client/sensor/__init__.py | 5 +- esphome/components/ble_rssi/sensor.py | 9 ++- esphome/components/bme280/sensor.py | 19 ++++++- esphome/components/bme680/sensor.py | 25 +++++++-- esphome/components/bme680_bsec/sensor.py | 37 +++++++++--- esphome/components/bmp085/sensor.py | 13 ++++- esphome/components/bmp280/sensor.py | 13 ++++- esphome/components/ccs811/sensor.py | 13 ++++- esphome/components/cse7766/sensor.py | 11 +++- esphome/components/ct_clamp/sensor.py | 5 +- esphome/components/dallas/sensor.py | 5 +- esphome/components/dht/sensor.py | 9 ++- esphome/components/dht12/sensor.py | 13 ++++- esphome/components/duty_cycle/sensor.py | 5 +- esphome/components/esp32_hall/sensor.py | 5 +- esphome/components/fingerprint_grow/sensor.py | 13 +++-- esphome/components/gps/__init__.py | 18 ++++-- esphome/components/hdc1080/sensor.py | 13 ++++- esphome/components/hlw8012/sensor.py | 10 ++-- esphome/components/hm3301/sensor.py | 10 +++- esphome/components/hmc5883l/sensor.py | 6 +- .../homeassistant/sensor/__init__.py | 3 +- esphome/components/htu21d/sensor.py | 13 ++++- esphome/components/hx711/sensor.py | 5 +- esphome/components/ina219/sensor.py | 13 +++-- esphome/components/ina226/sensor.py | 13 +++-- esphome/components/ina3221/sensor.py | 9 +-- .../components/inkbird_ibsth1_mini/sensor.py | 19 ++++++- esphome/components/max31855/sensor.py | 7 ++- esphome/components/max31856/sensor.py | 9 ++- esphome/components/max31865/sensor.py | 5 +- esphome/components/max6675/sensor.py | 12 +++- esphome/components/mcp9808/sensor.py | 12 +++- esphome/components/mhz19/sensor.py | 13 ++++- esphome/components/midea_ac/climate.py | 15 ++++- esphome/components/mpu6050/sensor.py | 15 ++++- .../mqtt_subscribe/sensor/__init__.py | 5 +- esphome/components/ms5611/sensor.py | 13 ++++- esphome/components/ntc/sensor.py | 5 +- esphome/components/pid/sensor/__init__.py | 5 +- esphome/components/pmsx003/sensor.py | 17 +++++- esphome/components/pulse_counter/sensor.py | 12 +++- esphome/components/pulse_meter/sensor.py | 6 +- esphome/components/pulse_width/sensor.py | 13 ++++- esphome/components/pzem004t/sensor.py | 18 ++++-- esphome/components/pzemac/sensor.py | 30 ++++++++-- esphome/components/pzemdc/sensor.py | 11 +++- esphome/components/qmc5883l/sensor.py | 6 +- esphome/components/resistance/sensor.py | 13 ++++- esphome/components/rotary_encoder/sensor.py | 5 +- esphome/components/ruuvitag/sensor.py | 56 +++++++++++++++---- esphome/components/scd30/sensor.py | 19 ++++++- esphome/components/sdm_meter/sensor.py | 39 +++++++++---- esphome/components/sds011/sensor.py | 3 + esphome/components/senseair/sensor.py | 7 ++- esphome/components/sensor/__init__.py | 18 ++++++ esphome/components/sensor/sensor.cpp | 18 ++++++ esphome/components/sensor/sensor.h | 18 ++++++ esphome/components/sgp30/sensor.py | 13 ++++- esphome/components/sgp40/sensor.py | 12 +++- esphome/components/sht3xd/sensor.py | 13 ++++- esphome/components/sht4x/sensor.py | 13 ++++- esphome/components/shtcx/sensor.py | 13 ++++- esphome/components/sm300d2/sensor.py | 38 +++++++++++-- esphome/components/sps30/sensor.py | 41 ++++++++++++-- esphome/components/sts3x/sensor.py | 12 +++- esphome/components/sun/sensor/__init__.py | 5 +- esphome/components/tcs34725/sensor.py | 7 ++- .../components/template/sensor/__init__.py | 11 +++- esphome/components/tmp102/sensor.py | 12 +++- esphome/components/tmp117/sensor.py | 5 +- esphome/components/tof10120/sensor.py | 16 +++++- esphome/components/tsl2561/sensor.py | 5 +- esphome/components/tx20/sensor.py | 10 +++- esphome/components/ultrasonic/sensor.py | 9 ++- esphome/components/uptime/sensor.py | 12 +++- esphome/components/vl53l0x/sensor.py | 9 ++- esphome/components/wifi_signal/sensor.py | 7 ++- esphome/components/xiaomi_cgd1/sensor.py | 19 ++++++- esphome/components/xiaomi_cgdk2/sensor.py | 19 ++++++- esphome/components/xiaomi_cgg1/sensor.py | 19 ++++++- esphome/components/xiaomi_gcls002/sensor.py | 25 +++++++-- esphome/components/xiaomi_hhccjcy01/sensor.py | 31 ++++++++-- .../components/xiaomi_hhccpot002/sensor.py | 13 ++++- esphome/components/xiaomi_jqjcy01ym/sensor.py | 20 ++++++- esphome/components/xiaomi_lywsd02/sensor.py | 19 ++++++- .../components/xiaomi_lywsd03mmc/sensor.py | 19 ++++++- esphome/components/xiaomi_lywsdcgq/sensor.py | 19 ++++++- esphome/components/xiaomi_mhoc401/sensor.py | 19 ++++++- esphome/components/xiaomi_miscale/sensor.py | 7 ++- esphome/components/xiaomi_miscale2/sensor.py | 9 ++- .../xiaomi_mjyd02yla/binary_sensor.py | 16 +++++- .../components/xiaomi_wx08zm/binary_sensor.py | 9 ++- esphome/components/zyaura/sensor.py | 15 ++++- esphome/const.py | 7 +++ tests/test1.yaml | 2 +- 112 files changed, 1237 insertions(+), 269 deletions(-) diff --git a/esphome/components/adc/sensor.py b/esphome/components/adc/sensor.py index f988563227..90561679b7 100644 --- a/esphome/components/adc/sensor.py +++ b/esphome/components/adc/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_PIN, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, ) @@ -35,7 +36,9 @@ ADCSensor = adc_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE) + sensor.sensor_schema( + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(ADCSensor), diff --git a/esphome/components/ade7953/sensor.py b/esphome/components/ade7953/sensor.py index b91e8d3bea..90873f1a5e 100644 --- a/esphome/components/ade7953/sensor.py +++ b/esphome/components/ade7953/sensor.py @@ -9,6 +9,7 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -31,19 +32,27 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(ADE7953), cv.Optional(CONF_IRQ_PIN): pins.input_pin, cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT_A): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, + ICON_EMPTY, + 2, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CURRENT_B): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, + ICON_EMPTY, + 2, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ACTIVE_POWER_A): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_ACTIVE_POWER_B): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), } ) diff --git a/esphome/components/ads1115/sensor.py b/esphome/components/ads1115/sensor.py index 4585a758b5..c521769279 100644 --- a/esphome/components/ads1115/sensor.py +++ b/esphome/components/ads1115/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_MULTIPLEXER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, CONF_ID, ) @@ -51,7 +52,9 @@ ADS1115Sensor = ads1115_ns.class_( CONF_ADS1115_ID = "ads1115_id" CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE) + sensor.sensor_schema( + UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(ADS1115Sensor), diff --git a/esphome/components/aht10/sensor.py b/esphome/components/aht10/sensor.py index 2e553638d1..35168be54a 100644 --- a/esphome/components/aht10/sensor.py +++ b/esphome/components/aht10/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ) @@ -22,10 +23,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(AHT10Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 2, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 2, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 2, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 2, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/am2320/sensor.py b/esphome/components/am2320/sensor.py index c0a6a60437..5d6cb9eded 100644 --- a/esphome/components/am2320/sensor.py +++ b/esphome/components/am2320/sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_TEMPERATURE, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_PERCENT, @@ -24,10 +25,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(AM2320Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/apds9960/sensor.py b/esphome/components/apds9960/sensor.py index 53633693a2..cb0c52735d 100644 --- a/esphome/components/apds9960/sensor.py +++ b/esphome/components/apds9960/sensor.py @@ -1,7 +1,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor -from esphome.const import CONF_TYPE, DEVICE_CLASS_EMPTY, UNIT_PERCENT, ICON_LIGHTBULB +from esphome.const import ( + CONF_TYPE, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_PERCENT, + ICON_LIGHTBULB, +) from . import APDS9960, CONF_APDS9960_ID DEPENDENCIES = ["apds9960"] @@ -15,7 +21,7 @@ TYPES = { } CONFIG_SCHEMA = sensor.sensor_schema( - UNIT_PERCENT, ICON_LIGHTBULB, 1, DEVICE_CLASS_EMPTY + UNIT_PERCENT, ICON_LIGHTBULB, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ).extend( { cv.Required(CONF_TYPE): cv.one_of(*TYPES, upper=True), diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index e228fe6874..43e23f640b 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -409,6 +409,11 @@ message LightCommandRequest { } // ==================== SENSOR ==================== +enum SensorStateClass { + STATE_CLASS_NONE = 0; + STATE_CLASS_MEASUREMENT = 1; +} + message ListEntitiesSensorResponse { option (id) = 16; option (source) = SOURCE_SERVER; @@ -424,6 +429,7 @@ message ListEntitiesSensorResponse { int32 accuracy_decimals = 7; bool force_update = 8; string device_class = 9; + SensorStateClass state_class = 10; } message SensorStateResponse { option (id) = 25; diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index ac04e1f09f..b5fc9b245c 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -395,6 +395,8 @@ bool APIConnection::send_sensor_info(sensor::Sensor *sensor) { msg.accuracy_decimals = sensor->get_accuracy_decimals(); msg.force_update = sensor->get_force_update(); msg.device_class = sensor->get_device_class(); + msg.state_class = static_cast(sensor->state_class); + return this->send_list_entities_sensor_response(msg); } #endif diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 8f8e2abf58..38efbc2ec4 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -62,6 +62,16 @@ template<> const char *proto_enum_to_string(enums::FanDirec return "UNKNOWN"; } } +template<> const char *proto_enum_to_string(enums::SensorStateClass value) { + switch (value) { + case enums::STATE_CLASS_NONE: + return "STATE_CLASS_NONE"; + case enums::STATE_CLASS_MEASUREMENT: + return "STATE_CLASS_MEASUREMENT"; + default: + return "UNKNOWN"; + } +} template<> const char *proto_enum_to_string(enums::LogLevel value) { switch (value) { case enums::LOG_LEVEL_NONE: @@ -1507,6 +1517,10 @@ bool ListEntitiesSensorResponse::decode_varint(uint32_t field_id, ProtoVarInt va this->force_update = value.as_bool(); return true; } + case 10: { + this->state_class = value.as_enum(); + return true; + } default: return false; } @@ -1561,6 +1575,7 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_int32(7, this->accuracy_decimals); buffer.encode_bool(8, this->force_update); buffer.encode_string(9, this->device_class); + buffer.encode_enum(10, this->state_class); } void ListEntitiesSensorResponse::dump_to(std::string &out) const { char buffer[64]; @@ -1602,6 +1617,10 @@ void ListEntitiesSensorResponse::dump_to(std::string &out) const { out.append(" device_class: "); out.append("'").append(this->device_class).append("'"); out.append("\n"); + + out.append(" state_class: "); + out.append(proto_enum_to_string(this->state_class)); + out.append("\n"); out.append("}"); } bool SensorStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) { diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 5715a7f50f..e49136b442 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -32,6 +32,10 @@ enum FanDirection : uint32_t { FAN_DIRECTION_FORWARD = 0, FAN_DIRECTION_REVERSE = 1, }; +enum SensorStateClass : uint32_t { + STATE_CLASS_NONE = 0, + STATE_CLASS_MEASUREMENT = 1, +}; enum LogLevel : uint32_t { LOG_LEVEL_NONE = 0, LOG_LEVEL_ERROR = 1, @@ -408,6 +412,7 @@ class ListEntitiesSensorResponse : public ProtoMessage { int32_t accuracy_decimals{0}; bool force_update{false}; std::string device_class{}; + enums::SensorStateClass state_class{}; void encode(ProtoWriteBuffer buffer) const override; void dump_to(std::string &out) const override; diff --git a/esphome/components/as3935/sensor.py b/esphome/components/as3935/sensor.py index 6afe890d65..a571121742 100644 --- a/esphome/components/as3935/sensor.py +++ b/esphome/components/as3935/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_DISTANCE, CONF_LIGHTNING_ENERGY, DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, UNIT_KILOMETER, UNIT_EMPTY, ICON_SIGNAL_DISTANCE_VARIANT, @@ -18,10 +19,14 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_AS3935_ID): cv.use_id(AS3935), cv.Optional(CONF_DISTANCE): sensor.sensor_schema( - UNIT_KILOMETER, ICON_SIGNAL_DISTANCE_VARIANT, 1, DEVICE_CLASS_EMPTY + UNIT_KILOMETER, + ICON_SIGNAL_DISTANCE_VARIANT, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, ), cv.Optional(CONF_LIGHTNING_ENERGY): sensor.sensor_schema( - UNIT_EMPTY, ICON_FLASH, 1, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_FLASH, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), } ).extend(cv.COMPONENT_SCHEMA) diff --git a/esphome/components/atc_mithermometer/sensor.py b/esphome/components/atc_mithermometer/sensor.py index 161f1f021b..efa3f2b51a 100644 --- a/esphome/components/atc_mithermometer/sensor.py +++ b/esphome/components/atc_mithermometer/sensor.py @@ -13,6 +13,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, UNIT_VOLT, @@ -33,16 +34,28 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(ATCMiThermometer), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), } ) diff --git a/esphome/components/atm90e32/sensor.py b/esphome/components/atm90e32/sensor.py index aaf9320b9e..68ec199bff 100644 --- a/esphome/components/atm90e32/sensor.py +++ b/esphome/components/atm90e32/sensor.py @@ -21,6 +21,7 @@ from esphome.const import ( ICON_EMPTY, ICON_LIGHTBULB, ICON_CURRENT_AC, + STATE_CLASS_MEASUREMENT, UNIT_HERTZ, UNIT_VOLT, UNIT_AMPERE, @@ -63,25 +64,37 @@ ATM90E32Component = atm90e32_ns.class_( ATM90E32_PHASE_SCHEMA = cv.Schema( { cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, + ICON_EMPTY, + 2, + DEVICE_CLASS_VOLTAGE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_REACTIVE_POWER): sensor.sensor_schema( - UNIT_VOLT_AMPS_REACTIVE, ICON_LIGHTBULB, 2, DEVICE_CLASS_EMPTY + UNIT_VOLT_AMPS_REACTIVE, + ICON_LIGHTBULB, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema( - UNIT_EMPTY, ICON_EMPTY, 2, DEVICE_CLASS_POWER_FACTOR + UNIT_EMPTY, + ICON_EMPTY, + 2, + DEVICE_CLASS_POWER_FACTOR, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_FORWARD_ACTIVE_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_REVERSE_ACTIVE_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_GAIN_VOLTAGE, default=7305): cv.uint16_t, cv.Optional(CONF_GAIN_CT, default=27961): cv.uint16_t, @@ -96,10 +109,18 @@ CONFIG_SCHEMA = ( cv.Optional(CONF_PHASE_B): ATM90E32_PHASE_SCHEMA, cv.Optional(CONF_PHASE_C): ATM90E32_PHASE_SCHEMA, cv.Optional(CONF_FREQUENCY): sensor.sensor_schema( - UNIT_HERTZ, ICON_CURRENT_AC, 1, DEVICE_CLASS_EMPTY + UNIT_HERTZ, + ICON_CURRENT_AC, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CHIP_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_LINE_FREQUENCY): cv.enum(LINE_FREQS, upper=True), cv.Optional(CONF_CURRENT_PHASES, default="3"): cv.enum( diff --git a/esphome/components/b_parasite/sensor.py b/esphome/components/b_parasite/sensor.py index 415cae454f..d93e41816b 100644 --- a/esphome/components/b_parasite/sensor.py +++ b/esphome/components/b_parasite/sensor.py @@ -12,6 +12,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, UNIT_VOLT, @@ -32,16 +33,28 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(BParasite), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_MOISTURE): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/bh1750/sensor.py b/esphome/components/bh1750/sensor.py index 6c3c20d9c4..e688241dcc 100644 --- a/esphome/components/bh1750/sensor.py +++ b/esphome/components/bh1750/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_RESOLUTION, DEVICE_CLASS_ILLUMINANCE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_LUX, CONF_MEASUREMENT_DURATION, ) @@ -26,7 +27,9 @@ BH1750Sensor = bh1750_ns.class_( CONF_MEASUREMENT_TIME = "measurement_time" CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_LUX, ICON_EMPTY, 1, DEVICE_CLASS_ILLUMINANCE) + sensor.sensor_schema( + UNIT_LUX, ICON_EMPTY, 1, DEVICE_CLASS_ILLUMINANCE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(BH1750Sensor), diff --git a/esphome/components/binary_sensor_map/sensor.py b/esphome/components/binary_sensor_map/sensor.py index 406a1b7bd1..131a050052 100644 --- a/esphome/components/binary_sensor_map/sensor.py +++ b/esphome/components/binary_sensor_map/sensor.py @@ -12,6 +12,7 @@ from esphome.const import ( ICON_CHECK_CIRCLE_OUTLINE, CONF_BINARY_SENSOR, CONF_GROUP, + STATE_CLASS_NONE, ) DEPENDENCIES = ["binary_sensor"] @@ -34,7 +35,11 @@ entry = { CONFIG_SCHEMA = cv.typed_schema( { CONF_GROUP: sensor.sensor_schema( - UNIT_EMPTY, ICON_CHECK_CIRCLE_OUTLINE, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, + ICON_CHECK_CIRCLE_OUTLINE, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, ).extend( { cv.GenerateID(): cv.declare_id(BinarySensorMap), diff --git a/esphome/components/ble_client/sensor/__init__.py b/esphome/components/ble_client/sensor/__init__.py index a0bc996d87..c6f05932ef 100644 --- a/esphome/components/ble_client/sensor/__init__.py +++ b/esphome/components/ble_client/sensor/__init__.py @@ -5,6 +5,7 @@ from esphome.const import ( DEVICE_CLASS_EMPTY, CONF_ID, CONF_LAMBDA, + STATE_CLASS_NONE, UNIT_EMPTY, ICON_EMPTY, CONF_TRIGGER_ID, @@ -32,7 +33,9 @@ BLESensorNotifyTrigger = ble_client_ns.class_( ) CONFIG_SCHEMA = cv.All( - sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 0, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_EMPTY, ICON_EMPTY, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE + ) .extend( { cv.GenerateID(): cv.declare_id(BLESensor), diff --git a/esphome/components/ble_rssi/sensor.py b/esphome/components/ble_rssi/sensor.py index 279fc4ae19..819b7c6fd7 100644 --- a/esphome/components/ble_rssi/sensor.py +++ b/esphome/components/ble_rssi/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_MAC_ADDRESS, CONF_ID, DEVICE_CLASS_SIGNAL_STRENGTH, + STATE_CLASS_MEASUREMENT, UNIT_DECIBEL, ICON_EMPTY, ) @@ -18,7 +19,13 @@ BLERSSISensor = ble_rssi_ns.class_( ) CONFIG_SCHEMA = cv.All( - sensor.sensor_schema(UNIT_DECIBEL, ICON_EMPTY, 0, DEVICE_CLASS_SIGNAL_STRENGTH) + sensor.sensor_schema( + UNIT_DECIBEL, + ICON_EMPTY, + 0, + DEVICE_CLASS_SIGNAL_STRENGTH, + STATE_CLASS_MEASUREMENT, + ) .extend( { cv.GenerateID(): cv.declare_id(BLERSSISensor), diff --git a/esphome/components/bme280/sensor.py b/esphome/components/bme280/sensor.py index 2b1b8825f8..8c6cc7ae56 100644 --- a/esphome/components/bme280/sensor.py +++ b/esphome/components/bme280/sensor.py @@ -12,6 +12,7 @@ from esphome.const import ( DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_HECTOPASCAL, UNIT_PERCENT, @@ -48,7 +49,11 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(BME280Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( @@ -57,7 +62,11 @@ CONFIG_SCHEMA = ( } ), cv.Optional(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_EMPTY, 1, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_EMPTY, + 1, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( @@ -66,7 +75,11 @@ CONFIG_SCHEMA = ( } ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( diff --git a/esphome/components/bme680/sensor.py b/esphome/components/bme680/sensor.py index 08a5b3b0a2..eaa158c9f8 100644 --- a/esphome/components/bme680/sensor.py +++ b/esphome/components/bme680/sensor.py @@ -16,6 +16,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_OHM, ICON_GAS_CYLINDER, UNIT_CELSIUS, @@ -58,7 +59,11 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(BME680Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( @@ -67,7 +72,11 @@ CONFIG_SCHEMA = ( } ), cv.Optional(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_EMPTY, 1, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_EMPTY, + 1, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( @@ -76,7 +85,11 @@ CONFIG_SCHEMA = ( } ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( @@ -85,7 +98,11 @@ CONFIG_SCHEMA = ( } ), cv.Optional(CONF_GAS_RESISTANCE): sensor.sensor_schema( - UNIT_OHM, ICON_GAS_CYLINDER, 1, DEVICE_CLASS_EMPTY + UNIT_OHM, + ICON_GAS_CYLINDER, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum( IIR_FILTER_OPTIONS, upper=True diff --git a/esphome/components/bme680_bsec/sensor.py b/esphome/components/bme680_bsec/sensor.py index 5634b21e75..4520bf3480 100644 --- a/esphome/components/bme680_bsec/sensor.py +++ b/esphome/components/bme680_bsec/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_EMPTY, UNIT_HECTOPASCAL, @@ -53,34 +54,54 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_BME680_BSEC_ID): cv.use_id(BME680BSECComponent), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_THERMOMETER, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_THERMOMETER, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ).extend( {cv.Optional(CONF_SAMPLE_RATE): cv.enum(SAMPLE_RATE_OPTIONS, upper=True)} ), cv.Optional(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_GAUGE, 1, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_GAUGE, + 1, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ).extend( {cv.Optional(CONF_SAMPLE_RATE): cv.enum(SAMPLE_RATE_OPTIONS, upper=True)} ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_WATER_PERCENT, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_WATER_PERCENT, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ).extend( {cv.Optional(CONF_SAMPLE_RATE): cv.enum(SAMPLE_RATE_OPTIONS, upper=True)} ), cv.Optional(CONF_GAS_RESISTANCE): sensor.sensor_schema( - UNIT_OHM, ICON_GAS_CYLINDER, 0, DEVICE_CLASS_EMPTY + UNIT_OHM, ICON_GAS_CYLINDER, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_IAQ): sensor.sensor_schema( - UNIT_IAQ, ICON_GAUGE, 0, DEVICE_CLASS_EMPTY + UNIT_IAQ, ICON_GAUGE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_IAQ_ACCURACY): sensor.sensor_schema( - UNIT_EMPTY, ICON_ACCURACY, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_ACCURACY, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CO2_EQUIVALENT): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_TEST_TUBE, 1, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_TEST_TUBE, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BREATH_VOC_EQUIVALENT): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_TEST_TUBE, 1, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_TEST_TUBE, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/bmp085/sensor.py b/esphome/components/bmp085/sensor.py index eca5283d5d..1b48f2e440 100644 --- a/esphome/components/bmp085/sensor.py +++ b/esphome/components/bmp085/sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_TEMPERATURE, DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_HECTOPASCAL, @@ -24,10 +25,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(BMP085Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_EMPTY, 1, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_EMPTY, + 1, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/bmp280/sensor.py b/esphome/components/bmp280/sensor.py index d86767ecd5..48953d0259 100644 --- a/esphome/components/bmp280/sensor.py +++ b/esphome/components/bmp280/sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_TEMPERATURE, DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_HECTOPASCAL, @@ -45,7 +46,11 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(BMP280Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( @@ -54,7 +59,11 @@ CONFIG_SCHEMA = ( } ), cv.Optional(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_EMPTY, 1, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_EMPTY, + 1, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum( diff --git a/esphome/components/ccs811/sensor.py b/esphome/components/ccs811/sensor.py index e86d8f9b83..4c4f8802d4 100644 --- a/esphome/components/ccs811/sensor.py +++ b/esphome/components/ccs811/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, ICON_RADIATOR, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, UNIT_PARTS_PER_BILLION, CONF_TEMPERATURE, @@ -28,10 +29,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(CCS811Component), cv.Required(CONF_ECO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_TVOC): sensor.sensor_schema( - UNIT_PARTS_PER_BILLION, ICON_RADIATOR, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_BILLION, + ICON_RADIATOR, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BASELINE): cv.hex_uint16_t, cv.Optional(CONF_TEMPERATURE): cv.use_id(sensor.Sensor), diff --git a/esphome/components/cse7766/sensor.py b/esphome/components/cse7766/sensor.py index ca829cb274..98cf4da96d 100644 --- a/esphome/components/cse7766/sensor.py +++ b/esphome/components/cse7766/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -27,13 +28,17 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(CSE7766Component), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, + ICON_EMPTY, + 2, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), } ) diff --git a/esphome/components/ct_clamp/sensor.py b/esphome/components/ct_clamp/sensor.py index 8b15c9309d..e44d46e7f4 100644 --- a/esphome/components/ct_clamp/sensor.py +++ b/esphome/components/ct_clamp/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_CURRENT, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_AMPERE, ) @@ -18,7 +19,9 @@ ct_clamp_ns = cg.esphome_ns.namespace("ct_clamp") CTClampSensor = ct_clamp_ns.class_("CTClampSensor", sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT) + sensor.sensor_schema( + UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(CTClampSensor), diff --git a/esphome/components/dallas/sensor.py b/esphome/components/dallas/sensor.py index 21ba7b9154..1c8db8fa2f 100644 --- a/esphome/components/dallas/sensor.py +++ b/esphome/components/dallas/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_RESOLUTION, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, CONF_ID, ) @@ -16,7 +17,9 @@ from . import DallasComponent, dallas_ns DallasTemperatureSensor = dallas_ns.class_("DallasTemperatureSensor", sensor.Sensor) CONFIG_SCHEMA = cv.All( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE).extend( + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ).extend( { cv.GenerateID(): cv.declare_id(DallasTemperatureSensor), cv.GenerateID(CONF_DALLAS_ID): cv.use_id(DallasComponent), diff --git a/esphome/components/dht/sensor.py b/esphome/components/dht/sensor.py index 57971e8202..c33ddd2286 100644 --- a/esphome/components/dht/sensor.py +++ b/esphome/components/dht/sensor.py @@ -9,6 +9,7 @@ from esphome.const import ( CONF_PIN, CONF_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, DEVICE_CLASS_TEMPERATURE, @@ -35,10 +36,14 @@ CONFIG_SCHEMA = cv.Schema( cv.GenerateID(): cv.declare_id(DHT), cv.Required(CONF_PIN): pins.gpio_input_pin_schema, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_MODEL, default="auto detect"): cv.enum( DHT_MODELS, upper=True, space="_" diff --git a/esphome/components/dht12/sensor.py b/esphome/components/dht12/sensor.py index 68ff6ae1bb..14c01f5d34 100644 --- a/esphome/components/dht12/sensor.py +++ b/esphome/components/dht12/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_HUMIDITY, CONF_ID, CONF_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_PERCENT, @@ -22,10 +23,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(DHT12Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/duty_cycle/sensor.py b/esphome/components/duty_cycle/sensor.py index e3d2318b51..39f6ebc88f 100644 --- a/esphome/components/duty_cycle/sensor.py +++ b/esphome/components/duty_cycle/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_ID, CONF_PIN, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_PERCENT, ICON_PERCENT, ) @@ -16,7 +17,9 @@ DutyCycleSensor = duty_cycle_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_PERCENT, ICON_PERCENT, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_PERCENT, ICON_PERCENT, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(DutyCycleSensor), diff --git a/esphome/components/esp32_hall/sensor.py b/esphome/components/esp32_hall/sensor.py index 0f1cbb373c..b800b3436a 100644 --- a/esphome/components/esp32_hall/sensor.py +++ b/esphome/components/esp32_hall/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, ESP_PLATFORM_ESP32, + STATE_CLASS_MEASUREMENT, UNIT_MICROTESLA, ICON_MAGNET, ) @@ -17,7 +18,9 @@ ESP32HallSensor = esp32_hall_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_MICROTESLA, ICON_MAGNET, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_MICROTESLA, ICON_MAGNET, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(ESP32HallSensor), diff --git a/esphome/components/fingerprint_grow/sensor.py b/esphome/components/fingerprint_grow/sensor.py index 11f61a6a71..f8a44eb0da 100644 --- a/esphome/components/fingerprint_grow/sensor.py +++ b/esphome/components/fingerprint_grow/sensor.py @@ -15,6 +15,7 @@ from esphome.const import ( ICON_EMPTY, ICON_FINGERPRINT, ICON_SECURITY, + STATE_CLASS_NONE, UNIT_EMPTY, ) from . import CONF_FINGERPRINT_GROW_ID, FingerprintGrowComponent @@ -25,22 +26,22 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(CONF_FINGERPRINT_GROW_ID): cv.use_id(FingerprintGrowComponent), cv.Optional(CONF_FINGERPRINT_COUNT): sensor.sensor_schema( - UNIT_EMPTY, ICON_FINGERPRINT, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_FINGERPRINT, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_STATUS): sensor.sensor_schema( - UNIT_EMPTY, ICON_EMPTY, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_EMPTY, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_CAPACITY): sensor.sensor_schema( - UNIT_EMPTY, ICON_DATABASE, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_DATABASE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_SECURITY_LEVEL): sensor.sensor_schema( - UNIT_EMPTY, ICON_SECURITY, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_SECURITY, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_LAST_FINGER_ID): sensor.sensor_schema( - UNIT_EMPTY, ICON_ACCOUNT, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_ACCOUNT, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_LAST_CONFIDENCE): sensor.sensor_schema( - UNIT_EMPTY, ICON_ACCOUNT_CHECK, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_ACCOUNT_CHECK, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), } ) diff --git a/esphome/components/gps/__init__.py b/esphome/components/gps/__init__.py index 57ae11ea4f..de3eae1115 100644 --- a/esphome/components/gps/__init__.py +++ b/esphome/components/gps/__init__.py @@ -10,6 +10,8 @@ from esphome.const import ( CONF_COURSE, CONF_ALTITUDE, CONF_SATELLITES, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_DEGREES, UNIT_KILOMETER_PER_HOUR, UNIT_METER, @@ -34,22 +36,26 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(GPS), cv.Optional(CONF_LATITUDE): sensor.sensor_schema( - UNIT_DEGREES, ICON_EMPTY, 6, DEVICE_CLASS_EMPTY + UNIT_DEGREES, ICON_EMPTY, 6, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_LONGITUDE): sensor.sensor_schema( - UNIT_DEGREES, ICON_EMPTY, 6, DEVICE_CLASS_EMPTY + UNIT_DEGREES, ICON_EMPTY, 6, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_SPEED): sensor.sensor_schema( - UNIT_KILOMETER_PER_HOUR, ICON_EMPTY, 6, DEVICE_CLASS_EMPTY + UNIT_KILOMETER_PER_HOUR, + ICON_EMPTY, + 6, + DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, ), cv.Optional(CONF_COURSE): sensor.sensor_schema( - UNIT_DEGREES, ICON_EMPTY, 2, DEVICE_CLASS_EMPTY + UNIT_DEGREES, ICON_EMPTY, 2, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_ALTITUDE): sensor.sensor_schema( - UNIT_METER, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY + UNIT_METER, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_SATELLITES): sensor.sensor_schema( - UNIT_EMPTY, ICON_EMPTY, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_EMPTY, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ), } ) diff --git a/esphome/components/hdc1080/sensor.py b/esphome/components/hdc1080/sensor.py index 0995f98589..26ec3ad0a9 100644 --- a/esphome/components/hdc1080/sensor.py +++ b/esphome/components/hdc1080/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ) @@ -24,10 +25,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(HDC1080Component), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/hlw8012/sensor.py b/esphome/components/hlw8012/sensor.py index aeab94a05c..6454a9fcc9 100644 --- a/esphome/components/hlw8012/sensor.py +++ b/esphome/components/hlw8012/sensor.py @@ -18,6 +18,8 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -47,16 +49,16 @@ CONFIG_SCHEMA = cv.Schema( pins.internal_gpio_input_pullup_pin_schema, pins.validate_has_interrupt ), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 1, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, ICON_EMPTY, 1, DEVICE_CLASS_ENERGY, STATE_CLASS_NONE ), 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/hm3301/sensor.py b/esphome/components/hm3301/sensor.py index 34555fdcd6..48a29ed5f8 100644 --- a/esphome/components/hm3301/sensor.py +++ b/esphome/components/hm3301/sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_PM_10_0, CONF_PM_1_0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, ) @@ -46,21 +47,28 @@ CONFIG_SCHEMA = cv.All( ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_2_5): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_10_0): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_AQI): sensor.sensor_schema( - UNIT_INDEX, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY + UNIT_INDEX, + ICON_CHEMICAL_WEAPON, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ).extend( { cv.Required(CONF_CALCULATION_TYPE): cv.enum( diff --git a/esphome/components/hmc5883l/sensor.py b/esphome/components/hmc5883l/sensor.py index c615f929ee..65469003ed 100644 --- a/esphome/components/hmc5883l/sensor.py +++ b/esphome/components/hmc5883l/sensor.py @@ -8,6 +8,8 @@ from esphome.const import ( CONF_RANGE, DEVICE_CLASS_EMPTY, ICON_MAGNET, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_MICROTESLA, UNIT_DEGREES, ICON_SCREEN_ROTATION, @@ -78,10 +80,10 @@ def validate_enum(enum_values, units=None, int=True): field_strength_schema = sensor.sensor_schema( - UNIT_MICROTESLA, ICON_MAGNET, 1, DEVICE_CLASS_EMPTY + UNIT_MICROTESLA, ICON_MAGNET, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ) heading_schema = sensor.sensor_schema( - UNIT_DEGREES, ICON_SCREEN_ROTATION, 1, DEVICE_CLASS_EMPTY + UNIT_DEGREES, ICON_SCREEN_ROTATION, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ) CONFIG_SCHEMA = ( diff --git a/esphome/components/homeassistant/sensor/__init__.py b/esphome/components/homeassistant/sensor/__init__.py index 771db25b85..0dadb78b73 100644 --- a/esphome/components/homeassistant/sensor/__init__.py +++ b/esphome/components/homeassistant/sensor/__init__.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_ENTITY_ID, CONF_ID, ICON_EMPTY, + STATE_CLASS_NONE, UNIT_EMPTY, DEVICE_CLASS_EMPTY, ) @@ -18,7 +19,7 @@ HomeassistantSensor = homeassistant_ns.class_( ) CONFIG_SCHEMA = sensor.sensor_schema( - UNIT_EMPTY, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ).extend( { cv.GenerateID(): cv.declare_id(HomeassistantSensor), diff --git a/esphome/components/htu21d/sensor.py b/esphome/components/htu21d/sensor.py index 596a7d4531..435c5bf1bb 100644 --- a/esphome/components/htu21d/sensor.py +++ b/esphome/components/htu21d/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ) @@ -24,10 +25,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(HTU21DComponent), cv.Required(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/hx711/sensor.py b/esphome/components/hx711/sensor.py index b249ca8f77..17a4e35d5f 100644 --- a/esphome/components/hx711/sensor.py +++ b/esphome/components/hx711/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, ICON_SCALE, + STATE_CLASS_MEASUREMENT, UNIT_EMPTY, ) @@ -24,7 +25,9 @@ GAINS = { } CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_EMPTY, ICON_SCALE, 0, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_EMPTY, ICON_SCALE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(HX711Sensor), diff --git a/esphome/components/ina219/sensor.py b/esphome/components/ina219/sensor.py index 29241c062e..ed88ace967 100644 --- a/esphome/components/ina219/sensor.py +++ b/esphome/components/ina219/sensor.py @@ -14,6 +14,7 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -31,16 +32,20 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(INA219Component), cv.Optional(CONF_BUS_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_SHUNT_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 3, DEVICE_CLASS_CURRENT + UNIT_AMPERE, + ICON_EMPTY, + 3, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_SHUNT_RESISTANCE, default=0.1): cv.All( cv.resistance, cv.Range(min=0.0, max=32.0) diff --git a/esphome/components/ina226/sensor.py b/esphome/components/ina226/sensor.py index d6d385f5cb..e4ceda39c1 100644 --- a/esphome/components/ina226/sensor.py +++ b/esphome/components/ina226/sensor.py @@ -13,6 +13,7 @@ from esphome.const import ( DEVICE_CLASS_CURRENT, DEVICE_CLASS_POWER, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -30,16 +31,20 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(INA226Component), cv.Optional(CONF_BUS_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_SHUNT_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 3, DEVICE_CLASS_CURRENT + UNIT_AMPERE, + ICON_EMPTY, + 3, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_SHUNT_RESISTANCE, default=0.1): cv.All( cv.resistance, cv.Range(min=0.0) diff --git a/esphome/components/ina3221/sensor.py b/esphome/components/ina3221/sensor.py index b97022ca27..8b861d972d 100644 --- a/esphome/components/ina3221/sensor.py +++ b/esphome/components/ina3221/sensor.py @@ -12,6 +12,7 @@ from esphome.const import ( DEVICE_CLASS_CURRENT, DEVICE_CLASS_POWER, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -31,16 +32,16 @@ INA3221Component = ina3221_ns.class_( INA3221_CHANNEL_SCHEMA = cv.Schema( { cv.Optional(CONF_BUS_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_SHUNT_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_SHUNT_RESISTANCE, default=0.1): cv.All( cv.resistance, cv.Range(min=0.0, max=32.0) diff --git a/esphome/components/inkbird_ibsth1_mini/sensor.py b/esphome/components/inkbird_ibsth1_mini/sensor.py index cc12be3bf4..044e7fe67d 100644 --- a/esphome/components/inkbird_ibsth1_mini/sensor.py +++ b/esphome/components/inkbird_ibsth1_mini/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, CONF_ID, @@ -29,13 +30,25 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(InkbirdUBSTH1_MINI), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/max31855/sensor.py b/esphome/components/max31855/sensor.py index 55de8d681e..a8b5d25c61 100644 --- a/esphome/components/max31855/sensor.py +++ b/esphome/components/max31855/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_REFERENCE_TEMPERATURE, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ) @@ -20,7 +21,11 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(MAX31855Sensor), cv.Optional(CONF_REFERENCE_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 2, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 2, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/max31856/sensor.py b/esphome/components/max31856/sensor.py index 5c7b7f1f0e..9583c0bcf9 100644 --- a/esphome/components/max31856/sensor.py +++ b/esphome/components/max31856/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_MAINS_FILTER, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ) @@ -21,7 +22,13 @@ FILTER = { } CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, + ) .extend( { cv.GenerateID(): cv.declare_id(MAX31856Sensor), diff --git a/esphome/components/max31865/sensor.py b/esphome/components/max31865/sensor.py index addedc8296..64495ebd7a 100644 --- a/esphome/components/max31865/sensor.py +++ b/esphome/components/max31865/sensor.py @@ -9,6 +9,7 @@ from esphome.const import ( CONF_RTD_WIRES, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ) @@ -24,7 +25,9 @@ FILTER = { } CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 2, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 2, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(MAX31865Sensor), diff --git a/esphome/components/max6675/sensor.py b/esphome/components/max6675/sensor.py index 6697f2ae90..ad0e89c028 100644 --- a/esphome/components/max6675/sensor.py +++ b/esphome/components/max6675/sensor.py @@ -1,7 +1,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor, spi -from esphome.const import CONF_ID, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, UNIT_CELSIUS +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_TEMPERATURE, + ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, +) max6675_ns = cg.esphome_ns.namespace("max6675") MAX6675Sensor = max6675_ns.class_( @@ -9,7 +15,9 @@ MAX6675Sensor = max6675_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(MAX6675Sensor), diff --git a/esphome/components/mcp9808/sensor.py b/esphome/components/mcp9808/sensor.py index f11d5cd755..d417f45955 100644 --- a/esphome/components/mcp9808/sensor.py +++ b/esphome/components/mcp9808/sensor.py @@ -1,7 +1,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, UNIT_CELSIUS +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_TEMPERATURE, + ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, +) CODEOWNERS = ["@k7hpn"] DEPENDENCIES = ["i2c"] @@ -12,7 +18,9 @@ MCP9808Sensor = mcp9808_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(MCP9808Sensor), diff --git a/esphome/components/mhz19/sensor.py b/esphome/components/mhz19/sensor.py index 54a0e3a366..ebcecb84e2 100644 --- a/esphome/components/mhz19/sensor.py +++ b/esphome/components/mhz19/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_EMPTY, DEVICE_CLASS_TEMPERATURE, ICON_MOLECULE_CO2, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, UNIT_CELSIUS, ICON_EMPTY, @@ -32,10 +33,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(MHZ19Component), cv.Required(CONF_CO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 0, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 0, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_AUTOMATIC_BASELINE_CALIBRATION): cv.boolean, } diff --git a/esphome/components/midea_ac/climate.py b/esphome/components/midea_ac/climate.py index e0128febfb..7bf77d7c6b 100644 --- a/esphome/components/midea_ac/climate.py +++ b/esphome/components/midea_ac/climate.py @@ -3,6 +3,7 @@ import esphome.config_validation as cv import esphome.codegen as cg from esphome.const import ( CONF_ID, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, UNIT_WATT, @@ -36,13 +37,21 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_SWING_HORIZONTAL, default=False): cv.boolean, cv.Optional(CONF_SWING_BOTH, default=False): cv.boolean, cv.Optional(CONF_OUTDOOR_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_THERMOMETER, 0, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_THERMOMETER, + 0, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER_USAGE): sensor.sensor_schema( - UNIT_WATT, ICON_POWER, 0, DEVICE_CLASS_POWER + UNIT_WATT, ICON_POWER, 0, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_HUMIDITY_SETPOINT): sensor.sensor_schema( - UNIT_PERCENT, ICON_WATER_PERCENT, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_WATER_PERCENT, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ).extend(cv.COMPONENT_SCHEMA) diff --git a/esphome/components/mpu6050/sensor.py b/esphome/components/mpu6050/sensor.py index 2054897dfb..05c26289b4 100644 --- a/esphome/components/mpu6050/sensor.py +++ b/esphome/components/mpu6050/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, ICON_BRIEFCASE_DOWNLOAD, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_METER_PER_SECOND_SQUARED, ICON_SCREEN_ROTATION, UNIT_DEGREE_PER_SECOND, @@ -29,13 +30,21 @@ MPU6050Component = mpu6050_ns.class_( ) accel_schema = sensor.sensor_schema( - UNIT_METER_PER_SECOND_SQUARED, ICON_BRIEFCASE_DOWNLOAD, 2, DEVICE_CLASS_EMPTY + UNIT_METER_PER_SECOND_SQUARED, + ICON_BRIEFCASE_DOWNLOAD, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ) gyro_schema = sensor.sensor_schema( - UNIT_DEGREE_PER_SECOND, ICON_SCREEN_ROTATION, 2, DEVICE_CLASS_EMPTY + UNIT_DEGREE_PER_SECOND, + ICON_SCREEN_ROTATION, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ) temperature_schema = sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT ) CONFIG_SCHEMA = ( diff --git a/esphome/components/mqtt_subscribe/sensor/__init__.py b/esphome/components/mqtt_subscribe/sensor/__init__.py index 25a2079f81..d640b254de 100644 --- a/esphome/components/mqtt_subscribe/sensor/__init__.py +++ b/esphome/components/mqtt_subscribe/sensor/__init__.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_ID, CONF_QOS, CONF_TOPIC, + STATE_CLASS_NONE, UNIT_EMPTY, ICON_EMPTY, DEVICE_CLASS_EMPTY, @@ -19,7 +20,9 @@ MQTTSubscribeSensor = mqtt_subscribe_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_EMPTY, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE + ) .extend( { cv.GenerateID(): cv.declare_id(MQTTSubscribeSensor), diff --git a/esphome/components/ms5611/sensor.py b/esphome/components/ms5611/sensor.py index ebf1a949e7..34198e04eb 100644 --- a/esphome/components/ms5611/sensor.py +++ b/esphome/components/ms5611/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_GAUGE, UNIT_HECTOPASCAL, @@ -25,10 +26,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(MS5611Component), cv.Required(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_GAUGE, 1, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_GAUGE, + 1, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/ntc/sensor.py b/esphome/components/ntc/sensor.py index d83afae350..e7b8c03586 100644 --- a/esphome/components/ntc/sensor.py +++ b/esphome/components/ntc/sensor.py @@ -13,6 +13,7 @@ from esphome.const import ( CONF_VALUE, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ) @@ -118,7 +119,9 @@ def process_calibration(value): CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(NTC), diff --git a/esphome/components/pid/sensor/__init__.py b/esphome/components/pid/sensor/__init__.py index d958f239de..61669d4716 100644 --- a/esphome/components/pid/sensor/__init__.py +++ b/esphome/components/pid/sensor/__init__.py @@ -4,6 +4,7 @@ from esphome.components import sensor from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_PERCENT, ICON_GAUGE, CONF_TYPE, @@ -28,7 +29,9 @@ PID_CLIMATE_SENSOR_TYPES = { CONF_CLIMATE_ID = "climate_id" CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_PERCENT, ICON_GAUGE, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_PERCENT, ICON_GAUGE, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(PIDClimateSensor), diff --git a/esphome/components/pmsx003/sensor.py b/esphome/components/pmsx003/sensor.py index bdbe2b1811..80f2b80e5e 100644 --- a/esphome/components/pmsx003/sensor.py +++ b/esphome/components/pmsx003/sensor.py @@ -15,6 +15,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, ICON_CHEMICAL_WEAPON, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_MICROGRAMS_PER_CUBIC_METER, UNIT_CELSIUS, UNIT_PERCENT, @@ -66,30 +67,42 @@ CONFIG_SCHEMA = ( ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_2_5): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_10_0): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_FORMALDEHYDE): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/pulse_counter/sensor.py b/esphome/components/pulse_counter/sensor.py index d6e4dfc1a3..71227ec491 100644 --- a/esphome/components/pulse_counter/sensor.py +++ b/esphome/components/pulse_counter/sensor.py @@ -13,6 +13,8 @@ from esphome.const import ( CONF_TOTAL, DEVICE_CLASS_EMPTY, ICON_PULSE, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_PULSES_PER_MINUTE, UNIT_PULSES, ) @@ -64,7 +66,13 @@ def validate_count_mode(value): CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_PULSES_PER_MINUTE, ICON_PULSE, 2, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_PULSES_PER_MINUTE, + ICON_PULSE, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + ) .extend( { cv.GenerateID(): cv.declare_id(PulseCounterSensor), @@ -86,7 +94,7 @@ CONFIG_SCHEMA = ( ), cv.Optional(CONF_INTERNAL_FILTER, default="13us"): validate_internal_filter, cv.Optional(CONF_TOTAL): sensor.sensor_schema( - UNIT_PULSES, ICON_PULSE, 0, DEVICE_CLASS_EMPTY + UNIT_PULSES, ICON_PULSE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), } ) diff --git a/esphome/components/pulse_meter/sensor.py b/esphome/components/pulse_meter/sensor.py index 5811863405..e732971c3a 100644 --- a/esphome/components/pulse_meter/sensor.py +++ b/esphome/components/pulse_meter/sensor.py @@ -11,6 +11,8 @@ from esphome.const import ( CONF_TOTAL, CONF_VALUE, ICON_PULSE, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_PULSES, UNIT_PULSES_PER_MINUTE, DEVICE_CLASS_EMPTY, @@ -49,7 +51,7 @@ def validate_pulse_meter_pin(value): CONFIG_SCHEMA = sensor.sensor_schema( - UNIT_PULSES_PER_MINUTE, ICON_PULSE, 2, DEVICE_CLASS_EMPTY + UNIT_PULSES_PER_MINUTE, ICON_PULSE, 2, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ).extend( { cv.GenerateID(): cv.declare_id(PulseMeterSensor), @@ -57,7 +59,7 @@ CONFIG_SCHEMA = sensor.sensor_schema( cv.Optional(CONF_INTERNAL_FILTER, default="13us"): validate_internal_filter, cv.Optional(CONF_TIMEOUT, default="5min"): validate_timeout, cv.Optional(CONF_TOTAL): sensor.sensor_schema( - UNIT_PULSES, ICON_PULSE, 0, DEVICE_CLASS_EMPTY + UNIT_PULSES, ICON_PULSE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), } ) diff --git a/esphome/components/pulse_width/sensor.py b/esphome/components/pulse_width/sensor.py index 358ffd2962..6a6147c6aa 100644 --- a/esphome/components/pulse_width/sensor.py +++ b/esphome/components/pulse_width/sensor.py @@ -2,7 +2,14 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome import pins from esphome.components import sensor -from esphome.const import CONF_ID, CONF_PIN, DEVICE_CLASS_EMPTY, UNIT_SECOND, ICON_TIMER +from esphome.const import ( + CONF_ID, + CONF_PIN, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_SECOND, + ICON_TIMER, +) pulse_width_ns = cg.esphome_ns.namespace("pulse_width") @@ -11,7 +18,9 @@ PulseWidthSensor = pulse_width_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_SECOND, ICON_TIMER, 3, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_SECOND, ICON_TIMER, 3, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(PulseWidthSensor), diff --git a/esphome/components/pzem004t/sensor.py b/esphome/components/pzem004t/sensor.py index b463e58f94..e3859f090c 100644 --- a/esphome/components/pzem004t/sensor.py +++ b/esphome/components/pzem004t/sensor.py @@ -12,6 +12,8 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -28,16 +30,24 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(PZEM004T), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_CURRENT): sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 2, DEVICE_CLASS_CURRENT + UNIT_AMPERE, + ICON_EMPTY, + 2, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 0, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 0, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 0, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, + ICON_EMPTY, + 0, + DEVICE_CLASS_ENERGY, + STATE_CLASS_NONE, ), } ) diff --git a/esphome/components/pzemac/sensor.py b/esphome/components/pzemac/sensor.py index bac2f1b60e..778c5054a0 100644 --- a/esphome/components/pzemac/sensor.py +++ b/esphome/components/pzemac/sensor.py @@ -17,6 +17,8 @@ from esphome.const import ( DEVICE_CLASS_ENERGY, ICON_EMPTY, ICON_CURRENT_AC, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_HERTZ, UNIT_VOLT, UNIT_AMPERE, @@ -35,22 +37,38 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(PZEMAC), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE + 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 + UNIT_AMPERE, + ICON_EMPTY, + 3, + DEVICE_CLASS_CURRENT, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER): sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 0, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, + ICON_EMPTY, + 0, + DEVICE_CLASS_ENERGY, + STATE_CLASS_NONE, ), cv.Optional(CONF_FREQUENCY): sensor.sensor_schema( - UNIT_HERTZ, ICON_CURRENT_AC, 1, DEVICE_CLASS_EMPTY + UNIT_HERTZ, + ICON_CURRENT_AC, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema( - UNIT_EMPTY, ICON_EMPTY, 2, DEVICE_CLASS_POWER_FACTOR + UNIT_EMPTY, + ICON_EMPTY, + 2, + DEVICE_CLASS_POWER_FACTOR, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/pzemdc/sensor.py b/esphome/components/pzemdc/sensor.py index 0bf4c9ba2a..58afea8e30 100644 --- a/esphome/components/pzemdc/sensor.py +++ b/esphome/components/pzemdc/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_POWER, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_VOLT, UNIT_AMPERE, UNIT_WATT, @@ -25,13 +26,17 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(PZEMDC), cv.Optional(CONF_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE + 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 + 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 + UNIT_WATT, ICON_EMPTY, 1, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), } ) diff --git a/esphome/components/qmc5883l/sensor.py b/esphome/components/qmc5883l/sensor.py index 395cb681a3..d0fdf1b77a 100644 --- a/esphome/components/qmc5883l/sensor.py +++ b/esphome/components/qmc5883l/sensor.py @@ -8,6 +8,8 @@ from esphome.const import ( CONF_RANGE, DEVICE_CLASS_EMPTY, ICON_MAGNET, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_MICROTESLA, UNIT_DEGREES, ICON_SCREEN_ROTATION, @@ -69,10 +71,10 @@ def validate_enum(enum_values, units=None, int=True): field_strength_schema = sensor.sensor_schema( - UNIT_MICROTESLA, ICON_MAGNET, 1, DEVICE_CLASS_EMPTY + UNIT_MICROTESLA, ICON_MAGNET, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ) heading_schema = sensor.sensor_schema( - UNIT_DEGREES, ICON_SCREEN_ROTATION, 1, DEVICE_CLASS_EMPTY + UNIT_DEGREES, ICON_SCREEN_ROTATION, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ) CONFIG_SCHEMA = ( diff --git a/esphome/components/resistance/sensor.py b/esphome/components/resistance/sensor.py index 8ee00b25be..ca1501195a 100644 --- a/esphome/components/resistance/sensor.py +++ b/esphome/components/resistance/sensor.py @@ -1,7 +1,14 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor -from esphome.const import CONF_SENSOR, DEVICE_CLASS_EMPTY, UNIT_OHM, ICON_FLASH, CONF_ID +from esphome.const import ( + CONF_SENSOR, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_OHM, + ICON_FLASH, + CONF_ID, +) resistance_ns = cg.esphome_ns.namespace("resistance") ResistanceSensor = resistance_ns.class_("ResistanceSensor", cg.Component, sensor.Sensor) @@ -17,7 +24,9 @@ CONFIGURATIONS = { } CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_OHM, ICON_FLASH, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_OHM, ICON_FLASH, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(ResistanceSensor), diff --git a/esphome/components/rotary_encoder/sensor.py b/esphome/components/rotary_encoder/sensor.py index d4ced4b91f..079f00d284 100644 --- a/esphome/components/rotary_encoder/sensor.py +++ b/esphome/components/rotary_encoder/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_MIN_VALUE, CONF_MAX_VALUE, DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, UNIT_STEPS, ICON_ROTATE_RIGHT, CONF_VALUE, @@ -56,7 +57,9 @@ def validate_min_max_value(config): CONFIG_SCHEMA = cv.All( - sensor.sensor_schema(UNIT_STEPS, ICON_ROTATE_RIGHT, 0, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_STEPS, ICON_ROTATE_RIGHT, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE + ) .extend( { cv.GenerateID(): cv.declare_id(RotaryEncoderSensor), diff --git a/esphome/components/ruuvitag/sensor.py b/esphome/components/ruuvitag/sensor.py index d59dc094fb..12b8425d14 100644 --- a/esphome/components/ruuvitag/sensor.py +++ b/esphome/components/ruuvitag/sensor.py @@ -21,6 +21,8 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_VOLTAGE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_CELSIUS, UNIT_PERCENT, UNIT_VOLT, @@ -50,37 +52,69 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(RuuviTag), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 2, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 2, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 2, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 2, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PRESSURE): sensor.sensor_schema( - UNIT_HECTOPASCAL, ICON_EMPTY, 2, DEVICE_CLASS_PRESSURE + UNIT_HECTOPASCAL, + ICON_EMPTY, + 2, + DEVICE_CLASS_PRESSURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ACCELERATION): sensor.sensor_schema( - UNIT_G, ICON_ACCELERATION, 3, DEVICE_CLASS_EMPTY + UNIT_G, + ICON_ACCELERATION, + 3, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ACCELERATION_X): sensor.sensor_schema( - UNIT_G, ICON_ACCELERATION_X, 3, DEVICE_CLASS_EMPTY + UNIT_G, + ICON_ACCELERATION_X, + 3, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ACCELERATION_Y): sensor.sensor_schema( - UNIT_G, ICON_ACCELERATION_Y, 3, DEVICE_CLASS_EMPTY + UNIT_G, + ICON_ACCELERATION_Y, + 3, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ACCELERATION_Z): sensor.sensor_schema( - UNIT_G, ICON_ACCELERATION_Z, 3, DEVICE_CLASS_EMPTY + UNIT_G, + ICON_ACCELERATION_Z, + 3, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_VOLTAGE): sensor.sensor_schema( - UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE + UNIT_VOLT, ICON_EMPTY, 3, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_TX_POWER): sensor.sensor_schema( - UNIT_DECIBEL_MILLIWATT, ICON_EMPTY, 0, DEVICE_CLASS_SIGNAL_STRENGTH + UNIT_DECIBEL_MILLIWATT, + ICON_EMPTY, + 0, + DEVICE_CLASS_SIGNAL_STRENGTH, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_MOVEMENT_COUNTER): sensor.sensor_schema( - UNIT_EMPTY, ICON_GAUGE, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_GAUGE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_MEASUREMENT_SEQUENCE_NUMBER): sensor.sensor_schema( - UNIT_EMPTY, ICON_GAUGE, 0, DEVICE_CLASS_EMPTY + UNIT_EMPTY, ICON_GAUGE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), } ) diff --git a/esphome/components/scd30/sensor.py b/esphome/components/scd30/sensor.py index aad2a1811c..7a08289474 100644 --- a/esphome/components/scd30/sensor.py +++ b/esphome/components/scd30/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, UNIT_CELSIUS, @@ -32,13 +33,25 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(SCD30Component), cv.Optional(CONF_CO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_AUTOMATIC_SELF_CALIBRATION, default=True): cv.boolean, cv.Optional(CONF_ALTITUDE_COMPENSATION): cv.All( diff --git a/esphome/components/sdm_meter/sensor.py b/esphome/components/sdm_meter/sensor.py index 0f7a4c928b..39ef280fef 100644 --- a/esphome/components/sdm_meter/sensor.py +++ b/esphome/components/sdm_meter/sensor.py @@ -17,6 +17,7 @@ from esphome.const import ( CONF_REACTIVE_POWER, CONF_VOLTAGE, DEVICE_CLASS_CURRENT, + DEVICE_CLASS_EMPTY, DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, DEVICE_CLASS_POWER_FACTOR, @@ -24,6 +25,8 @@ from esphome.const import ( ICON_CURRENT_AC, ICON_EMPTY, ICON_FLASH, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_AMPERE, UNIT_DEGREES, UNIT_EMPTY, @@ -45,19 +48,23 @@ SDMMeter = sdm_meter_ns.class_("SDMMeter", cg.PollingComponent, modbus.ModbusDev PHASE_SENSORS = { CONF_VOLTAGE: sensor.sensor_schema(UNIT_VOLT, ICON_EMPTY, 2, DEVICE_CLASS_VOLTAGE), CONF_CURRENT: sensor.sensor_schema( - UNIT_AMPERE, ICON_EMPTY, 3, DEVICE_CLASS_CURRENT + UNIT_AMPERE, ICON_EMPTY, 3, DEVICE_CLASS_CURRENT, STATE_CLASS_MEASUREMENT ), CONF_ACTIVE_POWER: sensor.sensor_schema( - UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_WATT, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), CONF_APPARENT_POWER: sensor.sensor_schema( - UNIT_VOLT_AMPS, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_VOLT_AMPS, ICON_EMPTY, 2, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT ), CONF_REACTIVE_POWER: sensor.sensor_schema( - UNIT_VOLT_AMPS_REACTIVE, ICON_EMPTY, 2, DEVICE_CLASS_POWER + UNIT_VOLT_AMPS_REACTIVE, + ICON_EMPTY, + 2, + DEVICE_CLASS_POWER, + STATE_CLASS_MEASUREMENT, ), CONF_POWER_FACTOR: sensor.sensor_schema( - UNIT_EMPTY, ICON_EMPTY, 3, DEVICE_CLASS_POWER_FACTOR + UNIT_EMPTY, ICON_EMPTY, 3, DEVICE_CLASS_POWER_FACTOR, STATE_CLASS_MEASUREMENT ), CONF_PHASE_ANGLE: sensor.sensor_schema(UNIT_DEGREES, ICON_FLASH, 3), } @@ -74,19 +81,31 @@ CONFIG_SCHEMA = ( cv.Optional(CONF_PHASE_B): PHASE_SCHEMA, cv.Optional(CONF_PHASE_C): PHASE_SCHEMA, cv.Optional(CONF_FREQUENCY): sensor.sensor_schema( - UNIT_HERTZ, ICON_CURRENT_AC, 3 + UNIT_HERTZ, + ICON_CURRENT_AC, + 3, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_IMPORT_ACTIVE_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY, STATE_CLASS_NONE ), cv.Optional(CONF_EXPORT_ACTIVE_ENERGY): sensor.sensor_schema( - UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + UNIT_WATT_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY, STATE_CLASS_NONE ), cv.Optional(CONF_IMPORT_REACTIVE_ENERGY): sensor.sensor_schema( - UNIT_VOLT_AMPS_REACTIVE_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + UNIT_VOLT_AMPS_REACTIVE_HOURS, + ICON_EMPTY, + 2, + DEVICE_CLASS_ENERGY, + STATE_CLASS_NONE, ), cv.Optional(CONF_EXPORT_REACTIVE_ENERGY): sensor.sensor_schema( - UNIT_VOLT_AMPS_REACTIVE_HOURS, ICON_EMPTY, 2, DEVICE_CLASS_ENERGY + UNIT_VOLT_AMPS_REACTIVE_HOURS, + ICON_EMPTY, + 2, + DEVICE_CLASS_ENERGY, + STATE_CLASS_NONE, ), } ) diff --git a/esphome/components/sds011/sensor.py b/esphome/components/sds011/sensor.py index 5655533b21..af482839a9 100644 --- a/esphome/components/sds011/sensor.py +++ b/esphome/components/sds011/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_RX_ONLY, CONF_UPDATE_INTERVAL, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, ) @@ -42,12 +43,14 @@ CONFIG_SCHEMA = cv.All( ICON_CHEMICAL_WEAPON, 1, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_10_0): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 1, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_RX_ONLY, default=False): cv.boolean, cv.Optional(CONF_UPDATE_INTERVAL): cv.positive_time_period_minutes, diff --git a/esphome/components/senseair/sensor.py b/esphome/components/senseair/sensor.py index f37a2dd5ff..2d40e12a09 100644 --- a/esphome/components/senseair/sensor.py +++ b/esphome/components/senseair/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, ICON_MOLECULE_CO2, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, ) @@ -38,7 +39,11 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(SenseAirComponent), cv.Required(CONF_CO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 9c0b0bedd1..89bde9476a 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -22,6 +22,7 @@ from esphome.const import ( CONF_ON_VALUE_RANGE, CONF_SEND_EVERY, CONF_SEND_FIRST_AT, + CONF_STATE_CLASS, CONF_TO, CONF_TRIGGER_ID, CONF_UNIT_OF_MEASUREMENT, @@ -46,6 +47,7 @@ from esphome.const import ( DEVICE_CLASS_PRESSURE, DEVICE_CLASS_TIMESTAMP, DEVICE_CLASS_VOLTAGE, + STATE_CLASS_NONE, ) from esphome.core import CORE, coroutine_with_priority from esphome.util import Registry @@ -69,6 +71,14 @@ DEVICE_CLASSES = [ DEVICE_CLASS_VOLTAGE, ] +sensor_ns = cg.esphome_ns.namespace("sensor") +StateClasses = sensor_ns.enum("StateClass") +STATE_CLASSES = { + "": StateClasses.STATE_CLASS_NONE, + "measurement": StateClasses.STATE_CLASS_MEASUREMENT, +} +validate_state_class = cv.enum(STATE_CLASSES, lower=True, space="_") + IS_PLATFORM_COMPONENT = True @@ -157,6 +167,7 @@ SENSOR_SCHEMA = cv.MQTT_COMPONENT_SCHEMA.extend( cv.Optional(CONF_ICON): icon, 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_FORCE_UPDATE, default=False): cv.boolean, cv.Optional(CONF_EXPIRE_AFTER): cv.All( cv.requires_component("mqtt"), @@ -190,6 +201,7 @@ def sensor_schema( icon_: str, accuracy_decimals_: int, device_class_: Optional[str] = DEVICE_CLASS_EMPTY, + state_class_: Optional[str] = STATE_CLASS_NONE, ) -> cv.Schema: schema = SENSOR_SCHEMA if unit_of_measurement_ != UNIT_EMPTY: @@ -214,6 +226,10 @@ def sensor_schema( schema = schema.extend( {cv.Optional(CONF_DEVICE_CLASS, default=device_class_): device_class} ) + if state_class_ != STATE_CLASS_NONE: + schema = schema.extend( + {cv.Optional(CONF_STATE_CLASS, default=state_class_): validate_state_class} + ) return schema @@ -455,6 +471,8 @@ async def setup_sensor_core_(var, config): cg.add(var.set_internal(config[CONF_INTERNAL])) if CONF_DEVICE_CLASS in config: cg.add(var.set_device_class(config[CONF_DEVICE_CLASS])) + if CONF_STATE_CLASS in config: + cg.add(var.set_state_class(config[CONF_STATE_CLASS])) if CONF_UNIT_OF_MEASUREMENT in config: cg.add(var.set_unit_of_measurement(config[CONF_UNIT_OF_MEASUREMENT])) if CONF_ICON in config: diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index 069a5c5923..d7be618c2a 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -6,6 +6,16 @@ namespace sensor { static const char *TAG = "sensor"; +const char *state_class_to_string(StateClass state_class) { + switch (state_class) { + case STATE_CLASS_MEASUREMENT: + return "measurement"; + case STATE_CLASS_NONE: + default: + return ""; + } +} + void Sensor::publish_state(float state) { this->raw_state = state; this->raw_callback_.call(state); @@ -47,6 +57,14 @@ std::string Sensor::get_device_class() { return this->device_class(); } std::string Sensor::device_class() { return ""; } +void Sensor::set_state_class(StateClass state_class) { this->state_class = state_class; } +void Sensor::set_state_class(const std::string &state_class) { + if (str_equals_case_insensitive(state_class, "measurement")) { + this->state_class = STATE_CLASS_MEASUREMENT; + } else { + ESP_LOGW(TAG, "'%s' - Unrecognized state class %s", this->get_name().c_str(), state_class.c_str()); + } +} 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 6bb6c876ab..fe528603c4 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -13,6 +13,7 @@ namespace sensor { if (!obj->get_device_class().empty()) { \ 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)); \ 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()) { \ @@ -26,6 +27,16 @@ namespace sensor { } \ } +/** + * Sensor state classes + */ +enum StateClass : uint8_t { + STATE_CLASS_NONE = 0, + STATE_CLASS_MEASUREMENT = 1, +}; + +const char *state_class_to_string(StateClass state_class); + /** 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. @@ -139,6 +150,13 @@ class Sensor : public Nameable { /// Return whether this sensor has gotten a full state (that passed through all filters) yet. bool has_state() const; + // The state class of this sensor state + StateClass state_class{STATE_CLASS_NONE}; + + /// Manually set the Home Assistant state class (see sensor::state_class) + void set_state_class(StateClass state_class); + void set_state_class(const std::string &state_class); + /** Override this to set the Home Assistant device class for this sensor. * * Return "" to disable this feature. diff --git a/esphome/components/sgp30/sensor.py b/esphome/components/sgp30/sensor.py index 7046605f3b..f393627eda 100644 --- a/esphome/components/sgp30/sensor.py +++ b/esphome/components/sgp30/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, ICON_RADIATOR, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, UNIT_PARTS_PER_BILLION, ICON_MOLECULE_CO2, @@ -30,10 +31,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(SGP30Component), cv.Required(CONF_ECO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_TVOC): sensor.sensor_schema( - UNIT_PARTS_PER_BILLION, ICON_RADIATOR, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_BILLION, + ICON_RADIATOR, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BASELINE): cv.Schema( { diff --git a/esphome/components/sgp40/sensor.py b/esphome/components/sgp40/sensor.py index a308d7bb79..36e039d2b5 100644 --- a/esphome/components/sgp40/sensor.py +++ b/esphome/components/sgp40/sensor.py @@ -1,7 +1,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, DEVICE_CLASS_EMPTY, ICON_RADIATOR, UNIT_EMPTY +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_EMPTY, + ICON_RADIATOR, + STATE_CLASS_MEASUREMENT, + UNIT_EMPTY, +) DEPENDENCIES = ["i2c"] @@ -19,7 +25,9 @@ CONF_STORE_BASELINE = "store_baseline" CONF_VOC_BASELINE = "voc_baseline" CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_EMPTY, ICON_RADIATOR, 0, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_EMPTY, ICON_RADIATOR, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(SGP40Component), diff --git a/esphome/components/sht3xd/sensor.py b/esphome/components/sht3xd/sensor.py index cac53e47e3..2a4bb6594e 100644 --- a/esphome/components/sht3xd/sensor.py +++ b/esphome/components/sht3xd/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ) @@ -24,10 +25,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(SHT3XDComponent), cv.Required(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/sht4x/sensor.py b/esphome/components/sht4x/sensor.py index fdc4029e41..a746ecde07 100644 --- a/esphome/components/sht4x/sensor.py +++ b/esphome/components/sht4x/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_ID, CONF_TEMPERATURE, CONF_HUMIDITY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ICON_THERMOMETER, @@ -50,10 +51,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(SHT4XComponent), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_THERMOMETER, 2, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_THERMOMETER, + 2, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_WATER_PERCENT, 2, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_WATER_PERCENT, + 2, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PRECISION, default="High"): cv.enum(PRECISION_OPTIONS), cv.Optional(CONF_HEATER_POWER, default="High"): cv.enum( diff --git a/esphome/components/shtcx/sensor.py b/esphome/components/shtcx/sensor.py index 7156029488..af9379218c 100644 --- a/esphome/components/shtcx/sensor.py +++ b/esphome/components/shtcx/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ) @@ -24,10 +25,18 @@ CONFIG_SCHEMA = ( { cv.GenerateID(): cv.declare_id(SHTCXComponent), cv.Required(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Required(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/sm300d2/sensor.py b/esphome/components/sm300d2/sensor.py index 7618136bd9..3d522b3bd5 100644 --- a/esphome/components/sm300d2/sensor.py +++ b/esphome/components/sm300d2/sensor.py @@ -13,6 +13,7 @@ from esphome.const import ( DEVICE_CLASS_EMPTY, DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, UNIT_MICROGRAMS_PER_CUBIC_METER, UNIT_CELSIUS, @@ -34,28 +35,53 @@ CONFIG_SCHEMA = cv.All( { cv.GenerateID(): cv.declare_id(SM300D2Sensor), cv.Optional(CONF_CO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_FORMALDEHYDE): sensor.sensor_schema( - UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_FLASK, 0, DEVICE_CLASS_EMPTY + UNIT_MICROGRAMS_PER_CUBIC_METER, + ICON_FLASK, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_TVOC): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 0, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_2_5): sensor.sensor_schema( - UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_GRAIN, 0, DEVICE_CLASS_EMPTY + UNIT_MICROGRAMS_PER_CUBIC_METER, + ICON_GRAIN, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_10_0): sensor.sensor_schema( - UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_GRAIN, 0, DEVICE_CLASS_EMPTY + UNIT_MICROGRAMS_PER_CUBIC_METER, + ICON_GRAIN, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 0, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 0, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/sps30/sensor.py b/esphome/components/sps30/sensor.py index 32c55b969c..219f68c5c8 100644 --- a/esphome/components/sps30/sensor.py +++ b/esphome/components/sps30/sensor.py @@ -14,6 +14,7 @@ from esphome.const import ( CONF_PMC_10_0, CONF_PM_SIZE, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_MICROGRAMS_PER_CUBIC_METER, UNIT_COUNTS_PER_CUBIC_METER, UNIT_MICROMETER, @@ -36,42 +37,70 @@ CONFIG_SCHEMA = ( ICON_CHEMICAL_WEAPON, 2, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_2_5): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 2, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_4_0): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 2, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_10_0): sensor.sensor_schema( UNIT_MICROGRAMS_PER_CUBIC_METER, ICON_CHEMICAL_WEAPON, 2, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PMC_0_5): sensor.sensor_schema( - UNIT_COUNTS_PER_CUBIC_METER, ICON_COUNTER, 2, DEVICE_CLASS_EMPTY + UNIT_COUNTS_PER_CUBIC_METER, + ICON_COUNTER, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PMC_1_0): sensor.sensor_schema( - UNIT_COUNTS_PER_CUBIC_METER, ICON_COUNTER, 2, DEVICE_CLASS_EMPTY + UNIT_COUNTS_PER_CUBIC_METER, + ICON_COUNTER, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PMC_2_5): sensor.sensor_schema( - UNIT_COUNTS_PER_CUBIC_METER, ICON_COUNTER, 2, DEVICE_CLASS_EMPTY + UNIT_COUNTS_PER_CUBIC_METER, + ICON_COUNTER, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PMC_4_0): sensor.sensor_schema( - UNIT_COUNTS_PER_CUBIC_METER, ICON_COUNTER, 2, DEVICE_CLASS_EMPTY + UNIT_COUNTS_PER_CUBIC_METER, + ICON_COUNTER, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PMC_10_0): sensor.sensor_schema( - UNIT_COUNTS_PER_CUBIC_METER, ICON_COUNTER, 2, DEVICE_CLASS_EMPTY + UNIT_COUNTS_PER_CUBIC_METER, + ICON_COUNTER, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_PM_SIZE): sensor.sensor_schema( - UNIT_MICROMETER, ICON_RULER, 0, DEVICE_CLASS_EMPTY + UNIT_MICROMETER, + ICON_RULER, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/sts3x/sensor.py b/esphome/components/sts3x/sensor.py index 9fb798e7f1..9de077c20a 100644 --- a/esphome/components/sts3x/sensor.py +++ b/esphome/components/sts3x/sensor.py @@ -1,7 +1,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, UNIT_CELSIUS +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_TEMPERATURE, + ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, +) DEPENDENCIES = ["i2c"] @@ -12,7 +18,9 @@ STS3XComponent = sts3x_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(STS3XComponent), diff --git a/esphome/components/sun/sensor/__init__.py b/esphome/components/sun/sensor/__init__.py index 3e72a2a04e..644490ffc6 100644 --- a/esphome/components/sun/sensor/__init__.py +++ b/esphome/components/sun/sensor/__init__.py @@ -3,6 +3,7 @@ import esphome.config_validation as cv from esphome.components import sensor from esphome.const import ( DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, UNIT_DEGREES, ICON_WEATHER_SUNSET, CONF_ID, @@ -20,7 +21,9 @@ TYPES = { } CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_DEGREES, ICON_WEATHER_SUNSET, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_DEGREES, ICON_WEATHER_SUNSET, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE + ) .extend( { cv.GenerateID(): cv.declare_id(SunSensor), diff --git a/esphome/components/tcs34725/sensor.py b/esphome/components/tcs34725/sensor.py index 2b02f7b296..d0fa0c1732 100644 --- a/esphome/components/tcs34725/sensor.py +++ b/esphome/components/tcs34725/sensor.py @@ -11,6 +11,7 @@ from esphome.const import ( DEVICE_CLASS_ILLUMINANCE, ICON_EMPTY, ICON_LIGHTBULB, + STATE_CLASS_MEASUREMENT, UNIT_PERCENT, ICON_THERMOMETER, UNIT_KELVIN, @@ -48,13 +49,13 @@ TCS34725_GAINS = { } color_channel_schema = sensor.sensor_schema( - UNIT_PERCENT, ICON_LIGHTBULB, 1, DEVICE_CLASS_EMPTY + UNIT_PERCENT, ICON_LIGHTBULB, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ) color_temperature_schema = sensor.sensor_schema( - UNIT_KELVIN, ICON_THERMOMETER, 1, DEVICE_CLASS_EMPTY + UNIT_KELVIN, ICON_THERMOMETER, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ) illuminance_schema = sensor.sensor_schema( - UNIT_LUX, ICON_EMPTY, 1, DEVICE_CLASS_ILLUMINANCE + UNIT_LUX, ICON_EMPTY, 1, DEVICE_CLASS_ILLUMINANCE, STATE_CLASS_MEASUREMENT ) CONFIG_SCHEMA = ( diff --git a/esphome/components/template/sensor/__init__.py b/esphome/components/template/sensor/__init__.py index f4d2a1831f..47027583bf 100644 --- a/esphome/components/template/sensor/__init__.py +++ b/esphome/components/template/sensor/__init__.py @@ -7,8 +7,9 @@ from esphome.const import ( CONF_LAMBDA, CONF_STATE, DEVICE_CLASS_EMPTY, - UNIT_EMPTY, ICON_EMPTY, + STATE_CLASS_NONE, + UNIT_EMPTY, ) from .. import template_ns @@ -17,7 +18,13 @@ TemplateSensor = template_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_EMPTY, ICON_EMPTY, 1, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_EMPTY, + ICON_EMPTY, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, + ) .extend( { cv.GenerateID(): cv.declare_id(TemplateSensor), diff --git a/esphome/components/tmp102/sensor.py b/esphome/components/tmp102/sensor.py index 6a0d3cd73e..b54d5646ba 100644 --- a/esphome/components/tmp102/sensor.py +++ b/esphome/components/tmp102/sensor.py @@ -10,7 +10,13 @@ https://www.sparkfun.com/datasheets/Sensors/Temperature/tmp102.pdf import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, UNIT_CELSIUS +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_TEMPERATURE, + ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, +) CODEOWNERS = ["@timsavage"] DEPENDENCIES = ["i2c"] @@ -21,7 +27,9 @@ TMP102Component = tmp102_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(TMP102Component), diff --git a/esphome/components/tmp117/sensor.py b/esphome/components/tmp117/sensor.py index 81a9569ca5..a5fc027b20 100644 --- a/esphome/components/tmp117/sensor.py +++ b/esphome/components/tmp117/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_UPDATE_INTERVAL, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ) @@ -17,7 +18,9 @@ TMP117Component = tmp117_ns.class_( ) CONFIG_SCHEMA = cv.All( - sensor.sensor_schema(UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE) + sensor.sensor_schema( + UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(TMP117Component), diff --git a/esphome/components/tof10120/sensor.py b/esphome/components/tof10120/sensor.py index 5ff8c23cc6..2110cbfcf8 100644 --- a/esphome/components/tof10120/sensor.py +++ b/esphome/components/tof10120/sensor.py @@ -1,7 +1,13 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import i2c, sensor -from esphome.const import CONF_ID, UNIT_METER, ICON_ARROW_EXPAND_VERTICAL +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + UNIT_METER, + ICON_ARROW_EXPAND_VERTICAL, +) CODEOWNERS = ["@wstrzalka"] DEPENDENCIES = ["i2c"] @@ -12,7 +18,13 @@ TOF10120Sensor = tof10120_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, 3) + sensor.sensor_schema( + UNIT_METER, + ICON_ARROW_EXPAND_VERTICAL, + 3, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + ) .extend({cv.GenerateID(): cv.declare_id(TOF10120Sensor)}) .extend(cv.polling_component_schema("60s")) .extend(i2c.i2c_device_schema(0x52)) diff --git a/esphome/components/tsl2561/sensor.py b/esphome/components/tsl2561/sensor.py index a49c776447..c05079f668 100644 --- a/esphome/components/tsl2561/sensor.py +++ b/esphome/components/tsl2561/sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_INTEGRATION_TIME, DEVICE_CLASS_ILLUMINANCE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_LUX, ) @@ -39,7 +40,9 @@ TSL2561Sensor = tsl2561_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_LUX, ICON_EMPTY, 1, DEVICE_CLASS_ILLUMINANCE) + sensor.sensor_schema( + UNIT_LUX, ICON_EMPTY, 1, DEVICE_CLASS_ILLUMINANCE, STATE_CLASS_MEASUREMENT + ) .extend( { cv.GenerateID(): cv.declare_id(TSL2561Sensor), diff --git a/esphome/components/tx20/sensor.py b/esphome/components/tx20/sensor.py index 5b275505fd..57c3165d16 100644 --- a/esphome/components/tx20/sensor.py +++ b/esphome/components/tx20/sensor.py @@ -8,6 +8,8 @@ from esphome.const import ( CONF_PIN, CONF_WIND_DIRECTION_DEGREES, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_KILOMETER_PER_HOUR, ICON_WEATHER_WINDY, ICON_SIGN_DIRECTION, @@ -21,10 +23,14 @@ CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(Tx20Component), cv.Optional(CONF_WIND_SPEED): sensor.sensor_schema( - UNIT_KILOMETER_PER_HOUR, ICON_WEATHER_WINDY, 1, DEVICE_CLASS_EMPTY + UNIT_KILOMETER_PER_HOUR, + ICON_WEATHER_WINDY, + 1, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_WIND_DIRECTION_DEGREES): sensor.sensor_schema( - UNIT_DEGREES, ICON_SIGN_DIRECTION, 1, DEVICE_CLASS_EMPTY + UNIT_DEGREES, ICON_SIGN_DIRECTION, 1, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Required(CONF_PIN): cv.All( pins.internal_gpio_input_pin_schema, pins.validate_has_interrupt diff --git a/esphome/components/ultrasonic/sensor.py b/esphome/components/ultrasonic/sensor.py index 43492a7a2e..77b08b3324 100644 --- a/esphome/components/ultrasonic/sensor.py +++ b/esphome/components/ultrasonic/sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( CONF_TRIGGER_PIN, CONF_TIMEOUT, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, ) @@ -20,7 +21,13 @@ UltrasonicSensorComponent = ultrasonic_ns.class_( ) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, 2, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_METER, + ICON_ARROW_EXPAND_VERTICAL, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + ) .extend( { cv.GenerateID(): cv.declare_id(UltrasonicSensorComponent), diff --git a/esphome/components/uptime/sensor.py b/esphome/components/uptime/sensor.py index 86bd58e739..eaaee5a2d5 100644 --- a/esphome/components/uptime/sensor.py +++ b/esphome/components/uptime/sensor.py @@ -1,13 +1,21 @@ import esphome.codegen as cg import esphome.config_validation as cv from esphome.components import sensor -from esphome.const import CONF_ID, DEVICE_CLASS_EMPTY, UNIT_SECOND, ICON_TIMER +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_EMPTY, + STATE_CLASS_NONE, + UNIT_SECOND, + ICON_TIMER, +) uptime_ns = cg.esphome_ns.namespace("uptime") UptimeSensor = uptime_ns.class_("UptimeSensor", sensor.Sensor, cg.PollingComponent) CONFIG_SCHEMA = ( - sensor.sensor_schema(UNIT_SECOND, ICON_TIMER, 0, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_SECOND, ICON_TIMER, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE + ) .extend( { cv.GenerateID(): cv.declare_id(UptimeSensor), diff --git a/esphome/components/vl53l0x/sensor.py b/esphome/components/vl53l0x/sensor.py index 775532dc9e..8a9667a1bd 100644 --- a/esphome/components/vl53l0x/sensor.py +++ b/esphome/components/vl53l0x/sensor.py @@ -4,6 +4,7 @@ from esphome.components import i2c, sensor from esphome.const import ( CONF_ID, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, CONF_ADDRESS, @@ -40,7 +41,13 @@ def check_timeout(value): CONFIG_SCHEMA = cv.All( - sensor.sensor_schema(UNIT_METER, ICON_ARROW_EXPAND_VERTICAL, 2, DEVICE_CLASS_EMPTY) + sensor.sensor_schema( + UNIT_METER, + ICON_ARROW_EXPAND_VERTICAL, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, + ) .extend( { cv.GenerateID(): cv.declare_id(VL53L0XSensor), diff --git a/esphome/components/wifi_signal/sensor.py b/esphome/components/wifi_signal/sensor.py index be7f32b0d4..f1807966a2 100644 --- a/esphome/components/wifi_signal/sensor.py +++ b/esphome/components/wifi_signal/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_ID, DEVICE_CLASS_SIGNAL_STRENGTH, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_DECIBEL_MILLIWATT, ) @@ -16,7 +17,11 @@ WiFiSignalSensor = wifi_signal_ns.class_( CONFIG_SCHEMA = ( sensor.sensor_schema( - UNIT_DECIBEL_MILLIWATT, ICON_EMPTY, 0, DEVICE_CLASS_SIGNAL_STRENGTH + UNIT_DECIBEL_MILLIWATT, + ICON_EMPTY, + 0, + DEVICE_CLASS_SIGNAL_STRENGTH, + STATE_CLASS_MEASUREMENT, ) .extend( { diff --git a/esphome/components/xiaomi_cgd1/sensor.py b/esphome/components/xiaomi_cgd1/sensor.py index e8a27660fd..e7f18a6be9 100644 --- a/esphome/components/xiaomi_cgd1/sensor.py +++ b/esphome/components/xiaomi_cgd1/sensor.py @@ -11,6 +11,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, CONF_BINDKEY, @@ -31,13 +32,25 @@ CONFIG_SCHEMA = ( cv.Required(CONF_BINDKEY): cv.bind_key, cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_cgdk2/sensor.py b/esphome/components/xiaomi_cgdk2/sensor.py index 50509b7af7..6b2c144911 100644 --- a/esphome/components/xiaomi_cgdk2/sensor.py +++ b/esphome/components/xiaomi_cgdk2/sensor.py @@ -10,6 +10,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, CONF_ID, @@ -31,13 +32,25 @@ CONFIG_SCHEMA = ( cv.Required(CONF_BINDKEY): cv.bind_key, cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_cgg1/sensor.py b/esphome/components/xiaomi_cgg1/sensor.py index 978393d2d4..f26a7ae54e 100644 --- a/esphome/components/xiaomi_cgg1/sensor.py +++ b/esphome/components/xiaomi_cgg1/sensor.py @@ -12,6 +12,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, ) @@ -31,13 +32,25 @@ CONFIG_SCHEMA = ( cv.Optional(CONF_BINDKEY): cv.bind_key, cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_gcls002/sensor.py b/esphome/components/xiaomi_gcls002/sensor.py index 8789378824..a5c702aa9d 100644 --- a/esphome/components/xiaomi_gcls002/sensor.py +++ b/esphome/components/xiaomi_gcls002/sensor.py @@ -9,6 +9,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, ICON_WATER_PERCENT, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, CONF_ID, @@ -34,16 +35,32 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiGCLS002), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_MOISTURE): sensor.sensor_schema( - UNIT_PERCENT, ICON_WATER_PERCENT, 0, DEVICE_CLASS_EMPTY + UNIT_PERCENT, + ICON_WATER_PERCENT, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema( - UNIT_LUX, ICON_EMPTY, 0, DEVICE_CLASS_ILLUMINANCE + UNIT_LUX, + ICON_EMPTY, + 0, + DEVICE_CLASS_ILLUMINANCE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CONDUCTIVITY): sensor.sensor_schema( - UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER, 0, DEVICE_CLASS_EMPTY + UNIT_MICROSIEMENS_PER_CENTIMETER, + ICON_FLOWER, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_hhccjcy01/sensor.py b/esphome/components/xiaomi_hhccjcy01/sensor.py index 13f6e5685c..03289a6219 100644 --- a/esphome/components/xiaomi_hhccjcy01/sensor.py +++ b/esphome/components/xiaomi_hhccjcy01/sensor.py @@ -9,6 +9,7 @@ from esphome.const import ( DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, ICON_WATER_PERCENT, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, CONF_ID, @@ -36,19 +37,39 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiHHCCJCY01), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_MOISTURE): sensor.sensor_schema( - UNIT_PERCENT, ICON_WATER_PERCENT, 0, DEVICE_CLASS_EMPTY + UNIT_PERCENT, + ICON_WATER_PERCENT, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema( - UNIT_LUX, ICON_EMPTY, 0, DEVICE_CLASS_ILLUMINANCE + UNIT_LUX, + ICON_EMPTY, + 0, + DEVICE_CLASS_ILLUMINANCE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CONDUCTIVITY): sensor.sensor_schema( - UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER, 0, DEVICE_CLASS_EMPTY + UNIT_MICROSIEMENS_PER_CENTIMETER, + ICON_FLOWER, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_hhccpot002/sensor.py b/esphome/components/xiaomi_hhccpot002/sensor.py index d2493f77c6..8393de5e5a 100644 --- a/esphome/components/xiaomi_hhccpot002/sensor.py +++ b/esphome/components/xiaomi_hhccpot002/sensor.py @@ -4,6 +4,7 @@ from esphome.components import sensor, esp32_ble_tracker from esphome.const import ( CONF_MAC_ADDRESS, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_PERCENT, ICON_WATER_PERCENT, CONF_ID, @@ -27,10 +28,18 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiHHCCPOT002), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_MOISTURE): sensor.sensor_schema( - UNIT_PERCENT, ICON_WATER_PERCENT, 0, DEVICE_CLASS_EMPTY + UNIT_PERCENT, + ICON_WATER_PERCENT, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_CONDUCTIVITY): sensor.sensor_schema( - UNIT_MICROSIEMENS_PER_CENTIMETER, ICON_FLOWER, 0, DEVICE_CLASS_EMPTY + UNIT_MICROSIEMENS_PER_CENTIMETER, + ICON_FLOWER, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_jqjcy01ym/sensor.py b/esphome/components/xiaomi_jqjcy01ym/sensor.py index e2c2bce40e..70036eb5d9 100644 --- a/esphome/components/xiaomi_jqjcy01ym/sensor.py +++ b/esphome/components/xiaomi_jqjcy01ym/sensor.py @@ -11,6 +11,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, UNIT_PERCENT, CONF_HUMIDITY, @@ -33,19 +34,32 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiJQJCY01YM), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_FORMALDEHYDE): sensor.sensor_schema( UNIT_MILLIGRAMS_PER_CUBIC_METER, ICON_FLASK_OUTLINE, 2, DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_lywsd02/sensor.py b/esphome/components/xiaomi_lywsd02/sensor.py index 5ea633dfa3..ca55f28176 100644 --- a/esphome/components/xiaomi_lywsd02/sensor.py +++ b/esphome/components/xiaomi_lywsd02/sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_MAC_ADDRESS, CONF_TEMPERATURE, DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_PERCENT, @@ -29,13 +30,25 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiLYWSD02), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_lywsd03mmc/sensor.py b/esphome/components/xiaomi_lywsd03mmc/sensor.py index 24bd8d78c6..05b3798955 100644 --- a/esphome/components/xiaomi_lywsd03mmc/sensor.py +++ b/esphome/components/xiaomi_lywsd03mmc/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_PERCENT, @@ -33,13 +34,25 @@ CONFIG_SCHEMA = ( cv.Required(CONF_BINDKEY): cv.bind_key, cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_lywsdcgq/sensor.py b/esphome/components/xiaomi_lywsdcgq/sensor.py index b36a03e455..82bb4c83fb 100644 --- a/esphome/components/xiaomi_lywsdcgq/sensor.py +++ b/esphome/components/xiaomi_lywsdcgq/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_PERCENT, @@ -29,13 +30,25 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiLYWSDCGQ), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 1, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_mhoc401/sensor.py b/esphome/components/xiaomi_mhoc401/sensor.py index 94c594621e..5180bdbb89 100644 --- a/esphome/components/xiaomi_mhoc401/sensor.py +++ b/esphome/components/xiaomi_mhoc401/sensor.py @@ -6,6 +6,7 @@ from esphome.const import ( CONF_HUMIDITY, CONF_MAC_ADDRESS, CONF_TEMPERATURE, + STATE_CLASS_MEASUREMENT, UNIT_CELSIUS, ICON_EMPTY, UNIT_PERCENT, @@ -32,13 +33,25 @@ CONFIG_SCHEMA = ( cv.Required(CONF_BINDKEY): cv.bind_key, cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_HUMIDITY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_miscale/sensor.py b/esphome/components/xiaomi_miscale/sensor.py index 4423dc13cd..9fe76c0645 100644 --- a/esphome/components/xiaomi_miscale/sensor.py +++ b/esphome/components/xiaomi_miscale/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_MAC_ADDRESS, CONF_ID, CONF_WEIGHT, + STATE_CLASS_MEASUREMENT, UNIT_KILOGRAM, ICON_SCALE_BATHROOM, DEVICE_CLASS_EMPTY, @@ -23,7 +24,11 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiMiscale), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_WEIGHT): sensor.sensor_schema( - UNIT_KILOGRAM, ICON_SCALE_BATHROOM, 2, DEVICE_CLASS_EMPTY + UNIT_KILOGRAM, + ICON_SCALE_BATHROOM, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/xiaomi_miscale2/sensor.py b/esphome/components/xiaomi_miscale2/sensor.py index f20ef38c87..9944098407 100644 --- a/esphome/components/xiaomi_miscale2/sensor.py +++ b/esphome/components/xiaomi_miscale2/sensor.py @@ -5,6 +5,7 @@ from esphome.const import ( CONF_MAC_ADDRESS, CONF_ID, CONF_WEIGHT, + STATE_CLASS_MEASUREMENT, UNIT_KILOGRAM, ICON_SCALE_BATHROOM, UNIT_OHM, @@ -26,10 +27,14 @@ CONFIG_SCHEMA = ( cv.GenerateID(): cv.declare_id(XiaomiMiscale2), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_WEIGHT): sensor.sensor_schema( - UNIT_KILOGRAM, ICON_SCALE_BATHROOM, 2, DEVICE_CLASS_EMPTY + UNIT_KILOGRAM, + ICON_SCALE_BATHROOM, + 2, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_IMPEDANCE): sensor.sensor_schema( - UNIT_OHM, ICON_OMEGA, 0, DEVICE_CLASS_EMPTY + UNIT_OHM, ICON_OMEGA, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ), } ) diff --git a/esphome/components/xiaomi_mjyd02yla/binary_sensor.py b/esphome/components/xiaomi_mjyd02yla/binary_sensor.py index a1c26b62a8..90b971c08a 100644 --- a/esphome/components/xiaomi_mjyd02yla/binary_sensor.py +++ b/esphome/components/xiaomi_mjyd02yla/binary_sensor.py @@ -12,6 +12,8 @@ from esphome.const import ( DEVICE_CLASS_EMPTY, DEVICE_CLASS_ILLUMINANCE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_NONE, UNIT_PERCENT, CONF_IDLE_TIME, CONF_ILLUMINANCE, @@ -41,13 +43,21 @@ CONFIG_SCHEMA = cv.All( CONF_DEVICE_CLASS, default="motion" ): binary_sensor.device_class, cv.Optional(CONF_IDLE_TIME): sensor.sensor_schema( - UNIT_MINUTE, ICON_TIMELAPSE, 0, DEVICE_CLASS_EMPTY + UNIT_MINUTE, ICON_TIMELAPSE, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_NONE ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema( - UNIT_LUX, ICON_EMPTY, 0, DEVICE_CLASS_ILLUMINANCE + UNIT_LUX, + ICON_EMPTY, + 0, + DEVICE_CLASS_ILLUMINANCE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_LIGHT): binary_sensor.BINARY_SENSOR_SCHEMA.extend( { diff --git a/esphome/components/xiaomi_wx08zm/binary_sensor.py b/esphome/components/xiaomi_wx08zm/binary_sensor.py index d538d4fbd7..90d4702da4 100644 --- a/esphome/components/xiaomi_wx08zm/binary_sensor.py +++ b/esphome/components/xiaomi_wx08zm/binary_sensor.py @@ -8,6 +8,7 @@ from esphome.const import ( DEVICE_CLASS_BATTERY, DEVICE_CLASS_EMPTY, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_PERCENT, ICON_BUG, CONF_ID, @@ -31,10 +32,14 @@ CONFIG_SCHEMA = cv.All( cv.GenerateID(): cv.declare_id(XiaomiWX08ZM), cv.Required(CONF_MAC_ADDRESS): cv.mac_address, cv.Optional(CONF_TABLET): sensor.sensor_schema( - UNIT_PERCENT, ICON_BUG, 0, DEVICE_CLASS_EMPTY + UNIT_PERCENT, ICON_BUG, 0, DEVICE_CLASS_EMPTY, STATE_CLASS_MEASUREMENT ), cv.Optional(CONF_BATTERY_LEVEL): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 0, DEVICE_CLASS_BATTERY + UNIT_PERCENT, + ICON_EMPTY, + 0, + DEVICE_CLASS_BATTERY, + STATE_CLASS_MEASUREMENT, ), } ) diff --git a/esphome/components/zyaura/sensor.py b/esphome/components/zyaura/sensor.py index ebd43af630..5f9a5e3add 100644 --- a/esphome/components/zyaura/sensor.py +++ b/esphome/components/zyaura/sensor.py @@ -13,6 +13,7 @@ from esphome.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_TEMPERATURE, ICON_EMPTY, + STATE_CLASS_MEASUREMENT, UNIT_PARTS_PER_MILLION, UNIT_CELSIUS, UNIT_PERCENT, @@ -33,13 +34,21 @@ CONFIG_SCHEMA = cv.Schema( pins.internal_gpio_input_pin_schema, pins.validate_has_interrupt ), cv.Optional(CONF_CO2): sensor.sensor_schema( - UNIT_PARTS_PER_MILLION, ICON_MOLECULE_CO2, 0, DEVICE_CLASS_EMPTY + UNIT_PARTS_PER_MILLION, + ICON_MOLECULE_CO2, + 0, + DEVICE_CLASS_EMPTY, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( - UNIT_CELSIUS, ICON_EMPTY, 1, DEVICE_CLASS_TEMPERATURE + UNIT_CELSIUS, + ICON_EMPTY, + 1, + DEVICE_CLASS_TEMPERATURE, + STATE_CLASS_MEASUREMENT, ), cv.Optional(CONF_HUMIDITY): sensor.sensor_schema( - UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY + UNIT_PERCENT, ICON_EMPTY, 1, DEVICE_CLASS_HUMIDITY, STATE_CLASS_MEASUREMENT ), } ).extend(cv.polling_component_schema("60s")) diff --git a/esphome/const.py b/esphome/const.py index 2316cee7dc..18ef10bb01 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -538,6 +538,7 @@ CONF_SPIKE_REJECTION = "spike_rejection" CONF_SSID = "ssid" CONF_SSL_FINGERPRINTS = "ssl_fingerprints" CONF_STATE = "state" +CONF_STATE_CLASS = "state_class" CONF_STATE_TOPIC = "state_topic" CONF_STATIC_IP = "static_ip" CONF_STATUS = "status" @@ -768,3 +769,9 @@ DEVICE_CLASS_POWER_FACTOR = "power_factor" DEVICE_CLASS_PRESSURE = "pressure" DEVICE_CLASS_TIMESTAMP = "timestamp" DEVICE_CLASS_VOLTAGE = "voltage" + +# state classes +STATE_CLASS_NONE = "" + +# The state represents a measurement in present time +STATE_CLASS_MEASUREMENT = "measurement" diff --git a/tests/test1.yaml b/tests/test1.yaml index e9e89defd3..378d4192d3 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -261,7 +261,6 @@ mcp23s17: cs_pin: GPIO12 deviceaddress: 1 - sensor: - platform: ble_client ble_client_id: ble_foo @@ -794,6 +793,7 @@ sensor: update_interval: 15s - platform: template name: 'Template Sensor' + state_class: measurement id: template_sensor lambda: |- if (id(ultrasonic_sensor1).state > 1) {