mirror of
https://github.com/home-assistant/core.git
synced 2025-12-19 18:38:58 +00:00
Remove the restriction that Bluetooth login to the Switchbot account is only possible in active mode (#157154)
This commit is contained in:
@@ -17,9 +17,7 @@ from switchbot import (
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.bluetooth import (
|
||||
BluetoothScanningMode,
|
||||
BluetoothServiceInfoBleak,
|
||||
async_current_scanners,
|
||||
async_discovered_service_info,
|
||||
)
|
||||
from homeassistant.config_entries import (
|
||||
@@ -325,15 +323,6 @@ class SwitchbotConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the user step to choose cloud login or direct discovery."""
|
||||
# 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"],
|
||||
|
||||
@@ -44,25 +44,13 @@ from tests.common import MockConfigEntry
|
||||
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
|
||||
@@ -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()
|
||||
@@ -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(
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user