Force update support for mqtt binary sensor (#12092)
This commit is contained in:
parent
6265d1b747
commit
25cbc8317f
2 changed files with 80 additions and 7 deletions
|
@ -1,13 +1,15 @@
|
|||
"""The tests for the MQTT binary sensor platform."""
|
||||
import unittest
|
||||
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.components.binary_sensor as binary_sensor
|
||||
from homeassistant.const import (STATE_OFF, STATE_ON,
|
||||
STATE_UNAVAILABLE)
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message)
|
||||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
||||
|
||||
from tests.common import get_test_home_assistant, fire_mqtt_message
|
||||
from tests.common import mock_component, mock_mqtt_component
|
||||
|
||||
|
||||
class TestSensorMQTT(unittest.TestCase):
|
||||
|
@ -141,3 +143,64 @@ class TestSensorMQTT(unittest.TestCase):
|
|||
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
self.assertEqual(STATE_UNAVAILABLE, state.state)
|
||||
|
||||
def test_force_update_disabled(self):
|
||||
"""Test force update option."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||
binary_sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'payload_on': 'ON',
|
||||
'payload_off': 'OFF'
|
||||
}
|
||||
})
|
||||
|
||||
events = []
|
||||
|
||||
@ha.callback
|
||||
def callback(event):
|
||||
"""Verify event got called."""
|
||||
events.append(event)
|
||||
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
def test_force_update_enabled(self):
|
||||
"""Test force update option."""
|
||||
mock_component(self.hass, 'mqtt')
|
||||
assert setup_component(self.hass, binary_sensor.DOMAIN, {
|
||||
binary_sensor.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'name': 'test',
|
||||
'state_topic': 'test-topic',
|
||||
'payload_on': 'ON',
|
||||
'payload_off': 'OFF',
|
||||
'force_update': True
|
||||
}
|
||||
})
|
||||
|
||||
events = []
|
||||
|
||||
@ha.callback
|
||||
def callback(event):
|
||||
"""Verify event got called."""
|
||||
events.append(event)
|
||||
|
||||
self.hass.bus.listen(EVENT_STATE_CHANGED, callback)
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(events))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue