1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Improve MQTT tests (#23296)

* Improve MQTT tests

* Tweak
This commit is contained in:
Erik Montnemery
2019-04-22 14:48:50 +02:00
committed by Charles Garwood
parent 357631d659
commit a89c7f8feb
13 changed files with 625 additions and 677 deletions

View File

@@ -38,18 +38,18 @@ async def test_controlling_state_via_topic(hass, mock_publish):
})
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, 'state-topic', '1')
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
async_fire_mqtt_message(hass, 'state-topic', '0')
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
@@ -71,7 +71,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
})
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
assert state.attributes.get(ATTR_ASSUMED_STATE)
common.turn_on(hass, 'switch.test')
@@ -81,7 +81,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
'command-topic', 'beer on', 2, False)
mock_publish.async_publish.reset_mock()
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
common.turn_off(hass, 'switch.test')
await hass.async_block_till_done()
@@ -90,7 +90,7 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
mock_publish.async_publish.assert_called_once_with(
'command-topic', 'beer off', 2, False)
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
async def test_controlling_state_via_topic_and_json_message(
@@ -109,17 +109,17 @@ async def test_controlling_state_via_topic_and_json_message(
})
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
async_fire_mqtt_message(hass, 'state-topic', '{"val":"beer on"}')
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
async_fire_mqtt_message(hass, 'state-topic', '{"val":"beer off"}')
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
async def test_default_availability_payload(hass, mock_publish):
@@ -137,28 +137,28 @@ async def test_default_availability_payload(hass, mock_publish):
})
state = hass.states.get('switch.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE
async_fire_mqtt_message(hass, 'availability_topic', 'online')
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, 'availability_topic', 'offline')
state = hass.states.get('switch.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE
async_fire_mqtt_message(hass, 'state-topic', '1')
state = hass.states.get('switch.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE
async_fire_mqtt_message(hass, 'availability_topic', 'online')
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
async def test_custom_availability_payload(hass, mock_publish):
@@ -178,28 +178,28 @@ async def test_custom_availability_payload(hass, mock_publish):
})
state = hass.states.get('switch.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE
async_fire_mqtt_message(hass, 'availability_topic', 'good')
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, 'availability_topic', 'nogood')
state = hass.states.get('switch.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE
async_fire_mqtt_message(hass, 'state-topic', '1')
state = hass.states.get('switch.test')
assert STATE_UNAVAILABLE == state.state
assert state.state == STATE_UNAVAILABLE
async_fire_mqtt_message(hass, 'availability_topic', 'good')
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
async def test_custom_state_payload(hass, mock_publish):
@@ -218,18 +218,18 @@ async def test_custom_state_payload(hass, mock_publish):
})
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
assert not state.attributes.get(ATTR_ASSUMED_STATE)
async_fire_mqtt_message(hass, 'state-topic', 'HIGH')
state = hass.states.get('switch.test')
assert STATE_ON == state.state
assert state.state == STATE_ON
async_fire_mqtt_message(hass, 'state-topic', 'LOW')
state = hass.states.get('switch.test')
assert STATE_OFF == state.state
assert state.state == STATE_OFF
async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
@@ -246,7 +246,7 @@ async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
async_fire_mqtt_message(hass, 'attr-topic', '{ "val": "100" }')
state = hass.states.get('switch.test')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'
async def test_update_with_json_attrs_not_dict(hass, mqtt_mock, caplog):
@@ -304,7 +304,7 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
await hass.async_block_till_done()
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "100" }')
state = hass.states.get('switch.beer')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'
# Change json_attributes_topic
async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config',
@@ -314,12 +314,12 @@ async def test_discovery_update_attr(hass, mqtt_mock, caplog):
# Verify we are no longer subscribing to the old topic
async_fire_mqtt_message(hass, 'attr-topic1', '{ "val": "50" }')
state = hass.states.get('switch.beer')
assert '100' == state.attributes.get('val')
assert state.attributes.get('val') == '100'
# Verify we are subscribing to the new topic
async_fire_mqtt_message(hass, 'attr-topic2', '{ "val": "75" }')
state = hass.states.get('switch.beer')
assert '75' == state.attributes.get('val')
assert state.attributes.get('val') == '75'
async def test_unique_id(hass):