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' testpull/11777/head
parent
536424b0c8
commit
526405c83b
|
@ -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")
|
||||
|
|
|
@ -260,3 +260,33 @@ class TestMinMaxSensor(unittest.TestCase):
|
|||
|
||||
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||
self.assertEqual('ERR', state.attributes.get('unit_of_measurement'))
|
||||
|
||||
def test_last_sensor(self):
|
||||
"""Test the last sensor."""
|
||||
config = {
|
||||
'sensor': {
|
||||
'platform': 'min_max',
|
||||
'name': 'test_last',
|
||||
'type': 'last',
|
||||
'entity_ids': [
|
||||
'sensor.test_1',
|
||||
'sensor.test_2',
|
||||
'sensor.test_3',
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
assert setup_component(self.hass, 'sensor', config)
|
||||
|
||||
entity_ids = config['sensor']['entity_ids']
|
||||
state = self.hass.states.get('sensor.test_last')
|
||||
|
||||
for entity_id, value in dict(zip(entity_ids, self.values)).items():
|
||||
self.hass.states.set(entity_id, value)
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get('sensor.test_last')
|
||||
self.assertEqual(str(float(value)), state.state)
|
||||
|
||||
self.assertEqual(self.min, state.attributes.get('min_value'))
|
||||
self.assertEqual(self.max, state.attributes.get('max_value'))
|
||||
self.assertEqual(self.mean, state.attributes.get('mean'))
|
||||
|
|
Loading…
Reference in New Issue