Drop unnecessary block_till_done, improve tests (#23247)

This commit is contained in:
Erik Montnemery 2019-04-19 21:26:56 +02:00 committed by Jason Hu
parent 887e1cd8e3
commit 0e429cca33

View File

@ -1,39 +1,24 @@
"""The tests for the MQTT binary sensor platform.""" """The tests for the MQTT binary sensor platform."""
from datetime import timedelta from datetime import timedelta
import json import json
import unittest from unittest.mock import ANY
from unittest.mock import ANY, Mock
from homeassistant.components import binary_sensor, mqtt from homeassistant.components import binary_sensor, mqtt
from homeassistant.components.mqtt.discovery import async_start from homeassistant.components.mqtt.discovery import async_start
from homeassistant.const import ( from homeassistant.const import (
EVENT_STATE_CHANGED, STATE_OFF, STATE_ON, STATE_UNAVAILABLE) EVENT_STATE_CHANGED, STATE_OFF, STATE_ON, STATE_UNAVAILABLE)
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.setup import async_setup_component, setup_component from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from tests.common import ( from tests.common import (
MockConfigEntry, async_fire_mqtt_message, async_mock_mqtt_component, MockConfigEntry, async_fire_mqtt_message, async_fire_time_changed,
fire_mqtt_message, fire_time_changed, get_test_home_assistant, async_mock_mqtt_component, mock_registry)
mock_component, mock_mqtt_component, mock_registry)
class TestSensorMQTT(unittest.TestCase): async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock):
"""Test the MQTT sensor."""
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
"""Stop everything that was started."""
self.hass.stop()
def test_setting_sensor_value_via_mqtt_message(self):
"""Test the setting of the value via MQTT.""" """Test the setting of the value via MQTT."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -43,22 +28,22 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state assert STATE_OFF == state.state
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() state = hass.states.get('binary_sensor.test')
state = self.hass.states.get('binary_sensor.test')
assert STATE_ON == state.state assert STATE_ON == state.state
fire_mqtt_message(self.hass, 'test-topic', 'OFF') async_fire_mqtt_message(hass, 'test-topic', 'OFF')
self.hass.block_till_done() state = hass.states.get('binary_sensor.test')
state = self.hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state assert STATE_OFF == state.state
def test_setting_sensor_value_via_mqtt_message_and_template(self):
async def test_setting_sensor_value_via_mqtt_message_and_template(
hass, mqtt_mock):
"""Test the setting of the value via MQTT.""" """Test the setting of the value via MQTT."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -70,22 +55,21 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state assert STATE_OFF == state.state
fire_mqtt_message(self.hass, 'test-topic', '') async_fire_mqtt_message(hass, 'test-topic', '')
self.hass.block_till_done() state = hass.states.get('binary_sensor.test')
state = self.hass.states.get('binary_sensor.test')
assert STATE_ON == state.state assert STATE_ON == state.state
fire_mqtt_message(self.hass, 'test-topic', '') async_fire_mqtt_message(hass, 'test-topic', '')
self.hass.block_till_done() state = hass.states.get('binary_sensor.test')
state = self.hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state assert STATE_OFF == state.state
def test_valid_device_class(self):
async def test_valid_device_class(hass, mqtt_mock):
"""Test the setting of a valid sensor class.""" """Test the setting of a valid sensor class."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -94,12 +78,13 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert 'motion' == state.attributes.get('device_class') assert 'motion' == state.attributes.get('device_class')
def test_invalid_device_class(self):
async def test_invalid_device_class(hass, mqtt_mock):
"""Test the setting of an invalid sensor class.""" """Test the setting of an invalid sensor class."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -108,12 +93,13 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert state is None assert state is None
def test_availability_without_topic(self):
async def test_availability_without_topic(hass, mqtt_mock):
"""Test availability without defined availability topic.""" """Test availability without defined availability topic."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -121,12 +107,13 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE != state.state assert STATE_UNAVAILABLE != state.state
def test_availability_by_defaults(self):
async def test_availability_by_defaults(hass, mqtt_mock):
"""Test availability by defaults with defined topic.""" """Test availability by defaults with defined topic."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -135,24 +122,23 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state assert STATE_UNAVAILABLE == state.state
fire_mqtt_message(self.hass, 'availability-topic', 'online') async_fire_mqtt_message(hass, 'availability-topic', 'online')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE != state.state assert STATE_UNAVAILABLE != state.state
fire_mqtt_message(self.hass, 'availability-topic', 'offline') async_fire_mqtt_message(hass, 'availability-topic', 'offline')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state assert STATE_UNAVAILABLE == state.state
def test_availability_by_custom_payload(self):
async def test_availability_by_custom_payload(hass, mqtt_mock):
"""Test availability by custom payload with defined topic.""" """Test availability by custom payload with defined topic."""
assert setup_component(self.hass, binary_sensor.DOMAIN, { assert await async_setup_component(hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -163,25 +149,23 @@ class TestSensorMQTT(unittest.TestCase):
} }
}) })
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state assert STATE_UNAVAILABLE == state.state
fire_mqtt_message(self.hass, 'availability-topic', 'good') async_fire_mqtt_message(hass, 'availability-topic', 'good')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE != state.state assert STATE_UNAVAILABLE != state.state
fire_mqtt_message(self.hass, 'availability-topic', 'nogood') async_fire_mqtt_message(hass, 'availability-topic', 'nogood')
self.hass.block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_UNAVAILABLE == state.state assert STATE_UNAVAILABLE == state.state
def test_force_update_disabled(self):
async def test_force_update_disabled(hass, mqtt_mock):
"""Test force update option.""" """Test force update option."""
mock_component(self.hass, 'mqtt') assert await async_setup_component(hass, binary_sensor.DOMAIN, {
assert setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -198,20 +182,20 @@ class TestSensorMQTT(unittest.TestCase):
"""Verify event got called.""" """Verify event got called."""
events.append(event) events.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback) hass.bus.async_listen(EVENT_STATE_CHANGED, callback)
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() await hass.async_block_till_done()
assert 1 == len(events) assert 1 == len(events)
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() await hass.async_block_till_done()
assert 1 == len(events) assert 1 == len(events)
def test_force_update_enabled(self):
async def test_force_update_enabled(hass, mqtt_mock):
"""Test force update option.""" """Test force update option."""
mock_component(self.hass, 'mqtt') assert await async_setup_component(hass, binary_sensor.DOMAIN, {
assert setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -229,20 +213,20 @@ class TestSensorMQTT(unittest.TestCase):
"""Verify event got called.""" """Verify event got called."""
events.append(event) events.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback) hass.bus.async_listen(EVENT_STATE_CHANGED, callback)
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() await hass.async_block_till_done()
assert 1 == len(events) assert 1 == len(events)
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() await hass.async_block_till_done()
assert 2 == len(events) assert 2 == len(events)
def test_off_delay(self):
async def test_off_delay(hass, mqtt_mock):
"""Test off_delay option.""" """Test off_delay option."""
mock_component(self.hass, 'mqtt') assert await async_setup_component(hass, binary_sensor.DOMAIN, {
assert setup_component(self.hass, binary_sensor.DOMAIN, {
binary_sensor.DOMAIN: { binary_sensor.DOMAIN: {
'platform': 'mqtt', 'platform': 'mqtt',
'name': 'test', 'name': 'test',
@ -261,23 +245,23 @@ class TestSensorMQTT(unittest.TestCase):
"""Verify event got called.""" """Verify event got called."""
events.append(event) events.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback) hass.bus.async_listen(EVENT_STATE_CHANGED, callback)
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_ON == state.state assert STATE_ON == state.state
assert 1 == len(events) assert 1 == len(events)
fire_mqtt_message(self.hass, 'test-topic', 'ON') async_fire_mqtt_message(hass, 'test-topic', 'ON')
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_ON == state.state assert STATE_ON == state.state
assert 2 == len(events) assert 2 == len(events)
fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=30)) async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=30))
self.hass.block_till_done() await hass.async_block_till_done()
state = self.hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert STATE_OFF == state.state assert STATE_OFF == state.state
assert 3 == len(events) assert 3 == len(events)
@ -294,7 +278,6 @@ async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
}) })
async_fire_mqtt_message(hass, 'attr-topic', '{ "val": "100" }') async_fire_mqtt_message(hass, 'attr-topic', '{ "val": "100" }')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert '100' == state.attributes.get('val') assert '100' == state.attributes.get('val')
@ -312,7 +295,6 @@ async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
}) })
async_fire_mqtt_message(hass, 'attr-topic', '[ "list", "of", "things"]') async_fire_mqtt_message(hass, 'attr-topic', '[ "list", "of", "things"]')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert state.attributes.get('val') is None assert state.attributes.get('val') is None
@ -331,7 +313,6 @@ async def test_update_with_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
}) })
async_fire_mqtt_message(hass, 'attr-topic', 'This is not JSON') async_fire_mqtt_message(hass, 'attr-topic', 'This is not JSON')
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.test') state = hass.states.get('binary_sensor.test')
assert state.attributes.get('val') is None assert state.attributes.get('val') is None
@ -356,8 +337,6 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
data1) data1)
await hass.async_block_till_done() await hass.async_block_till_done()
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "100" }') async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "100" }')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer') state = hass.states.get('binary_sensor.beer')
assert '100' == state.attributes.get('val') assert '100' == state.attributes.get('val')
@ -365,19 +344,14 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data2) data2)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
# Verify we are no longer subscribing to the old topic # Verify we are no longer subscribing to the old topic
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "50" }') async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "50" }')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer') state = hass.states.get('binary_sensor.beer')
assert '100' == state.attributes.get('val') assert '100' == state.attributes.get('val')
# Verify we are subscribing to the new topic # Verify we are subscribing to the new topic
async_fire_mqtt_message(hass, 'attr-topic2', '{ "val": "75" }') async_fire_mqtt_message(hass, 'attr-topic2', '{ "val": "75" }')
await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer') state = hass.states.get('binary_sensor.beer')
assert '75' == state.attributes.get('val') assert '75' == state.attributes.get('val')
@ -399,7 +373,6 @@ async def test_unique_id(hass):
}] }]
}) })
async_fire_mqtt_message(hass, 'test-topic', 'payload') async_fire_mqtt_message(hass, 'test-topic', 'payload')
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1 assert len(hass.states.async_all()) == 1
@ -421,7 +394,6 @@ async def test_discovery_removal_binary_sensor(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
'') '')
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer') state = hass.states.get('binary_sensor.beer')
assert state is None assert state is None
@ -449,7 +421,6 @@ async def test_discovery_update_binary_sensor(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data2) data2)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer') state = hass.states.get('binary_sensor.beer')
assert state is not None assert state is not None
assert state.name == 'Milk' assert state.name == 'Milk'
@ -482,7 +453,6 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data2) data2)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.milk') state = hass.states.get('binary_sensor.milk')
assert state is not None assert state is not None
@ -517,7 +487,6 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data) data)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
device = registry.async_get_device({('mqtt', 'helloworld')}, set()) device = registry.async_get_device({('mqtt', 'helloworld')}, set())
assert device is not None assert device is not None
@ -557,7 +526,6 @@ async def test_entity_device_info_update(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data) data)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
device = registry.async_get_device({('mqtt', 'helloworld')}, set()) device = registry.async_get_device({('mqtt', 'helloworld')}, set())
assert device is not None assert device is not None
@ -568,7 +536,6 @@ async def test_entity_device_info_update(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
data) data)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
device = registry.async_get_device({('mqtt', 'helloworld')}, set()) device = registry.async_get_device({('mqtt', 'helloworld')}, set())
assert device is not None assert device is not None
@ -599,7 +566,6 @@ async def test_entity_id_update(hass, mqtt_mock):
registry.async_update_entity( registry.async_update_entity(
'binary_sensor.beer', new_entity_id='binary_sensor.milk') 'binary_sensor.beer', new_entity_id='binary_sensor.milk')
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('binary_sensor.beer') state = hass.states.get('binary_sensor.beer')
assert state is None assert state is None