Add integration for ProgettiHWSW automation boards (#37922)

* Opened a new fresh page to clean my mess.

* Solved pylint warnings

* Fixing pylint issue of defining attr outside init.

* Excluded files from being tested by codecov.

* Solved binary sensor error.

* Fixed some stylisation errors.

* Resolved input not updating problem.

* Added port entry to test file.

* Added tests for create_entry.

* Added support for better state management.

* Increased code coverage of config_flow.py & made some tweaks.

* Increased coverage of config_flow.py by adding tests for unknown exceptions.

* A small bugfix.

* Stylised code as per Chris' suggestions.

* Stylised code again.

* Improved quality of test code.

* Added step_id in config flow tests.
This commit is contained in:
Arda ŞEREMET authored and GitHub committed 2020-08-30 15:03:33 -05:00
1 parent 4bbc737954
commit e707b50658
15 files changed
+647

No files matched your search

@@ -0,0 +1 @@
"""Tests for the ProgettiHWSW Automation integration."""
@@ -0,0 +1,168 @@
"""Test the ProgettiHWSW Automation config flow."""
from homeassistant import config_entries, setup
from homeassistant.components.progettihwsw.const import DOMAIN
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
from tests.async_mock import patch
mock_value_step_user = {
"title": "1R & 1IN Board",
"relay_count": 1,
"input_count": 1,
"is_old": False,
}
async def test_form(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
mock_value_step_rm = {
"relay_1": "bistable", # Mocking a single relay board instance.
}
with patch(
"homeassistant.components.progettihwsw.config_flow.ProgettiHWSWAPI.check_board",
return_value=mock_value_step_user,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "", CONF_PORT: 80},
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["step_id"] == "relay_modes"
assert result2["errors"] == {}
with patch(
"homeassistant.components.progettihwsw.async_setup",
return_value=True,
), patch(
"homeassistant.components.progettihwsw.async_setup_entry",
return_value=True,
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
mock_value_step_rm,
)
assert result3["type"] == RESULT_TYPE_CREATE_ENTRY
assert result3["data"]
assert result3["data"]["title"] == "1R & 1IN Board"
assert result3["data"]["is_old"] is False
assert result3["data"]["relay_count"] == result3["data"]["input_count"] == 1
async def test_form_wrong_info(hass):
"""Test we handle wrong info exception."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
with patch(
"homeassistant.components.progettihwsw.config_flow.ProgettiHWSWAPI.check_board",
return_value=mock_value_step_user,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {CONF_HOST: "", CONF_PORT: 80}
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["step_id"] == "relay_modes"
assert result2["errors"] == {}
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"], {"relay_1": ""}
)
assert result3["type"] == RESULT_TYPE_FORM
assert result3["step_id"] == "relay_modes"
assert result3["errors"] == {"base": "wrong_info_relay_modes"}
async def test_form_cannot_connect(hass):
"""Test we handle unexisting board."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
with patch(
"homeassistant.components.progettihwsw.config_flow.ProgettiHWSWAPI.check_board",
return_value=False,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "", CONF_PORT: 80},
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["step_id"] == "user"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_user_exception(hass):
"""Test we handle unknown exception."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
with patch(
"homeassistant.components.progettihwsw.config_flow.validate_input",
side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "", CONF_PORT: 80},
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["step_id"] == "user"
assert result2["errors"] == {"base": "unknown"}
async def test_form_rm_exception(hass):
"""Test we handle unknown exception on seconds step."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
with patch(
"homeassistant.components.progettihwsw.config_flow.ProgettiHWSWAPI.check_board",
return_value=mock_value_step_user,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "", CONF_PORT: 80},
)
assert result2["type"] == RESULT_TYPE_FORM
assert result2["step_id"] == "relay_modes"
assert result2["errors"] == {}
with patch(
"homeassistant.components.progettihwsw.config_flow.validate_input_relay_modes",
side_effect=Exception,
):
result3 = await hass.config_entries.flow.async_configure(
result2["flow_id"],
{"relay_1": "bistable"},
)
assert result3["type"] == RESULT_TYPE_FORM
assert result3["step_id"] == "relay_modes"
assert result3["errors"] == {"base": "unknown"}