Correct unit_of_measurement for statistics sensor (#58023)

This commit is contained in:
Erik Montnemery 2021-10-20 20:28:48 +02:00 committed by GitHub
parent 35e9cf68a2
commit 398061706c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -150,10 +150,6 @@ class StatisticsSensor(SensorEntity):
if (new_state := event.data.get("new_state")) is None: if (new_state := event.data.get("new_state")) is None:
return return
self._unit_of_measurement = new_state.attributes.get(
ATTR_UNIT_OF_MEASUREMENT
)
self._add_state_to_queue(new_state) self._add_state_to_queue(new_state)
self.async_schedule_update_ha_state(True) self.async_schedule_update_ha_state(True)
@ -171,7 +167,7 @@ class StatisticsSensor(SensorEntity):
if "recorder" in self.hass.config.components: if "recorder" in self.hass.config.components:
# Only use the database if it's configured # Only use the database if it's configured
self.hass.async_create_task(self._async_initialize_from_database()) self.hass.async_create_task(self._initialize_from_database())
self.hass.bus.async_listen_once( self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, async_stats_sensor_startup EVENT_HOMEASSISTANT_START, async_stats_sensor_startup
@ -195,6 +191,9 @@ class StatisticsSensor(SensorEntity):
self.entity_id, self.entity_id,
new_state.state, new_state.state,
) )
return
self._unit_of_measurement = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
@property @property
def name(self): def name(self):
@ -355,7 +354,7 @@ class StatisticsSensor(SensorEntity):
self.hass, _scheduled_update, next_to_purge_timestamp self.hass, _scheduled_update, next_to_purge_timestamp
) )
async def _async_initialize_from_database(self): async def _initialize_from_database(self):
"""Initialize the list of states from the database. """Initialize the list of states from the database.
The query will get the list of states in DESCENDING order so that we The query will get the list of states in DESCENDING order so that we

View File

@ -123,6 +123,18 @@ class TestStatisticsSensor(unittest.TestCase):
assert self.change == state.attributes.get("change") assert self.change == state.attributes.get("change")
assert self.average_change == state.attributes.get("average_change") assert self.average_change == state.attributes.get("average_change")
# Source sensor is unavailable, unit and state should not change
self.hass.states.set("sensor.test_monitored", "unavailable", {})
self.hass.block_till_done()
new_state = self.hass.states.get("sensor.test")
assert state == new_state
# Source sensor has a non float state, unit and state should not change
self.hass.states.set("sensor.test_monitored", "beer", {})
self.hass.block_till_done()
new_state = self.hass.states.get("sensor.test")
assert state == new_state
def test_sampling_size(self): def test_sampling_size(self):
"""Test rotation.""" """Test rotation."""
assert setup_component( assert setup_component(
@ -380,6 +392,7 @@ class TestStatisticsSensor(unittest.TestCase):
# check if the result is as in test_sensor_source() # check if the result is as in test_sensor_source()
state = self.hass.states.get("sensor.test") state = self.hass.states.get("sensor.test")
assert str(self.mean) == state.state assert str(self.mean) == state.state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_CELSIUS
def test_initialize_from_database_with_maxage(self): def test_initialize_from_database_with_maxage(self):
"""Test initializing the statistics from the database.""" """Test initializing the statistics from the database."""