Add support for off_delay to MQTT binary_sensor (#16993)
* Add support for off_delay to MQTT binary_sensor * Fix debounce, add testcase * Make off_delay number of seconds instead of timedelta * Update mqtt.py * Fix testcase, remove CONF_OFF_DELAY from const.py
This commit is contained in:
parent
61bf4d8a29
commit
5961f2f577
2 changed files with 72 additions and 4 deletions
|
@ -1,6 +1,8 @@
|
|||
"""The tests for the MQTT binary sensor platform."""
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
from datetime import timedelta
|
||||
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.setup import setup_component, async_setup_component
|
||||
|
@ -10,10 +12,12 @@ from homeassistant.components.mqtt.discovery import async_start
|
|||
from homeassistant.const import STATE_OFF, STATE_ON
|
||||
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE
|
||||
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, fire_mqtt_message, async_fire_mqtt_message,
|
||||
mock_component, mock_mqtt_component, async_mock_mqtt_component,
|
||||
MockConfigEntry)
|
||||
fire_time_changed, mock_component, mock_mqtt_component,
|
||||
async_mock_mqtt_component, MockConfigEntry)
|
||||
|
||||
|
||||
class TestSensorMQTT(unittest.TestCase):
|
||||
|
@ -22,6 +26,7 @@ class TestSensorMQTT(unittest.TestCase):
|
|||
def setUp(self): # pylint: disable=invalid-name
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.config_entries._async_schedule_save = Mock()
|
||||
mock_mqtt_component(self.hass)
|
||||
|
||||
def tearDown(self): # pylint: disable=invalid-name
|
||||
|
@ -209,6 +214,48 @@ class TestSensorMQTT(unittest.TestCase):
|
|||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(events))
|
||||
|
||||
def test_off_delay(self):
|
||||
"""Test off_delay 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',
|
||||
'off_delay': 30,
|
||||
'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()
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
self.assertEqual(1, len(events))
|
||||
|
||||
fire_mqtt_message(self.hass, 'test-topic', 'ON')
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
self.assertEqual(2, len(events))
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=30))
|
||||
self.hass.block_till_done()
|
||||
state = self.hass.states.get('binary_sensor.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
self.assertEqual(3, len(events))
|
||||
|
||||
|
||||
async def test_unique_id(hass):
|
||||
"""Test unique id option only creates one sensor per unique_id."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue