Skip to content

Commit

Permalink
Add test (#37890)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Jul 24, 2020
1 parent c76b11f commit 686e6b8
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/components/mqtt/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ async def test_option_flow(hass, mqtt_mock, mock_try_connection):
result["flow_id"],
user_input={
mqtt.CONF_DISCOVERY: True,
"birth_enable": True,
"birth_topic": "ha_state/online",
"birth_payload": "online",
"birth_qos": 1,
"birth_retain": True,
"will_enable": True,
"will_topic": "ha_state/offline",
"will_payload": "offline",
"will_qos": 2,
Expand Down Expand Up @@ -221,6 +223,69 @@ async def test_option_flow(hass, mqtt_mock, mock_try_connection):
assert mqtt_mock.async_connect.call_count == 1


async def test_disable_birth_will(hass, mqtt_mock, mock_try_connection):
"""Test disabling birth and will."""
mock_try_connection.return_value = True
config_entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
await async_start(hass, "homeassistant", config_entry)
config_entry.data = {
mqtt.CONF_BROKER: "test-broker",
mqtt.CONF_PORT: 1234,
}

mqtt_mock.async_connect.reset_mock()

result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "broker"

result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
mqtt.CONF_BROKER: "another-broker",
mqtt.CONF_PORT: 2345,
mqtt.CONF_USERNAME: "user",
mqtt.CONF_PASSWORD: "pass",
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "options"

await hass.async_block_till_done()
assert mqtt_mock.async_connect.call_count == 0

result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
mqtt.CONF_DISCOVERY: True,
"birth_enable": False,
"birth_topic": "ha_state/online",
"birth_payload": "online",
"birth_qos": 1,
"birth_retain": True,
"will_enable": False,
"will_topic": "ha_state/offline",
"will_payload": "offline",
"will_qos": 2,
"will_retain": True,
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"] is None
assert config_entry.data == {
mqtt.CONF_BROKER: "another-broker",
mqtt.CONF_PORT: 2345,
mqtt.CONF_USERNAME: "user",
mqtt.CONF_PASSWORD: "pass",
mqtt.CONF_DISCOVERY: True,
mqtt.CONF_BIRTH_MESSAGE: {},
mqtt.CONF_WILL_MESSAGE: {},
}

await hass.async_block_till_done()
assert mqtt_mock.async_connect.call_count == 1


def get_default(schema, key):
"""Get default value for key in voluptuous schema."""
for k in schema.keys():
Expand Down

0 comments on commit 686e6b8

Please sign in to comment.