mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
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
@ -120,7 +120,7 @@ class StatisticsSensor(Entity):
|
|||||||
self.hass, self._entity_id, async_stats_sensor_state_listener)
|
self.hass, self._entity_id, async_stats_sensor_state_listener)
|
||||||
|
|
||||||
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.hass.async_create_task(
|
||||||
self._async_initialize_from_database()
|
self._async_initialize_from_database()
|
||||||
)
|
)
|
||||||
@ -129,11 +129,20 @@ class StatisticsSensor(Entity):
|
|||||||
EVENT_HOMEASSISTANT_START, async_stats_sensor_startup)
|
EVENT_HOMEASSISTANT_START, async_stats_sensor_startup)
|
||||||
|
|
||||||
def _add_state_to_queue(self, new_state):
|
def _add_state_to_queue(self, new_state):
|
||||||
|
"""Add the state to the queue."""
|
||||||
|
if new_state.state == STATE_UNKNOWN:
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
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)
|
self.ages.append(new_state.last_updated)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
_LOGGER.error("%s: parsing error, expected number and received %s",
|
||||||
|
self.entity_id, new_state.state)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -40,7 +40,7 @@ class TestStatisticsSensor(unittest.TestCase):
|
|||||||
|
|
||||||
def test_binary_sensor_source(self):
|
def test_binary_sensor_source(self):
|
||||||
"""Test if source is a sensor."""
|
"""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', {
|
assert setup_component(self.hass, 'sensor', {
|
||||||
'sensor': {
|
'sensor': {
|
||||||
'platform': 'statistics',
|
'platform': 'statistics',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user