init home-assistant
This commit is contained in:
1
home-assistant/tests/components/mill/__init__.py
Normal file
1
home-assistant/tests/components/mill/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for Mill."""
|
||||
249
home-assistant/tests/components/mill/test_config_flow.py
Normal file
249
home-assistant/tests/components/mill/test_config_flow.py
Normal file
@@ -0,0 +1,249 @@
|
||||
"""Tests for Mill config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.mill.const import CLOUD, CONNECTION_TYPE, DOMAIN, LOCAL
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_show_config_form(hass: HomeAssistant) -> None:
|
||||
"""Test show configuration form."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_create_entry(hass: HomeAssistant) -> None:
|
||||
"""Test create entry from user input."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONNECTION_TYPE: CLOUD,
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
|
||||
with patch("mill.Mill.connect", return_value=True):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pswd",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "user"
|
||||
assert result["data"] == {
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pswd",
|
||||
CONNECTION_TYPE: CLOUD,
|
||||
}
|
||||
|
||||
|
||||
async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
|
||||
"""Test user input for config_entry that already exists."""
|
||||
|
||||
test_data = {
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pswd",
|
||||
}
|
||||
|
||||
first_entry = MockConfigEntry(
|
||||
domain="mill",
|
||||
data=test_data,
|
||||
unique_id=test_data[CONF_USERNAME],
|
||||
)
|
||||
first_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONNECTION_TYPE: CLOUD,
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
|
||||
with patch("mill.Mill.connect", return_value=True):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
test_data,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_connection_error(hass: HomeAssistant) -> None:
|
||||
"""Test connection error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONNECTION_TYPE: CLOUD,
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
|
||||
with patch("mill.Mill.connect", return_value=False):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
{
|
||||
CONF_USERNAME: "user",
|
||||
CONF_PASSWORD: "pswd",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_local_create_entry(hass: HomeAssistant) -> None:
|
||||
"""Test create entry from user input."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONNECTION_TYPE: LOCAL,
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
|
||||
test_data = {
|
||||
CONF_IP_ADDRESS: "192.168.1.59",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"mill_local.Mill.connect",
|
||||
return_value={
|
||||
"name": "panel heater gen. 3",
|
||||
"version": "0x210927",
|
||||
"operation_key": "",
|
||||
"status": "ok",
|
||||
},
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
test_data,
|
||||
)
|
||||
|
||||
test_data[CONNECTION_TYPE] = LOCAL
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == test_data[CONF_IP_ADDRESS]
|
||||
assert result["data"] == test_data
|
||||
|
||||
|
||||
async def test_local_flow_entry_already_exists(hass: HomeAssistant) -> None:
|
||||
"""Test user input for config_entry that already exists."""
|
||||
|
||||
test_data = {
|
||||
CONF_IP_ADDRESS: "192.168.1.59",
|
||||
}
|
||||
|
||||
first_entry = MockConfigEntry(
|
||||
domain="mill",
|
||||
data=test_data,
|
||||
unique_id=test_data[CONF_IP_ADDRESS],
|
||||
)
|
||||
first_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONNECTION_TYPE: LOCAL,
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
|
||||
test_data = {
|
||||
CONF_IP_ADDRESS: "192.168.1.59",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"mill_local.Mill.connect",
|
||||
return_value={
|
||||
"name": "panel heater gen. 3",
|
||||
"version": "0x210927",
|
||||
"operation_key": "",
|
||||
"status": "ok",
|
||||
},
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
test_data,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_local_connection_error(hass: HomeAssistant) -> None:
|
||||
"""Test connection error."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONNECTION_TYPE: LOCAL,
|
||||
},
|
||||
)
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
|
||||
test_data = {
|
||||
CONF_IP_ADDRESS: "192.168.1.59",
|
||||
}
|
||||
|
||||
with patch(
|
||||
"mill_local.Mill.connect",
|
||||
return_value=None,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
test_data,
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
151
home-assistant/tests/components/mill/test_init.py
Normal file
151
home-assistant/tests/components/mill/test_init.py
Normal file
@@ -0,0 +1,151 @@
|
||||
"""Tests for Mill init."""
|
||||
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components import mill
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup_with_cloud_config(hass: HomeAssistant) -> None:
|
||||
"""Test setup of cloud config."""
|
||||
entry = MockConfigEntry(
|
||||
domain=mill.DOMAIN,
|
||||
data={
|
||||
mill.CONF_USERNAME: "user",
|
||||
mill.CONF_PASSWORD: "pswd",
|
||||
mill.CONNECTION_TYPE: mill.CLOUD,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with (
|
||||
patch("mill.Mill.fetch_heater_and_sensor_data", return_value={}) as mock_fetch,
|
||||
patch("mill.Mill.connect", return_value=True) as mock_connect,
|
||||
):
|
||||
assert await async_setup_component(hass, "mill", {})
|
||||
assert len(mock_fetch.mock_calls) == 1
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_with_cloud_config_fails(hass: HomeAssistant) -> None:
|
||||
"""Test setup of cloud config."""
|
||||
entry = MockConfigEntry(
|
||||
domain=mill.DOMAIN,
|
||||
data={
|
||||
mill.CONF_USERNAME: "user",
|
||||
mill.CONF_PASSWORD: "pswd",
|
||||
mill.CONNECTION_TYPE: mill.CLOUD,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with patch("mill.Mill.connect", return_value=False):
|
||||
assert await async_setup_component(hass, "mill", {})
|
||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_setup_with_cloud_config_times_out(hass: HomeAssistant) -> None:
|
||||
"""Test setup of cloud config will retry if timed out."""
|
||||
entry = MockConfigEntry(
|
||||
domain=mill.DOMAIN,
|
||||
data={
|
||||
mill.CONF_USERNAME: "user",
|
||||
mill.CONF_PASSWORD: "pswd",
|
||||
mill.CONNECTION_TYPE: mill.CLOUD,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with patch("mill.Mill.connect", side_effect=asyncio.TimeoutError):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
async def test_setup_with_old_cloud_config(hass: HomeAssistant) -> None:
|
||||
"""Test setup of old cloud config."""
|
||||
entry = MockConfigEntry(
|
||||
domain=mill.DOMAIN,
|
||||
data={
|
||||
mill.CONF_USERNAME: "user",
|
||||
mill.CONF_PASSWORD: "pswd",
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with (
|
||||
patch("mill.Mill.fetch_heater_and_sensor_data", return_value={}),
|
||||
patch("mill.Mill.connect", return_value=True) as mock_connect,
|
||||
):
|
||||
assert await async_setup_component(hass, "mill", {})
|
||||
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_setup_with_local_config(hass: HomeAssistant) -> None:
|
||||
"""Test setup of local config."""
|
||||
entry = MockConfigEntry(
|
||||
domain=mill.DOMAIN,
|
||||
data={
|
||||
mill.CONF_IP_ADDRESS: "192.168.1.59",
|
||||
mill.CONNECTION_TYPE: mill.LOCAL,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with (
|
||||
patch(
|
||||
"mill_local.Mill.fetch_heater_and_sensor_data",
|
||||
return_value={
|
||||
"ambient_temperature": 20,
|
||||
"set_temperature": 22,
|
||||
"current_power": 0,
|
||||
"control_signal": 0,
|
||||
"raw_ambient_temperature": 19,
|
||||
},
|
||||
) as mock_fetch,
|
||||
patch(
|
||||
"mill_local.Mill.connect",
|
||||
return_value={
|
||||
"name": "panel heater gen. 3",
|
||||
"version": "0x210927",
|
||||
"operation_key": "",
|
||||
"status": "ok",
|
||||
},
|
||||
) as mock_connect,
|
||||
):
|
||||
assert await async_setup_component(hass, "mill", {})
|
||||
|
||||
assert len(mock_fetch.mock_calls) == 1
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_unload_entry(hass: HomeAssistant) -> None:
|
||||
"""Test removing mill client."""
|
||||
entry = MockConfigEntry(
|
||||
domain=mill.DOMAIN,
|
||||
data={
|
||||
mill.CONF_USERNAME: "user",
|
||||
mill.CONF_PASSWORD: "pswd",
|
||||
mill.CONNECTION_TYPE: mill.CLOUD,
|
||||
},
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
hass.config_entries,
|
||||
"async_forward_entry_unload",
|
||||
return_value=True,
|
||||
) as unload_entry,
|
||||
patch("mill.Mill.fetch_heater_and_sensor_data", return_value={}),
|
||||
patch(
|
||||
"mill.Mill.connect",
|
||||
return_value=True,
|
||||
),
|
||||
):
|
||||
assert await async_setup_component(hass, "mill", {})
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
|
||||
assert unload_entry.call_count == 2
|
||||
assert entry.entry_id not in hass.data[mill.DOMAIN]
|
||||
Reference in New Issue
Block a user