Fix optimistic mode and add tests (#22899)

This commit is contained in:
Erik Montnemery 2019-04-10 22:56:34 +02:00 committed by Paulus Schoutsen
parent 6463b8165f
commit 38d92b2abf
3 changed files with 127 additions and 2 deletions

View file

@ -11,6 +11,7 @@ from homeassistant.setup import async_setup_component
from tests.common import (
MockConfigEntry, async_fire_mqtt_message, async_mock_mqtt_component,
mock_registry)
from tests.components.lock import common
async def test_controlling_state_via_topic(hass, mqtt_mock):
@ -75,6 +76,82 @@ async def test_controlling_state_via_topic_and_json_message(hass, mqtt_mock):
assert state.state is STATE_UNLOCKED
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
"""Test optimistic mode without state topic."""
assert await async_setup_component(hass, lock.DOMAIN, {
lock.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'command_topic': 'command-topic',
'payload_lock': 'LOCK',
'payload_unlock': 'UNLOCK'
}
})
state = hass.states.get('lock.test')
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
common.async_lock(hass, 'lock.test')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'command-topic', 'LOCK', 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('lock.test')
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
common.async_unlock(hass, 'lock.test')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'command-topic', 'UNLOCK', 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('lock.test')
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock):
"""Test optimistic mode without state topic."""
assert await async_setup_component(hass, lock.DOMAIN, {
lock.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'state-topic',
'command_topic': 'command-topic',
'payload_lock': 'LOCK',
'payload_unlock': 'UNLOCK',
'optimistic': True
}
})
state = hass.states.get('lock.test')
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
common.async_lock(hass, 'lock.test')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'command-topic', 'LOCK', 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('lock.test')
assert state.state is STATE_LOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
common.async_unlock(hass, 'lock.test')
await hass.async_block_till_done()
mqtt_mock.async_publish.assert_called_once_with(
'command-topic', 'UNLOCK', 0, False)
mqtt_mock.async_publish.reset_mock()
state = hass.states.get('lock.test')
assert state.state is STATE_UNLOCKED
assert state.attributes.get(ATTR_ASSUMED_STATE)
async def test_default_availability_payload(hass, mqtt_mock):
"""Test availability by default payload with defined topic."""
assert await async_setup_component(hass, lock.DOMAIN, {