Add config flow to HLK-SW16 (#37190)
* Add config flow to HLK-SW16
* Use entry_id for unique_id
* Add options update capability
* Refactor entry_id under domain
* Remove name from config
* Set options
* Remove options flow
* remove unneccesary else block from validate_input and move domain cleanup to async_unload_entry
* Add tests and config import
* Add back config schema
* Remove config import
* Refactor unload
* Add back config import
* Update coveragerc
* Don't mock validate_input
* Test duplicate configs
* Add import test
* Use patch for timeout test
* Use mock for testing timeout
* Use MockSW16Client for tests
* Check mock_calls count
* Remove unused NameExists exception
* Remove title from strings.json
* Mock setup for import test
* Set PARALLEL_UPDATES for switch
* Move hass.data.setdefault(DOMAIN, {}) to async_setup_entry
This commit is contained in:
1 parent
76b46b9175
commit
064cc52ad6
14 files changed
+482
-90
No files matched your search
@@ -0,0 +1 @@
|
||||
"""Tests for the Hi-Link HLK-SW16 integration."""
|
||||
@@ -0,0 +1,193 @@
|
||||
"""Test the Hi-Link HLK-SW16 config flow."""
|
||||
import asyncio
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.hlk_sw16.const import DOMAIN
|
||||
|
||||
from tests.async_mock import patch
|
||||
|
||||
|
||||
class MockSW16Client:
|
||||
"""Class to mock the SW16Client client."""
|
||||
|
||||
def __init__(self, fail):
|
||||
"""Initialise client with failure modes."""
|
||||
self.fail = fail
|
||||
self.disconnect_callback = None
|
||||
self.in_transaction = False
|
||||
self.active_transaction = None
|
||||
|
||||
async def setup(self):
|
||||
"""Mock successful setup."""
|
||||
fut = asyncio.Future()
|
||||
fut.set_result(True)
|
||||
return fut
|
||||
|
||||
async def status(self):
|
||||
"""Mock status based on failure mode."""
|
||||
self.in_transaction = True
|
||||
self.active_transaction = asyncio.Future()
|
||||
if self.fail:
|
||||
if self.disconnect_callback:
|
||||
self.disconnect_callback()
|
||||
return await self.active_transaction
|
||||
else:
|
||||
self.active_transaction.set_result(True)
|
||||
return self.active_transaction
|
||||
|
||||
def stop(self):
|
||||
"""Mock client stop."""
|
||||
self.in_transaction = False
|
||||
self.active_transaction = None
|
||||
|
||||
|
||||
async def create_mock_hlk_sw16_connection(fail):
|
||||
"""Create a mock HLK-SW16 client."""
|
||||
client = MockSW16Client(fail)
|
||||
await client.setup()
|
||||
return client
|
||||
|
||||
|
||||
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"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
conf = {
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
|
||||
mock_hlk_sw16_connection = await create_mock_hlk_sw16_connection(False)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.hlk_sw16.config_flow.create_hlk_sw16_connection",
|
||||
return_value=mock_hlk_sw16_connection,
|
||||
), patch(
|
||||
"homeassistant.components.hlk_sw16.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.hlk_sw16.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], conf,
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "127.0.0.1:8080"
|
||||
assert result2["data"] == {
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
mock_hlk_sw16_connection = await create_mock_hlk_sw16_connection(False)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.hlk_sw16.config_flow.create_hlk_sw16_connection",
|
||||
return_value=mock_hlk_sw16_connection,
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result3["type"] == "form"
|
||||
assert result3["errors"] == {}
|
||||
|
||||
result4 = await hass.config_entries.flow.async_configure(result3["flow_id"], conf,)
|
||||
|
||||
assert result4["type"] == "form"
|
||||
assert result4["errors"] == {"base": "already_configured"}
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_import(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_IMPORT}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
conf = {
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
|
||||
mock_hlk_sw16_connection = await create_mock_hlk_sw16_connection(False)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.hlk_sw16.config_flow.connect_client",
|
||||
return_value=mock_hlk_sw16_connection,
|
||||
), patch(
|
||||
"homeassistant.components.hlk_sw16.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.hlk_sw16.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], conf,
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "127.0.0.1:8080"
|
||||
assert result2["data"] == {
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_data(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
mock_hlk_sw16_connection = await create_mock_hlk_sw16_connection(True)
|
||||
|
||||
conf = {
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.hlk_sw16.config_flow.connect_client",
|
||||
return_value=mock_hlk_sw16_connection,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], conf,
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
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}
|
||||
)
|
||||
|
||||
conf = {
|
||||
"host": "127.0.0.1",
|
||||
"port": 8080,
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.hlk_sw16.config_flow.connect_client",
|
||||
side_effect=asyncio.TimeoutError,
|
||||
return_value=None,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], conf,
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
Reference in new issue
Block a user