Add a 'last' mode and attribute to min_max sensor (#11037)

* Add 'last' type to min/max sensor

Now supports types: min, max, mean, last
'last' is the most recently received value from all tracked entities.

* Min/max sensor 'last' type test

* Fix min/max sensor 'last' test
This commit is contained in:
markferry 2018-01-18 22:03:41 +00:00 committed by Fabian Affolter
parent 536424b0c8
commit 526405c83b
2 changed files with 35 additions and 1 deletions

View file

@ -23,12 +23,14 @@ ATTR_MIN_VALUE = 'min_value'
ATTR_MAX_VALUE = 'max_value'
ATTR_COUNT_SENSORS = 'count_sensors'
ATTR_MEAN = 'mean'
ATTR_LAST = 'last'
ATTR_TO_PROPERTY = [
ATTR_COUNT_SENSORS,
ATTR_MAX_VALUE,
ATTR_MEAN,
ATTR_MIN_VALUE,
ATTR_LAST,
]
CONF_ENTITY_IDS = 'entity_ids'
@ -40,6 +42,7 @@ SENSOR_TYPES = {
ATTR_MIN_VALUE: 'min',
ATTR_MAX_VALUE: 'max',
ATTR_MEAN: 'mean',
ATTR_LAST: 'last',
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
@ -116,7 +119,7 @@ class MinMaxSensor(Entity):
if self._sensor_type == v)).capitalize()
self._unit_of_measurement = None
self._unit_of_measurement_mismatch = False
self.min_value = self.max_value = self.mean = STATE_UNKNOWN
self.min_value = self.max_value = self.mean = self.last = STATE_UNKNOWN
self.count_sensors = len(self._entity_ids)
self.states = {}
@ -142,6 +145,7 @@ class MinMaxSensor(Entity):
try:
self.states[entity] = float(new_state.state)
self.last = float(new_state.state)
except ValueError:
_LOGGER.warning("Unable to store state. "
"Only numerical states are supported")