Fix statistics for binary sensor (#18764)
* Fix statistics for binary sensor -) Binary sensors have 'on' and 'off' for state resulting in issue as numbers were expected. Fixed so that it works with non-numeric states as well. -) Added check to skip unknown states. -) Updates test so that binary sensor test will use non-numeric values for states. * Using guard clause and changed debug to error Changed to use a guard clause for state unknown. Writing error on value error instead of debug. * Add docstring
This commit is contained in:
parent
48e28843e6
commit
aadf72d445
2 changed files with 13 additions and 4 deletions
|
@ -120,7 +120,7 @@ class StatisticsSensor(Entity):
|
|||
self.hass, self._entity_id, async_stats_sensor_state_listener)
|
||||
|
||||
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()
|
||||
)
|
||||
|
@ -129,11 +129,20 @@ class StatisticsSensor(Entity):
|
|||
EVENT_HOMEASSISTANT_START, async_stats_sensor_startup)
|
||||
|
||||
def _add_state_to_queue(self, new_state):
|
||||
"""Add the state to the queue."""
|
||||
if new_state.state == STATE_UNKNOWN:
|
||||
return
|
||||
|
||||
try:
|
||||
self.states.append(float(new_state.state))
|
||||
if self.is_binary:
|
||||
self.states.append(new_state.state)
|
||||
else:
|
||||
self.states.append(float(new_state.state))
|
||||
|
||||
self.ages.append(new_state.last_updated)
|
||||
except ValueError:
|
||||
pass
|
||||
_LOGGER.error("%s: parsing error, expected number and received %s",
|
||||
self.entity_id, new_state.state)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
|
|
@ -40,7 +40,7 @@ class TestStatisticsSensor(unittest.TestCase):
|
|||
|
||||
def test_binary_sensor_source(self):
|
||||
"""Test if source is a sensor."""
|
||||
values = [1, 0, 1, 0, 1, 0, 1]
|
||||
values = ['on', 'off', 'on', 'off', 'on', 'off', 'on']
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'statistics',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue