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

74 lines
2.3 KiB
Python
Raw Normal View History

Add Config Flow to Obihai (#88627) * Obihai: Config Flow Only * Remove reboot service * Update .coveragerc Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * PR Feedback * Use Issue Registry * Add config_flow tests * Another pass with pre-commit * Resolve cyclical import and move sensorClasses to sensor * Update homeassistant/components/obihai/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/obihai/sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/obihai/sensor.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/obihai/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Another round of feedback * More PR feedback * Offline testing, already_configured is required * Update homeassistant/components/obihai/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/obihai/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/obihai/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Cleanup * Update homeassistant/components/obihai/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * PR feedback * Backout today's changes: Fix mypy error * Update tests/components/obihai/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/obihai/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/obihai/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/obihai/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Don't plan ahead * PR feedback * Update homeassistant/components/obihai/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Cleanup strings --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
2023-02-27 08:22:15 -08:00
"""Test the Obihai config flow."""
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant import config_entries
from homeassistant.components.obihai.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from . import USER_INPUT
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
async def test_user_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test we get the user initiated form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
with patch("pyobihai.PyObihai.check_account"):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert result["title"] == "10.10.10.30"
assert result["data"] == {**USER_INPUT}
assert len(mock_setup_entry.mock_calls) == 1
async def test_auth_failure(hass: HomeAssistant) -> None:
"""Test we get the authentication error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.obihai.config_flow.validate_auth", return_value=False
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"]["base"] == "cannot_connect"
async def test_yaml_import(hass: HomeAssistant) -> None:
"""Test we get the YAML imported."""
with patch(
"homeassistant.components.obihai.config_flow.validate_auth", return_value=True
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data=USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] == FlowResultType.CREATE_ENTRY
assert "errors" not in result