Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions tests/components/melnor/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for the melnor integration."""
from __future__ import annotations

from collections.abc import Generator
from unittest.mock import AsyncMock, patch

from bleak.backends.device import BLEDevice
Expand Down Expand Up @@ -141,12 +142,13 @@ def mock_melnor_device():
return device


def patch_async_setup_entry(return_value=True):
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
"""Patch async setup entry to return True."""
return patch(
"homeassistant.components.melnor.async_setup_entry",
return_value=return_value,
)
with patch(
"homeassistant.components.melnor.async_setup_entry", return_value=True
) as mock_setup:
yield mock_setup


# pylint: disable=dangerous-default-value
Expand Down
99 changes: 53 additions & 46 deletions tests/components/melnor/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test the melnor config flow."""
from unittest.mock import AsyncMock

import pytest
import voluptuous as vol

Expand All @@ -13,15 +15,14 @@
FAKE_SERVICE_INFO_1,
FAKE_SERVICE_INFO_2,
patch_async_discovered_service_info,
patch_async_setup_entry,
)


async def test_user_step_no_devices(hass: HomeAssistant) -> None:
async def test_user_step_no_devices(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we handle no devices found."""
with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info(
[]
):
with patch_async_discovered_service_info([]):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
Expand All @@ -30,13 +31,15 @@ async def test_user_step_no_devices(hass: HomeAssistant) -> None:
assert result["type"] == FlowResultType.ABORT
assert result["reason"] == "no_devices_found"

assert len(mock_setup_entry.mock_calls) == 0
mock_setup_entry.assert_not_called()


async def test_user_step_discovered_devices(hass: HomeAssistant) -> None:
async def test_user_step_discovered_devices(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we properly handle device picking."""

with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info():
with patch_async_discovered_service_info():
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_USER},
Expand All @@ -57,13 +60,15 @@ async def test_user_step_discovered_devices(hass: HomeAssistant) -> None:
assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1}

assert len(mock_setup_entry.mock_calls) == 1
mock_setup_entry.assert_called_once()


async def test_user_step_with_existing_device(hass: HomeAssistant) -> None:
async def test_user_step_with_existing_device(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we properly handle device picking."""

with patch_async_setup_entry() as mock_setup_entry, patch_async_discovered_service_info(
with patch_async_discovered_service_info(
[FAKE_SERVICE_INFO_1, FAKE_SERVICE_INFO_2]
):
# Create the config flow
Expand Down Expand Up @@ -95,48 +100,50 @@ async def test_user_step_with_existing_device(hass: HomeAssistant) -> None:
result["flow_id"], user_input={CONF_ADDRESS: FAKE_ADDRESS_1}
)

assert len(mock_setup_entry.mock_calls) == 0
mock_setup_entry.assert_not_called()


async def test_bluetooth_discovered(hass: HomeAssistant) -> None:
async def test_bluetooth_discovered(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we short circuit to config entry creation."""

with patch_async_setup_entry() as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_BLUETOOTH},
data=FAKE_SERVICE_INFO_1,
)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_BLUETOOTH},
data=FAKE_SERVICE_INFO_1,
)

assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "bluetooth_confirm"
assert result["description_placeholders"] == {"name": FAKE_ADDRESS_1}
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "bluetooth_confirm"
assert result["description_placeholders"] == {"name": FAKE_ADDRESS_1}

assert len(mock_setup_entry.mock_calls) == 0
mock_setup_entry.assert_not_called()


async def test_bluetooth_confirm(hass: HomeAssistant) -> None:
async def test_bluetooth_confirm(
hass: HomeAssistant, mock_setup_entry: AsyncMock
) -> None:
"""Test we short circuit to config entry creation."""

with patch_async_setup_entry() as mock_setup_entry:
# Create the config flow
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_BLUETOOTH,
"step_id": "bluetooth_confirm",
"user_input": {CONF_MAC: FAKE_ADDRESS_1},
},
data=FAKE_SERVICE_INFO_1,
)

# Interact with it like a user would
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)

assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == FAKE_ADDRESS_1
assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1}

assert len(mock_setup_entry.mock_calls) == 1
# Create the config flow
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={
"source": config_entries.SOURCE_BLUETOOTH,
"step_id": "bluetooth_confirm",
"user_input": {CONF_MAC: FAKE_ADDRESS_1},
},
data=FAKE_SERVICE_INFO_1,
)

# Interact with it like a user would
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)

assert result2["type"] == FlowResultType.CREATE_ENTRY
assert result2["title"] == FAKE_ADDRESS_1
assert result2["data"] == {CONF_ADDRESS: FAKE_ADDRESS_1}

mock_setup_entry.assert_called_once()