Drop unnecessary block_till_done, improve tests (#23252)

This commit is contained in:
Erik Montnemery 2019-04-19 23:52:23 +02:00 committed by Jason Hu
parent f584878204
commit 73a7d5e6f4

View File

@ -1,7 +1,6 @@
"""The tests for the MQTT sensor platform.""" """The tests for the MQTT sensor platform."""
from datetime import datetime, timedelta from datetime import datetime, timedelta
import json import json
import unittest
from unittest.mock import ANY, patch from unittest.mock import ANY, patch
from homeassistant.components import mqtt from homeassistant.components import mqtt
@ -9,361 +8,340 @@ from homeassistant.components.mqtt.discovery import async_start
import homeassistant.components.sensor as sensor import homeassistant.components.sensor as sensor
from homeassistant.const import EVENT_STATE_CHANGED, STATE_UNAVAILABLE from homeassistant.const import EVENT_STATE_CHANGED, 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, assert_setup_component, async_fire_mqtt_message, MockConfigEntry, async_fire_mqtt_message, async_mock_mqtt_component,
async_mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, async_fire_time_changed, 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.""" """Test the setting of the value via MQTT."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit'
}
})
def setUp(self): # pylint: disable=invalid-name async_fire_mqtt_message(hass, 'test-topic', '100')
"""Set up things to be run when tests are started.""" state = hass.states.get('sensor.test')
self.hass = get_test_home_assistant()
mock_mqtt_component(self.hass)
def tearDown(self): # pylint: disable=invalid-name assert '100' == state.state
"""Stop down everything that was started.""" assert 'fav unit' == \
self.hass.stop() state.attributes.get('unit_of_measurement')
def test_setting_sensor_value_via_mqtt_message(self):
"""Test the setting of the value via MQTT."""
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'
}
})
fire_mqtt_message(self.hass, 'test-topic', '100') async def test_setting_sensor_value_expires(hass, mqtt_mock, caplog):
self.hass.block_till_done() """Test the expiration of the value."""
state = self.hass.states.get('sensor.test') assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'expire_after': '4',
'force_update': True
}
})
assert '100' == state.state state = hass.states.get('sensor.test')
assert 'fav unit' == \ assert 'unknown' == state.state
state.attributes.get('unit_of_measurement')
@patch('homeassistant.core.dt_util.utcnow') now = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC)
def test_setting_sensor_value_expires(self, mock_utcnow): with patch(('homeassistant.helpers.event.'
"""Test the expiration of the value.""" 'dt_util.utcnow'), return_value=now):
mock_component(self.hass, 'mqtt') async_fire_time_changed(hass, now)
assert setup_component(self.hass, sensor.DOMAIN, { async_fire_mqtt_message(hass, 'test-topic', '100')
sensor.DOMAIN: { await hass.async_block_till_done()
'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') # Value was set correctly.
assert 'unknown' == state.state state = hass.states.get('sensor.test')
assert '100' == state.state
now = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC) # Time jump +3s
mock_utcnow.return_value = now now = now + timedelta(seconds=3)
fire_mqtt_message(self.hass, 'test-topic', '100') async_fire_time_changed(hass, now)
self.hass.block_till_done() await hass.async_block_till_done()
# Value was set correctly. # Value is not yet expired
state = self.hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert '100' == state.state assert '100' == state.state
# Time jump +3s # Next message resets timer
now = now + timedelta(seconds=3) with patch(('homeassistant.helpers.event.'
self._send_time_changed(now) 'dt_util.utcnow'), return_value=now):
self.hass.block_till_done() async_fire_time_changed(hass, now)
async_fire_mqtt_message(hass, 'test-topic', '101')
await hass.async_block_till_done()
# Value is not yet expired # Value was updated correctly.
state = self.hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert '100' == state.state assert '101' == state.state
# Next message resets timer # Time jump +3s
mock_utcnow.return_value = now now = now + timedelta(seconds=3)
fire_mqtt_message(self.hass, 'test-topic', '101') async_fire_time_changed(hass, now)
self.hass.block_till_done() await hass.async_block_till_done()
# Value was updated correctly. # Value is not yet expired
state = self.hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert '101' == state.state assert '101' == state.state
# Time jump +3s # Time jump +2s
now = now + timedelta(seconds=3) now = now + timedelta(seconds=2)
self._send_time_changed(now) async_fire_time_changed(hass, now)
self.hass.block_till_done() await hass.async_block_till_done()
# Value is not yet expired # Value is expired now
state = self.hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert '101' == state.state assert 'unknown' == state.state
# Time jump +2s
now = now + timedelta(seconds=2)
self._send_time_changed(now)
self.hass.block_till_done()
# Value is expired now async def test_setting_sensor_value_via_mqtt_json_message(hass, mqtt_mock):
state = self.hass.states.get('sensor.test') """Test the setting of the value via MQTT with JSON payload."""
assert 'unknown' == state.state assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'value_template': '{{ value_json.val }}'
}
})
def test_setting_sensor_value_via_mqtt_json_message(self): async_fire_mqtt_message(hass, 'test-topic', '{ "val": "100" }')
"""Test the setting of the value via MQTT with JSON payload.""" state = hass.states.get('sensor.test')
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',
'value_template': '{{ value_json.val }}'
}
})
fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }') assert '100' == state.state
self.hass.block_till_done()
state = self.hass.states.get('sensor.test')
assert '100' == 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, sensor.DOMAIN, {
assert setup_component(self.hass, sensor.DOMAIN, { sensor.DOMAIN: {
sensor.DOMAIN: { 'platform': 'mqtt',
'platform': 'mqtt', 'name': 'test',
'name': 'test', 'state_topic': 'test-topic',
'state_topic': 'test-topic', 'unit_of_measurement': 'fav unit'
'unit_of_measurement': 'fav unit' }
} })
})
events = [] events = []
@ha.callback @ha.callback
def callback(event): def callback(event):
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', '100') async_fire_mqtt_message(hass, 'test-topic', '100')
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', '100') async_fire_mqtt_message(hass, 'test-topic', '100')
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):
"""Test force update option."""
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',
'force_update': True
}
})
events = [] async def test_force_update_enabled(hass, mqtt_mock):
"""Test force update option."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'force_update': True
}
})
@ha.callback events = []
def callback(event):
events.append(event)
self.hass.bus.listen(EVENT_STATE_CHANGED, callback) @ha.callback
def callback(event):
events.append(event)
fire_mqtt_message(self.hass, 'test-topic', '100') hass.bus.async_listen(EVENT_STATE_CHANGED, callback)
self.hass.block_till_done()
assert 1 == len(events)
fire_mqtt_message(self.hass, 'test-topic', '100') async_fire_mqtt_message(hass, 'test-topic', '100')
self.hass.block_till_done() await hass.async_block_till_done()
assert 2 == len(events) assert 1 == len(events)
def test_default_availability_payload(self): async_fire_mqtt_message(hass, 'test-topic', '100')
"""Test availability by default payload with defined topic.""" await hass.async_block_till_done()
assert setup_component(self.hass, sensor.DOMAIN, { assert 2 == len(events)
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'availability_topic': 'availability-topic'
}
})
state = self.hass.states.get('sensor.test')
assert STATE_UNAVAILABLE == state.state
fire_mqtt_message(self.hass, 'availability-topic', 'online') async def test_default_availability_payload(hass, mqtt_mock):
self.hass.block_till_done() """Test availability by default payload with defined topic."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'availability_topic': 'availability-topic'
}
})
state = self.hass.states.get('sensor.test') state = hass.states.get('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', 'online')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert STATE_UNAVAILABLE == state.state assert STATE_UNAVAILABLE != state.state
def test_custom_availability_payload(self): async_fire_mqtt_message(hass, 'availability-topic', 'offline')
"""Test availability by custom payload with defined topic."""
assert setup_component(self.hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'availability_topic': 'availability-topic',
'payload_available': 'good',
'payload_not_available': 'nogood'
}
})
state = self.hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert STATE_UNAVAILABLE == state.state assert STATE_UNAVAILABLE == state.state
fire_mqtt_message(self.hass, 'availability-topic', 'good')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test') async def test_custom_availability_payload(hass, mqtt_mock):
assert STATE_UNAVAILABLE != state.state """Test availability by custom payload with defined topic."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'availability_topic': 'availability-topic',
'payload_available': 'good',
'payload_not_available': 'nogood'
}
})
fire_mqtt_message(self.hass, 'availability-topic', 'nogood') state = hass.states.get('sensor.test')
self.hass.block_till_done() assert STATE_UNAVAILABLE == state.state
state = self.hass.states.get('sensor.test') async_fire_mqtt_message(hass, 'availability-topic', 'good')
assert STATE_UNAVAILABLE == state.state
def _send_time_changed(self, now): state = hass.states.get('sensor.test')
"""Send a time changed event.""" assert STATE_UNAVAILABLE != state.state
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
def test_setting_sensor_attribute_via_mqtt_json_message(self): async_fire_mqtt_message(hass, 'availability-topic', 'nogood')
"""Test the setting of attribute via MQTT with JSON payload."""
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',
'json_attributes': 'val'
}
})
fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }') state = hass.states.get('sensor.test')
self.hass.block_till_done() assert STATE_UNAVAILABLE == state.state
state = self.hass.states.get('sensor.test')
assert '100' == \
state.attributes.get('val')
@patch('homeassistant.components.mqtt.sensor._LOGGER') async def test_setting_sensor_attribute_via_legacy_mqtt_json_message(
def test_update_with_json_attrs_not_dict(self, mock_logger): hass, mqtt_mock):
"""Test attributes get extracted from a JSON result.""" """Test the setting of attribute via MQTT with JSON payload."""
mock_component(self.hass, 'mqtt') assert await async_setup_component(hass, sensor.DOMAIN, {
assert setup_component(self.hass, sensor.DOMAIN, { sensor.DOMAIN: {
sensor.DOMAIN: { 'platform': 'mqtt',
'platform': 'mqtt', 'name': 'test',
'name': 'test', 'state_topic': 'test-topic',
'state_topic': 'test-topic', 'unit_of_measurement': 'fav unit',
'unit_of_measurement': 'fav unit', 'json_attributes': 'val'
'json_attributes': 'val' }
} })
})
fire_mqtt_message(self.hass, 'test-topic', '[ "list", "of", "things"]') async_fire_mqtt_message(hass, 'test-topic', '{ "val": "100" }')
self.hass.block_till_done() state = hass.states.get('sensor.test')
state = self.hass.states.get('sensor.test')
assert state.attributes.get('val') is None assert '100' == \
assert mock_logger.warning.called state.attributes.get('val')
@patch('homeassistant.components.mqtt.sensor._LOGGER')
def test_update_with_json_attrs_bad_JSON(self, mock_logger):
"""Test attributes get extracted from a JSON result."""
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',
'json_attributes': 'val'
}
})
fire_mqtt_message(self.hass, 'test-topic', 'This is not JSON') async def test_update_with_legacy_json_attrs_not_dict(hass, mqtt_mock, caplog):
self.hass.block_till_done() """Test attributes get extracted from a JSON result."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'json_attributes': 'val'
}
})
state = self.hass.states.get('sensor.test') async_fire_mqtt_message(hass, 'test-topic', '[ "list", "of", "things"]')
assert state.attributes.get('val') is None state = hass.states.get('sensor.test')
assert mock_logger.warning.called
assert mock_logger.debug.called
def test_update_with_json_attrs_and_template(self): assert state.attributes.get('val') is None
"""Test attributes get extracted from a JSON result.""" assert 'JSON result was not a dictionary' in caplog.text
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',
'value_template': '{{ value_json.val }}',
'json_attributes': 'val'
}
})
fire_mqtt_message(self.hass, 'test-topic', '{ "val": "100" }')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test')
assert '100' == \ async def test_update_with_legacy_json_attrs_bad_JSON(hass, mqtt_mock, caplog):
state.attributes.get('val') """Test attributes get extracted from a JSON result."""
assert '100' == state.state assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'json_attributes': 'val'
}
})
def test_invalid_device_class(self): async_fire_mqtt_message(hass, 'test-topic', 'This is not JSON')
"""Test device_class option with invalid value."""
with assert_setup_component(0):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'device_class': 'foobarnotreal'
}
})
def test_valid_device_class(self): state = hass.states.get('sensor.test')
"""Test device_class option with valid values.""" assert state.attributes.get('val') is None
assert setup_component(self.hass, 'sensor', { assert 'Erroneous JSON: This is not JSON' in caplog.text
'sensor': [{
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'device_class': 'temperature'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'state_topic': 'test-topic',
}]
})
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_1')
assert state.attributes['device_class'] == 'temperature' async def test_update_with_legacy_json_attrs_and_template(hass, mqtt_mock):
state = self.hass.states.get('sensor.test_2') """Test attributes get extracted from a JSON result."""
assert 'device_class' not in state.attributes assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'unit_of_measurement': 'fav unit',
'value_template': '{{ value_json.val }}',
'json_attributes': 'val'
}
})
async_fire_mqtt_message(hass, 'test-topic', '{ "val": "100" }')
state = hass.states.get('sensor.test')
assert '100' == \
state.attributes.get('val')
assert '100' == state.state
async def test_invalid_device_class(hass, mqtt_mock):
"""Test device_class option with invalid value."""
assert await async_setup_component(hass, sensor.DOMAIN, {
sensor.DOMAIN: {
'platform': 'mqtt',
'name': 'test',
'state_topic': 'test-topic',
'device_class': 'foobarnotreal'
}
})
state = hass.states.get('sensor.test')
assert state is None
async def test_valid_device_class(hass, mqtt_mock):
"""Test device_class option with valid values."""
assert await async_setup_component(hass, 'sensor', {
'sensor': [{
'platform': 'mqtt',
'name': 'Test 1',
'state_topic': 'test-topic',
'device_class': 'temperature'
}, {
'platform': 'mqtt',
'name': 'Test 2',
'state_topic': 'test-topic',
}]
})
await hass.async_block_till_done()
state = hass.states.get('sensor.test_1')
assert state.attributes['device_class'] == 'temperature'
state = hass.states.get('sensor.test_2')
assert 'device_class' not in state.attributes
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock): async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
@ -378,7 +356,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('sensor.test') state = hass.states.get('sensor.test')
assert '100' == state.attributes.get('val') assert '100' == state.attributes.get('val')
@ -398,7 +375,6 @@ async def test_setting_attribute_with_template(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'attr-topic', json.dumps( async_fire_mqtt_message(hass, 'attr-topic', json.dumps(
{"Timer1": {"Arm": 0, "Time": "22:18"}})) {"Timer1": {"Arm": 0, "Time": "22:18"}}))
await hass.async_block_till_done()
state = hass.states.get('sensor.test') state = hass.states.get('sensor.test')
assert 0 == state.attributes.get('Arm') assert 0 == state.attributes.get('Arm')
@ -417,7 +393,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('sensor.test') state = hass.states.get('sensor.test')
assert state.attributes.get('val') is None assert state.attributes.get('val') is None
@ -436,7 +411,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('sensor.test') state = hass.states.get('sensor.test')
assert state.attributes.get('val') is None assert state.attributes.get('val') is None
@ -461,8 +435,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('sensor.beer') state = hass.states.get('sensor.beer')
assert '100' == state.attributes.get('val') assert '100' == state.attributes.get('val')
@ -470,19 +442,14 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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('sensor.beer') state = hass.states.get('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('sensor.beer') state = hass.states.get('sensor.beer')
assert '75' == state.attributes.get('val') assert '75' == state.attributes.get('val')
@ -505,7 +472,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
@ -527,7 +493,6 @@ async def test_discovery_removal_sensor(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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('sensor.beer') state = hass.states.get('sensor.beer')
assert state is None assert state is None
@ -555,7 +520,6 @@ async def test_discovery_update_sensor(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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('sensor.beer') state = hass.states.get('sensor.beer')
assert state is not None assert state is not None
@ -589,7 +553,6 @@ async def test_discovery_broken(hass, mqtt_mock, caplog):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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('sensor.milk') state = hass.states.get('sensor.milk')
assert state is not None assert state is not None
@ -624,7 +587,6 @@ async def test_entity_device_info_with_identifier(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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
@ -664,7 +626,6 @@ async def test_entity_device_info_update(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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
@ -675,7 +636,6 @@ async def test_entity_device_info_update(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', async_fire_mqtt_message(hass, 'homeassistant/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
@ -705,7 +665,6 @@ async def test_entity_id_update(hass, mqtt_mock):
registry.async_update_entity('sensor.beer', new_entity_id='sensor.milk') registry.async_update_entity('sensor.beer', new_entity_id='sensor.milk')
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done()
state = hass.states.get('sensor.beer') state = hass.states.get('sensor.beer')
assert state is None assert state is None
@ -743,7 +702,6 @@ async def test_entity_device_info_with_hub(hass, mqtt_mock):
}) })
async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', data) async_fire_mqtt_message(hass, 'homeassistant/sensor/bla/config', 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