Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/trend/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def trend_sensor_state_listener(entity, old_state, new_state):
else:
state = new_state.state
if state not in (STATE_UNKNOWN, STATE_UNAVAILABLE):
sample = (utcnow().timestamp(), float(state))
sample = (new_state.last_updated.timestamp(), float(state))
self.samples.append(sample)
self.async_schedule_update_ha_state(True)
except (ValueError, TypeError) as ex:
Expand Down
35 changes: 25 additions & 10 deletions tests/components/trend/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""The test for the Trend sensor platform."""
from datetime import timedelta
from unittest.mock import patch

from homeassistant import setup
import homeassistant.util.dt as dt_util

from tests.common import get_test_home_assistant, assert_setup_component

Expand Down Expand Up @@ -46,24 +50,30 @@ def test_up_using_trendline(self):
'sensors': {
'test_trend_sensor': {
'entity_id': "sensor.test_state",
'sample_duration': 300,
'sample_duration': 10000,
'min_gradient': 1,
'max_samples': 25,
}
}
}
})

for val in [1, 0, 2, 3]:
self.hass.states.set('sensor.test_state', val)
now = dt_util.utcnow()
for val in [10, 0, 20, 30]:
with patch('homeassistant.util.dt.utcnow', return_value=now):
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
now += timedelta(seconds=2)

state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'

for val in [0, 1, 0, 0]:
self.hass.states.set('sensor.test_state', val)
# have to change state value, otherwise sample will lost
for val in [0, 30, 1, 0]:
with patch('homeassistant.util.dt.utcnow', return_value=now):
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
now += timedelta(seconds=2)

state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
Expand All @@ -76,7 +86,7 @@ def test_down_using_trendline(self):
'sensors': {
'test_trend_sensor': {
'entity_id': "sensor.test_state",
'sample_duration': 300,
'sample_duration': 10000,
'min_gradient': 1,
'max_samples': 25,
'invert': 'Yes'
Expand All @@ -85,16 +95,21 @@ def test_down_using_trendline(self):
}
})

for val in [3, 2, 3, 1]:
self.hass.states.set('sensor.test_state', val)
now = dt_util.utcnow()
for val in [30, 20, 30, 10]:
with patch('homeassistant.util.dt.utcnow', return_value=now):
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
now += timedelta(seconds=2)

state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'on'

for val in [4, 2, 4, 4]:
self.hass.states.set('sensor.test_state', val)
for val in [30, 0, 45, 50]:
with patch('homeassistant.util.dt.utcnow', return_value=now):
self.hass.states.set('sensor.test_state', val)
self.hass.block_till_done()
now += timedelta(seconds=2)

state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
Expand Down