hass-core/tests/components/dexcom/test_config_flow.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

152 lines
4.4 KiB
Python
Raw Normal View History

2020-07-01 20:14:54 -04:00
"""Test the Dexcom config flow."""
2021-01-01 22:31:56 +01:00
from unittest.mock import patch
2020-07-01 20:14:54 -04:00
from pydexcom import AccountError, SessionError
from homeassistant import config_entries
2020-07-01 20:14:54 -04:00
from homeassistant.components.dexcom.const import DOMAIN, MG_DL, MMOL_L
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
2020-07-01 20:14:54 -04:00
from . import CONFIG
2020-07-01 20:14:54 -04:00
from tests.common import MockConfigEntry
async def test_form(hass: HomeAssistant) -> None:
2020-07-01 20:14:54 -04:00
"""Test we get the form."""
2020-07-01 20:14:54 -04:00
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
2020-07-01 20:14:54 -04:00
assert result["errors"] == {}
with (
patch(
"homeassistant.components.dexcom.config_flow.Dexcom.create_session",
return_value="test_session_id",
),
patch(
2020-08-27 13:56:20 +02:00
"homeassistant.components.dexcom.async_setup_entry",
return_value=True,
2020-07-01 20:14:54 -04:00
) as mock_setup_entry,
):
result2 = await hass.config_entries.flow.async_configure(
2020-08-27 13:56:20 +02:00
result["flow_id"],
CONFIG,
2020-07-01 20:14:54 -04:00
)
await hass.async_block_till_done()
2020-07-01 20:14:54 -04:00
assert result2["type"] is FlowResultType.CREATE_ENTRY
2020-07-01 20:14:54 -04:00
assert result2["title"] == CONFIG[CONF_USERNAME]
assert result2["data"] == CONFIG
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_account_error(hass: HomeAssistant) -> None:
2020-07-01 20:14:54 -04:00
"""Test we handle account error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
2020-08-27 13:56:20 +02:00
"homeassistant.components.dexcom.config_flow.Dexcom",
side_effect=AccountError,
2020-07-01 20:14:54 -04:00
):
result2 = await hass.config_entries.flow.async_configure(
2020-08-27 13:56:20 +02:00
result["flow_id"],
CONFIG,
2020-07-01 20:14:54 -04:00
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "invalid_auth"}
2020-07-01 20:14:54 -04:00
async def test_form_session_error(hass: HomeAssistant) -> None:
2020-07-01 20:14:54 -04:00
"""Test we handle session error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
2020-08-27 13:56:20 +02:00
"homeassistant.components.dexcom.config_flow.Dexcom",
side_effect=SessionError,
2020-07-01 20:14:54 -04:00
):
result2 = await hass.config_entries.flow.async_configure(
2020-08-27 13:56:20 +02:00
result["flow_id"],
CONFIG,
2020-07-01 20:14:54 -04:00
)
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": "cannot_connect"}
2020-07-01 20:14:54 -04:00
async def test_form_unknown_error(hass: HomeAssistant) -> None:
2020-07-01 20:14:54 -04:00
"""Test we handle unknown error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
2020-08-27 13:56:20 +02:00
"homeassistant.components.dexcom.config_flow.Dexcom",
side_effect=Exception,
2020-07-01 20:14:54 -04:00
):
result2 = await hass.config_entries.flow.async_configure(
2020-08-27 13:56:20 +02:00
result["flow_id"],
CONFIG,
2020-07-01 20:14:54 -04:00
)
assert result2["type"] is FlowResultType.FORM
2020-07-01 20:14:54 -04:00
assert result2["errors"] == {"base": "unknown"}
async def test_option_flow_default(hass: HomeAssistant) -> None:
2020-07-01 20:14:54 -04:00
"""Test config flow options."""
2020-08-27 13:56:20 +02:00
entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG,
options=None,
)
2020-07-01 20:14:54 -04:00
entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] is FlowResultType.FORM
2020-07-01 20:14:54 -04:00
assert result["step_id"] == "init"
result2 = await hass.config_entries.options.async_configure(
2020-08-27 13:56:20 +02:00
result["flow_id"],
user_input={},
2020-07-01 20:14:54 -04:00
)
assert result2["type"] is FlowResultType.CREATE_ENTRY
2020-07-01 20:14:54 -04:00
assert result2["data"] == {
CONF_UNIT_OF_MEASUREMENT: MG_DL,
}
async def test_option_flow(hass: HomeAssistant) -> None:
2020-07-01 20:14:54 -04:00
"""Test config flow options."""
entry = MockConfigEntry(
2020-08-27 13:56:20 +02:00
domain=DOMAIN,
data=CONFIG,
options={CONF_UNIT_OF_MEASUREMENT: MG_DL},
2020-07-01 20:14:54 -04:00
)
entry.add_to_hass(hass)
result = await hass.config_entries.options.async_init(entry.entry_id)
assert result["type"] is FlowResultType.FORM
2020-07-01 20:14:54 -04:00
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
2020-08-27 13:56:20 +02:00
result["flow_id"],
user_input={CONF_UNIT_OF_MEASUREMENT: MMOL_L},
2020-07-01 20:14:54 -04:00
)
assert result["type"] is FlowResultType.CREATE_ENTRY
2020-07-01 20:14:54 -04:00
assert result["data"] == {
CONF_UNIT_OF_MEASUREMENT: MMOL_L,
}