From 7f41b7cd939c1e488fcc2fb66490ab007be6c54a Mon Sep 17 00:00:00 2001 From: Peter Galantha Date: Wed, 10 Aug 2022 19:00:09 -0700 Subject: [PATCH] Add state_class total (#3608) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/api/api.proto | 1 + esphome/components/api/api_pb2.cpp | 2 ++ esphome/components/api/api_pb2.h | 1 + esphome/components/sensor/__init__.py | 1 + esphome/components/sensor/sensor.cpp | 2 ++ esphome/components/sensor/sensor.h | 1 + esphome/const.py | 3 +++ 7 files changed, 11 insertions(+) diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 3e9a62f3d8..88a74540d0 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -473,6 +473,7 @@ enum SensorStateClass { STATE_CLASS_NONE = 0; STATE_CLASS_MEASUREMENT = 1; STATE_CLASS_TOTAL_INCREASING = 2; + STATE_CLASS_TOTAL = 3; } enum SensorLastResetType { diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 70f909c07a..b91c9bd600 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -108,6 +108,8 @@ template<> const char *proto_enum_to_string(enums::Sens return "STATE_CLASS_MEASUREMENT"; case enums::STATE_CLASS_TOTAL_INCREASING: return "STATE_CLASS_TOTAL_INCREASING"; + case enums::STATE_CLASS_TOTAL: + return "STATE_CLASS_TOTAL"; default: return "UNKNOWN"; } diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index ec1cdc35ac..f9981fdbb7 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -53,6 +53,7 @@ enum SensorStateClass : uint32_t { STATE_CLASS_NONE = 0, STATE_CLASS_MEASUREMENT = 1, STATE_CLASS_TOTAL_INCREASING = 2, + STATE_CLASS_TOTAL = 3, }; enum SensorLastResetType : uint32_t { LAST_RESET_NONE = 0, diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index e88cd05c31..d6ba038057 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -109,6 +109,7 @@ STATE_CLASSES = { "": StateClasses.STATE_CLASS_NONE, "measurement": StateClasses.STATE_CLASS_MEASUREMENT, "total_increasing": StateClasses.STATE_CLASS_TOTAL_INCREASING, + "total": StateClasses.STATE_CLASS_TOTAL, } validate_state_class = cv.enum(STATE_CLASSES, lower=True, space="_") diff --git a/esphome/components/sensor/sensor.cpp b/esphome/components/sensor/sensor.cpp index c0869631aa..a729791e7e 100644 --- a/esphome/components/sensor/sensor.cpp +++ b/esphome/components/sensor/sensor.cpp @@ -12,6 +12,8 @@ std::string state_class_to_string(StateClass state_class) { return "measurement"; case STATE_CLASS_TOTAL_INCREASING: return "total_increasing"; + case STATE_CLASS_TOTAL: + return "total"; case STATE_CLASS_NONE: default: return ""; diff --git a/esphome/components/sensor/sensor.h b/esphome/components/sensor/sensor.h index f29125ee42..ba9edd68d0 100644 --- a/esphome/components/sensor/sensor.h +++ b/esphome/components/sensor/sensor.h @@ -36,6 +36,7 @@ enum StateClass : uint8_t { STATE_CLASS_NONE = 0, STATE_CLASS_MEASUREMENT = 1, STATE_CLASS_TOTAL_INCREASING = 2, + STATE_CLASS_TOTAL = 3, }; std::string state_class_to_string(StateClass state_class); diff --git a/esphome/const.py b/esphome/const.py index 0593c19672..4e1e293e94 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -956,6 +956,9 @@ STATE_CLASS_MEASUREMENT = "measurement" # The state represents a total that only increases, a decrease is considered a reset. STATE_CLASS_TOTAL_INCREASING = "total_increasing" +# The state represents a total amount that can both increase and decrease, e.g. a net energy meter. +STATE_CLASS_TOTAL = "total" + KEY_CORE = "core" KEY_TARGET_PLATFORM = "target_platform" KEY_TARGET_FRAMEWORK = "target_framework"