Add unique ID to config entry in Luftdaten (#62176)
This commit is contained in:
parent
b559d8845e
commit
7fe895e554
4 changed files with 122 additions and 97 deletions
|
@ -1,84 +1,114 @@
|
|||
"""Define tests for the Luftdaten config flow."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.luftdaten import DOMAIN, config_flow
|
||||
from luftdaten.exceptions import LuftdatenConnectionError
|
||||
|
||||
from homeassistant.components.luftdaten import DOMAIN
|
||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
RESULT_TYPE_FORM,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_duplicate_error(hass):
|
||||
async def test_duplicate_error(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that errors are shown when duplicates are added."""
|
||||
conf = {CONF_SENSOR_ID: "12345abcde"}
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass)
|
||||
flow = config_flow.LuftDatenFlowHandler()
|
||||
flow.hass = hass
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
result = await flow.async_step_user(user_input=conf)
|
||||
assert result["errors"] == {CONF_SENSOR_ID: "already_configured"}
|
||||
assert result.get("type") == RESULT_TYPE_FORM
|
||||
assert result.get("step_id") == SOURCE_USER
|
||||
assert "flow_id" in result
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result2.get("type") == RESULT_TYPE_ABORT
|
||||
assert result2.get("reason") == "already_configured"
|
||||
|
||||
|
||||
async def test_communication_error(hass):
|
||||
async def test_communication_error(hass: HomeAssistant) -> None:
|
||||
"""Test that no sensor is added while unable to communicate with API."""
|
||||
conf = {CONF_SENSOR_ID: "12345abcde"}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
flow = config_flow.LuftDatenFlowHandler()
|
||||
flow.hass = hass
|
||||
assert result.get("type") == RESULT_TYPE_FORM
|
||||
assert result.get("step_id") == SOURCE_USER
|
||||
assert "flow_id" in result
|
||||
|
||||
with patch("luftdaten.Luftdaten.get_data", return_value=None):
|
||||
result = await flow.async_step_user(user_input=conf)
|
||||
assert result["errors"] == {CONF_SENSOR_ID: "invalid_sensor"}
|
||||
with patch("luftdaten.Luftdaten.get_data", side_effect=LuftdatenConnectionError):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result2.get("type") == RESULT_TYPE_FORM
|
||||
assert result2.get("step_id") == SOURCE_USER
|
||||
assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"}
|
||||
|
||||
|
||||
async def test_invalid_sensor(hass):
|
||||
async def test_invalid_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test that an invalid sensor throws an error."""
|
||||
conf = {CONF_SENSOR_ID: "12345abcde"}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
flow = config_flow.LuftDatenFlowHandler()
|
||||
flow.hass = hass
|
||||
assert result.get("type") == RESULT_TYPE_FORM
|
||||
assert result.get("step_id") == SOURCE_USER
|
||||
assert "flow_id" in result
|
||||
|
||||
with patch("luftdaten.Luftdaten.get_data", return_value=False), patch(
|
||||
"luftdaten.Luftdaten.validate_sensor", return_value=False
|
||||
):
|
||||
result = await flow.async_step_user(user_input=conf)
|
||||
assert result["errors"] == {CONF_SENSOR_ID: "invalid_sensor"}
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result2.get("type") == RESULT_TYPE_FORM
|
||||
assert result2.get("step_id") == SOURCE_USER
|
||||
assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"}
|
||||
|
||||
|
||||
async def test_show_form(hass):
|
||||
"""Test that the form is served with no input."""
|
||||
flow = config_flow.LuftDatenFlowHandler()
|
||||
flow.hass = hass
|
||||
|
||||
result = await flow.async_step_user(user_input=None)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_step_user(hass):
|
||||
async def test_step_user(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None:
|
||||
"""Test that the user step works."""
|
||||
conf = {
|
||||
CONF_SENSOR_ID: "12345abcde",
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
CONF_SCAN_INTERVAL: timedelta(minutes=5),
|
||||
}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
|
||||
flow = config_flow.LuftDatenFlowHandler()
|
||||
flow.hass = hass
|
||||
assert result.get("type") == RESULT_TYPE_FORM
|
||||
assert result.get("step_id") == SOURCE_USER
|
||||
assert "flow_id" in result
|
||||
|
||||
with patch("luftdaten.Luftdaten.get_data", return_value=True), patch(
|
||||
"luftdaten.Luftdaten.validate_sensor", return_value=True
|
||||
):
|
||||
result = await flow.async_step_user(user_input=conf)
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
assert result["title"] == "12345abcde"
|
||||
assert result["data"] == {
|
||||
CONF_SENSOR_ID: "12345abcde",
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
CONF_SCAN_INTERVAL: 300,
|
||||
}
|
||||
assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result2.get("title") == "12345"
|
||||
assert result2.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
CONF_SCAN_INTERVAL: 600.0,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue