mirror of
https://github.com/home-assistant/core.git
synced 2025-08-03 12:45:28 +02:00
Better handling of mismatch in unit of measurement.
Set state to "unkown" and unit of measurement to "ERR" if unit of measurement differs between aggregatet states. Add entity_id to logged warning.
This commit is contained in:
@@ -115,6 +115,7 @@ class MinMaxSensor(Entity):
|
|||||||
next(v for k, v in SENSOR_TYPES.items()
|
next(v for k, v in SENSOR_TYPES.items()
|
||||||
if self._sensor_type == v)).capitalize()
|
if self._sensor_type == v)).capitalize()
|
||||||
self._unit_of_measurement = None
|
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 = STATE_UNKNOWN
|
||||||
self.count_sensors = len(self._entity_ids)
|
self.count_sensors = len(self._entity_ids)
|
||||||
self.states = {}
|
self.states = {}
|
||||||
@@ -134,7 +135,10 @@ class MinMaxSensor(Entity):
|
|||||||
|
|
||||||
if self._unit_of_measurement != new_state.attributes.get(
|
if self._unit_of_measurement != new_state.attributes.get(
|
||||||
ATTR_UNIT_OF_MEASUREMENT):
|
ATTR_UNIT_OF_MEASUREMENT):
|
||||||
_LOGGER.warning("Units of measurement do not match")
|
_LOGGER.warning(
|
||||||
|
"Units of measurement do not match for entity %s" %
|
||||||
|
self.entity_id)
|
||||||
|
self._unit_of_measurement_mismatch = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.states[entity] = float(new_state.state)
|
self.states[entity] = float(new_state.state)
|
||||||
@@ -155,12 +159,16 @@ class MinMaxSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
|
if (self._unit_of_measurement_mismatch):
|
||||||
|
return STATE_UNKNOWN
|
||||||
return getattr(self, next(
|
return getattr(self, next(
|
||||||
k for k, v in SENSOR_TYPES.items() if self._sensor_type == v))
|
k for k, v in SENSOR_TYPES.items() if self._sensor_type == v))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit the value is expressed in."""
|
"""Return the unit the value is expressed in."""
|
||||||
|
if (self._unit_of_measurement_mismatch):
|
||||||
|
return "ERR"
|
||||||
return self._unit_of_measurement
|
return self._unit_of_measurement
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@@ -2,7 +2,8 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.setup import setup_component
|
||||||
from homeassistant.const import STATE_UNKNOWN
|
from homeassistant.const import (
|
||||||
|
STATE_UNKNOWN, ATTR_UNIT_OF_MEASUREMENT, TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
||||||
from tests.common import get_test_home_assistant
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
|
|
||||||
@@ -213,3 +214,49 @@ class TestMinMaxSensor(unittest.TestCase):
|
|||||||
|
|
||||||
state = self.hass.states.get('sensor.test_max')
|
state = self.hass.states.get('sensor.test_max')
|
||||||
self.assertEqual(STATE_UNKNOWN, state.state)
|
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||||
|
|
||||||
|
def test_different_unit_of_measurement(self):
|
||||||
|
"""Test for different unit of measurement."""
|
||||||
|
config = {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'min_max',
|
||||||
|
'name': 'test',
|
||||||
|
'type': 'mean',
|
||||||
|
'entity_ids': [
|
||||||
|
'sensor.test_1',
|
||||||
|
'sensor.test_2',
|
||||||
|
'sensor.test_3',
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert setup_component(self.hass, 'sensor', config)
|
||||||
|
|
||||||
|
entity_ids = config['sensor']['entity_ids']
|
||||||
|
|
||||||
|
self.hass.states.set(entity_ids[0], self.values[0],
|
||||||
|
{ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS})
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('sensor.test')
|
||||||
|
|
||||||
|
self.assertEqual(str(float(self.values[0])), state.state)
|
||||||
|
self.assertEqual('°C', state.attributes.get('unit_of_measurement'))
|
||||||
|
|
||||||
|
self.hass.states.set(entity_ids[1], self.values[1],
|
||||||
|
{ATTR_UNIT_OF_MEASUREMENT: TEMP_FAHRENHEIT})
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('sensor.test')
|
||||||
|
|
||||||
|
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||||
|
self.assertEqual('ERR', state.attributes.get('unit_of_measurement'))
|
||||||
|
|
||||||
|
self.hass.states.set(entity_ids[2], self.values[2],
|
||||||
|
{ATTR_UNIT_OF_MEASUREMENT: '%'})
|
||||||
|
self.hass.block_till_done()
|
||||||
|
|
||||||
|
state = self.hass.states.get('sensor.test')
|
||||||
|
|
||||||
|
self.assertEqual(STATE_UNKNOWN, state.state)
|
||||||
|
self.assertEqual('ERR', state.attributes.get('unit_of_measurement'))
|
||||||
|
Reference in New Issue
Block a user