2020-11-07 17:15:29 +00:00
|
|
|
"""Test the epson config flow."""
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
2021-05-07 21:05:59 +00:00
|
|
|
from epson_projector.const import PWR_OFF_STATE
|
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries
|
2020-11-07 17:15:29 +00:00
|
|
|
from homeassistant.components.epson.const import DOMAIN
|
2021-05-07 21:05:59 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME, STATE_UNAVAILABLE
|
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
|
|
|
|
async def test_form(hass):
|
|
|
|
"""Test we get the form."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2021-05-07 21:05:59 +00:00
|
|
|
with patch("homeassistant.components.epson.Projector.get_power", return_value="01"):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2020-11-07 17:15:29 +00:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
assert result["step_id"] == config_entries.SOURCE_USER
|
|
|
|
with patch(
|
2021-05-07 21:05:59 +00:00
|
|
|
"homeassistant.components.epson.Projector.get_power",
|
|
|
|
return_value="01",
|
2021-09-02 17:30:53 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.epson.Projector.get_serial_number",
|
|
|
|
return_value="12345",
|
2020-11-07 17:15:29 +00:00
|
|
|
), patch(
|
|
|
|
"homeassistant.components.epson.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
2021-05-07 21:05:59 +00:00
|
|
|
{CONF_HOST: "1.1.1.1", CONF_NAME: "test-epson"},
|
2020-11-07 17:15:29 +00:00
|
|
|
)
|
2021-05-07 21:05:59 +00:00
|
|
|
|
2020-11-07 17:15:29 +00:00
|
|
|
assert result2["type"] == "create_entry"
|
|
|
|
assert result2["title"] == "test-epson"
|
2021-05-07 21:05:59 +00:00
|
|
|
assert result2["data"] == {CONF_HOST: "1.1.1.1"}
|
2020-11-07 17:15:29 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_form_cannot_connect(hass):
|
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
2021-05-07 21:05:59 +00:00
|
|
|
"homeassistant.components.epson.Projector.get_power",
|
2020-11-07 17:15:29 +00:00
|
|
|
return_value=STATE_UNAVAILABLE,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
2021-05-07 21:05:59 +00:00
|
|
|
{CONF_HOST: "1.1.1.1", CONF_NAME: "test-epson"},
|
2020-11-07 17:15:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "cannot_connect"}
|
|
|
|
|
|
|
|
|
2021-05-07 21:05:59 +00:00
|
|
|
async def test_form_powered_off(hass):
|
|
|
|
"""Test we handle powered off during initial configuration."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.epson.Projector.get_power",
|
|
|
|
return_value=PWR_OFF_STATE,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{CONF_HOST: "1.1.1.1", CONF_NAME: "test-epson"},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result2["type"] == "form"
|
|
|
|
assert result2["errors"] == {"base": "powered_off"}
|