Discover Switchbot MAC in config flow (#56616)

* Update config_flow.py

* Switchbot Config_flow discover mac instead of needing to type it.

* Do not show already configured devices in config flow, abort if no unconfigured devices.

* Apply suggestions from code review

Co-authored-by: J. Nick Koston <nick@koston.org>

* Move MAC to top of config flow form dict.

* Update homeassistant/components/switchbot/config_flow.py

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
RenierM26 2021-09-27 22:12:40 +02:00 committed by GitHub
parent b40d229369
commit b15f11f46a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 125 deletions

View file

@ -12,18 +12,35 @@ class MocGetSwitchbotDevices:
"""Get switchbot devices class constructor."""
self._interface = interface
self._all_services_data = {
"mac_address": "e7:89:43:99:99:99",
"Flags": "06",
"Manufacturer": "5900e78943d9fe7c",
"Complete 128b Services": "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
"data": {
"switchMode": "true",
"isOn": "true",
"battery": 91,
"rssi": -71,
"e78943999999": {
"mac_address": "e7:89:43:99:99:99",
"Flags": "06",
"Manufacturer": "5900e78943d9fe7c",
"Complete 128b Services": "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
"data": {
"switchMode": "true",
"isOn": "true",
"battery": 91,
"rssi": -71,
},
"model": "H",
"modelName": "WoHand",
},
"e78943909090": {
"mac_address": "e7:89:43:90:90:90",
"Flags": "06",
"Manufacturer": "5900e78943d9fe7c",
"Complete 128b Services": "cba20d00-224d-11e6-9fb8-0002a5d5c51b",
"data": {
"calibration": True,
"battery": 74,
"position": 100,
"lightLevel": 2,
"rssi": -73,
},
"model": "c",
"modelName": "WoCurtain",
},
"model": "H",
"modelName": "WoHand",
}
self._curtain_all_services_data = {
"mac_address": "e7:89:43:90:90:90",
@ -90,6 +107,5 @@ def switchbot_config_flow(hass):
instance = mock_switchbot.return_value
instance.discover = MagicMock(return_value=True)
instance.get_device_data = MagicMock(return_value=True)
yield mock_switchbot

View file

@ -19,8 +19,6 @@ from homeassistant.setup import async_setup_component
from . import (
USER_INPUT,
USER_INPUT_CURTAIN,
USER_INPUT_INVALID,
USER_INPUT_UNSUPPORTED_DEVICE,
YAML_CONFIG,
_patch_async_setup_entry,
init_integration,
@ -58,24 +56,6 @@ async def test_user_form_valid_mac(hass):
assert len(mock_setup_entry.mock_calls) == 1
# test duplicate device creation fails.
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
await hass.async_block_till_done()
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "already_configured_device"
# test curtain device creation.
result = await hass.config_entries.flow.async_init(
@ -103,47 +83,13 @@ async def test_user_form_valid_mac(hass):
assert len(mock_setup_entry.mock_calls) == 1
async def test_user_form_unsupported_device(hass):
"""Test the user initiated form for unsupported device type."""
await async_setup_component(hass, "persistent_notification", {})
# tests abort if no unconfigured devices are found.
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_UNSUPPORTED_DEVICE,
)
await hass.async_block_till_done()
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "switchbot_unsupported_type"
async def test_user_form_invalid_device(hass):
"""Test the user initiated form for invalid device type."""
await async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT_INVALID,
)
await hass.async_block_till_done()
assert result["type"] == RESULT_TYPE_FORM
assert result["errors"] == {"base": "cannot_connect"}
assert result["reason"] == "no_unconfigured_devices"
async def test_async_step_import(hass):
@ -175,20 +121,13 @@ async def test_user_form_exception(hass, switchbot_config_flow):
DOMAIN, context={"source": SOURCE_USER}
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "cannot_connect"}
assert result["type"] == RESULT_TYPE_ABORT
assert result["reason"] == "cannot_connect"
switchbot_config_flow.side_effect = Exception
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
USER_INPUT,
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == RESULT_TYPE_ABORT