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
11 changes: 0 additions & 11 deletions homeassistant/components/switchbot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import voluptuous as vol

from homeassistant.components.bluetooth import (
BluetoothScanningMode,
BluetoothServiceInfoBleak,
async_current_scanners,
async_discovered_service_info,
)
from homeassistant.config_entries import (
Expand Down Expand Up @@ -325,15 +323,6 @@ async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the user step to choose cloud login or direct discovery."""
Comment thread
zerzhang marked this conversation as resolved.
# Check if all scanners are in active mode
# If so, skip the menu and go directly to device selection
scanners = async_current_scanners(self.hass)
if scanners and all(
scanner.current_mode == BluetoothScanningMode.ACTIVE for scanner in scanners
):
# All scanners are active, skip the menu
return await self.async_step_select_device()

return self.async_show_menu(
step_id="user",
menu_options=["cloud_login", "select_device"],
Expand Down
50 changes: 3 additions & 47 deletions tests/components/switchbot/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,13 @@
DOMAIN = "switchbot"


@pytest.fixture
def mock_scanners_all_active() -> Generator[None]:
"""Mock all scanners as active mode."""
mock_scanner = Mock()
mock_scanner.current_mode = BluetoothScanningMode.ACTIVE
with patch(
"homeassistant.components.switchbot.config_flow.async_current_scanners",
return_value=[mock_scanner],
):
yield


@pytest.fixture
def mock_scanners_all_passive() -> Generator[None]:
"""Mock all scanners as passive mode."""
mock_scanner = Mock()
mock_scanner.current_mode = BluetoothScanningMode.PASSIVE
with patch(
"homeassistant.components.switchbot.config_flow.async_current_scanners",
"homeassistant.components.bluetooth.async_current_scanners",
return_value=[mock_scanner],
):
yield
Expand Down Expand Up @@ -1461,38 +1449,6 @@ async def test_user_setup_worelay_switch_1pm_auth_switchbot_api_down(
assert result["description_placeholders"] == {"error_detail": "Switchbot API down"}


@pytest.mark.usefixtures("mock_scanners_all_active")
async def test_user_skip_menu_when_all_scanners_active(hass: HomeAssistant) -> None:
"""Test that menu is skipped when all scanners are in active mode."""
with (
patch(
"homeassistant.components.switchbot.config_flow.async_discovered_service_info",
return_value=[WOHAND_SERVICE_INFO],
),
patch_async_setup_entry() as mock_setup_entry,
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)

# Should skip menu and go directly to select_device -> confirm
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "confirm"

result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={}
)
await hass.async_block_till_done()

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "Bot EEFF"
assert result["data"] == {
CONF_ADDRESS: "AA:BB:CC:DD:EE:FF",
CONF_SENSOR_TYPE: "bot",
}
assert len(mock_setup_entry.mock_calls) == 1


async def test_user_show_menu_when_passive_scanner_present(hass: HomeAssistant) -> None:
"""Test that menu is shown when any scanner is in passive mode."""
mock_scanner_active = Mock()
Expand All @@ -1502,7 +1458,7 @@ async def test_user_show_menu_when_passive_scanner_present(hass: HomeAssistant)

with (
patch(
"homeassistant.components.switchbot.config_flow.async_current_scanners",
"homeassistant.components.bluetooth.async_current_scanners",
return_value=[mock_scanner_active, mock_scanner_passive],
),
patch(
Expand Down Expand Up @@ -1546,7 +1502,7 @@ async def test_user_show_menu_when_no_scanners(hass: HomeAssistant) -> None:
"""Test that menu is shown when no scanners are available."""
with (
patch(
"homeassistant.components.switchbot.config_flow.async_current_scanners",
"homeassistant.components.bluetooth.async_current_scanners",
return_value=[],
),
patch(
Expand Down
Loading