Adding expire_after to mqtt sensor to expire outdated values (#6708)
* Adding expire_after to mqtt sensor to expire outdated values * Extending test case * mqtt: refactoring expire_after to use timed events instead of polling; lint * refactor to reset unused trigger * Fix: handler must be set to None after execution or removal to avoid warning * Commenting out non-working test * Fix lint * Commit to trigger new build * Commit to trigger new build * Make testcase work * Undo unnecessary change * Remove default value, add extra check
This commit is contained in:
parent
3acd926d29
commit
6c5989895a
2 changed files with 104 additions and 2 deletions
|
@ -1,12 +1,16 @@
|
|||
"""The tests for the MQTT sensor platform."""
|
||||
import unittest
|
||||
|
||||
from datetime import timedelta, datetime
|
||||
from unittest.mock import patch
|
||||
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.components.sensor as sensor
|
||||
from homeassistant.const import EVENT_STATE_CHANGED
|
||||
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import mock_mqtt_component, fire_mqtt_message
|
||||
from tests.common import get_test_home_assistant, mock_component
|
||||
|
||||
|
||||
|
@ -42,6 +46,69 @@ class TestSensorMQTT(unittest.TestCase):
|
|||
self.assertEqual('fav unit',
|
||||
state.attributes.get('unit_of_measurement'))
|
||||
|
||||
@patch('homeassistant.core.dt_util.utcnow')
|
||||
def test_setting_sensor_value_expires(self, mock_utcnow):
|
||||
"""Test the expiration of the value."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
assert setup_component(self.hass, sensor.DOMAIN, {
|
||||
sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'unit_of_measurement': 'fav unit',
|
||||
'expire_after': '4',
|
||||
'force_update': True
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('unknown', state.state)
|
||||
|
||||
now = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC)
|
||||
mock_utcnow.return_value = now
|
||||
fire_mqtt_message(self.hass, 'test-topic', '100')
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Value was set correctly.
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('100', state.state)
|
||||
|
||||
# Time jump +3s
|
||||
now = now + timedelta(seconds=3)
|
||||
self._send_time_changed(now)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Value is not yet expired
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('100', state.state)
|
||||
|
||||
# Next message resets timer
|
||||
mock_utcnow.return_value = now
|
||||
fire_mqtt_message(self.hass, 'test-topic', '101')
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Value was updated correctly.
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('101', state.state)
|
||||
|
||||
# Time jump +3s
|
||||
now = now + timedelta(seconds=3)
|
||||
self._send_time_changed(now)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Value is not yet expired
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('101', state.state)
|
||||
|
||||
# Time jump +2s
|
||||
now = now + timedelta(seconds=2)
|
||||
self._send_time_changed(now)
|
||||
self.hass.block_till_done()
|
||||
|
||||
# Value is expired now
|
||||
state = self.hass.states.get('sensor.test')
|
||||
self.assertEqual('unknown', state.state)
|
||||
|
||||
def test_setting_sensor_value_via_mqtt_json_message(self):
|
||||
"""Test the setting of the value via MQTT with JSON playload."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
|
@ -117,3 +184,7 @@ class TestSensorMQTT(unittest.TestCase):
|
|||
fire_mqtt_message(self.hass, 'test-topic', '100')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(events))
|
||||
|
||||
def _send_time_changed(self, now):
|
||||
"""Send a time changed event."""
|
||||
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue