Standardize import step variable name (part 5) (#124698)

* Standardize import step variable name (part 5)

* Revert point

* Adjust soma tests
This commit is contained in:
epenet 2024-08-28 09:51:49 +02:00 committed by GitHub
parent 1f3c99dff3
commit bcc66c9a86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 54 additions and 42 deletions

View File

@ -22,17 +22,17 @@ class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN):
self.dongle_path = None
self.discovery_info = None
async def async_step_import(self, data=None):
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Import a yaml configuration."""
if not await self.validate_enocean_conf(data):
if not await self.validate_enocean_conf(import_data):
LOGGER.warning(
"Cannot import yaml configuration: %s is not a valid dongle path",
data[CONF_DEVICE],
import_data[CONF_DEVICE],
)
return self.async_abort(reason="invalid_dongle_path")
return self.create_enocean_entry(data)
return self.create_enocean_entry(import_data)
async def async_step_user(
self, user_input: dict[str, Any] | None = None

View File

@ -43,9 +43,9 @@ class HeosFlowHandler(ConfigFlow, domain=DOMAIN):
# Show selection form
return self.async_show_form(step_id="user")
async def async_step_import(self, user_input=None):
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Occurs when an entry is setup through config."""
host = user_input[CONF_HOST]
host = import_data[CONF_HOST]
# raise_on_progress is False here in case ssdp discovers
# heos first which would block the import
await self.async_set_unique_id(DOMAIN, raise_on_progress=False)

View File

@ -54,9 +54,9 @@ class SmartThingsFlowHandler(ConfigFlow, domain=DOMAIN):
self.location_id = None
self.endpoints_initialized = False
async def async_step_import(self, user_input=None):
async def async_step_import(self, import_data: None) -> ConfigFlowResult:
"""Occurs when a previously entry setup fails and is re-initiated."""
return await self.async_step_user(user_input)
return await self.async_step_user(import_data)
async def async_step_user(
self, user_input: dict[str, Any] | None = None

View File

@ -67,8 +67,8 @@ class SomaFlowHandler(ConfigFlow, domain=DOMAIN):
_LOGGER.error("Connection to SOMA Connect failed with KeyError")
return self.async_abort(reason="connection_error")
async def async_step_import(self, user_input=None):
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Handle flow start from existing config section."""
if self._async_current_entries():
return self.async_abort(reason="already_setup")
return await self.async_step_creation(user_input)
return await self.async_step_creation(import_data)

View File

@ -126,10 +126,10 @@ class SongpalConfigFlow(ConfigFlow, domain=DOMAIN):
return await self.async_step_init()
async def async_step_import(self, user_input=None):
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Import a config entry."""
name = user_input.get(CONF_NAME)
endpoint = user_input.get(CONF_ENDPOINT)
name = import_data.get(CONF_NAME)
endpoint = import_data.get(CONF_ENDPOINT)
parsed_url = urlparse(endpoint)
# Try to connect to test the endpoint
@ -146,4 +146,4 @@ class SongpalConfigFlow(ConfigFlow, domain=DOMAIN):
self.conf = SongpalConfig(name, parsed_url.hostname, endpoint)
return await self.async_step_init(user_input)
return await self.async_step_init(import_data)

View File

@ -244,21 +244,21 @@ class YeelightConfigFlow(ConfigFlow, domain=DOMAIN):
data_schema=vol.Schema({vol.Required(CONF_DEVICE): vol.In(devices_name)}),
)
async def async_step_import(self, user_input=None):
async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult:
"""Handle import step."""
host = user_input[CONF_HOST]
host = import_data[CONF_HOST]
try:
await self._async_try_connect(host, raise_on_progress=False)
except CannotConnect:
_LOGGER.error("Failed to import %s: cannot connect", host)
return self.async_abort(reason="cannot_connect")
if CONF_NIGHTLIGHT_SWITCH_TYPE in user_input:
user_input[CONF_NIGHTLIGHT_SWITCH] = (
user_input.pop(CONF_NIGHTLIGHT_SWITCH_TYPE)
if CONF_NIGHTLIGHT_SWITCH_TYPE in import_data:
import_data[CONF_NIGHTLIGHT_SWITCH] = (
import_data.pop(CONF_NIGHTLIGHT_SWITCH_TYPE)
== NIGHTLIGHT_SWITCH_TYPE_LIGHT
)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
return self.async_create_entry(title=import_data[CONF_NAME], data=import_data)
async def _async_try_connect(self, host, raise_on_progress=True):
"""Set up with options."""

View File

@ -5,7 +5,8 @@ from unittest.mock import patch
from api.soma_api import SomaApi
from requests import RequestException
from homeassistant.components.soma import DOMAIN, config_flow
from homeassistant.components.soma import DOMAIN
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -17,57 +18,66 @@ MOCK_PORT = 3000
async def test_form(hass: HomeAssistant) -> None:
"""Test user form showing."""
flow = config_flow.SomaFlowHandler()
flow.hass = hass
result = await flow.async_step_user()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
async def test_import_abort(hass: HomeAssistant) -> None:
"""Test configuration from YAML aborting with existing entity."""
flow = config_flow.SomaFlowHandler()
flow.hass = hass
MockConfigEntry(domain=DOMAIN).add_to_hass(hass)
result = await flow.async_step_import()
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_setup"
async def test_import_create(hass: HomeAssistant) -> None:
"""Test configuration from YAML."""
flow = config_flow.SomaFlowHandler()
flow.hass = hass
with patch.object(SomaApi, "list_devices", return_value={"result": "success"}):
result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT})
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={"host": MOCK_HOST, "port": MOCK_PORT},
)
assert result["type"] is FlowResultType.CREATE_ENTRY
async def test_error_status(hass: HomeAssistant) -> None:
"""Test Connect successfully returning error status."""
flow = config_flow.SomaFlowHandler()
flow.hass = hass
with patch.object(SomaApi, "list_devices", return_value={"result": "error"}):
result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT})
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={"host": MOCK_HOST, "port": MOCK_PORT},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "result_error"
async def test_key_error(hass: HomeAssistant) -> None:
"""Test Connect returning empty string."""
flow = config_flow.SomaFlowHandler()
flow.hass = hass
with patch.object(SomaApi, "list_devices", return_value={}):
result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT})
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={"host": MOCK_HOST, "port": MOCK_PORT},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "connection_error"
async def test_exception(hass: HomeAssistant) -> None:
"""Test if RequestException fires when no connection can be made."""
flow = config_flow.SomaFlowHandler()
flow.hass = hass
with patch.object(SomaApi, "list_devices", side_effect=RequestException()):
result = await flow.async_step_import({"host": MOCK_HOST, "port": MOCK_PORT})
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data={"host": MOCK_HOST, "port": MOCK_PORT},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "connection_error"
@ -75,8 +85,10 @@ async def test_exception(hass: HomeAssistant) -> None:
async def test_full_flow(hass: HomeAssistant) -> None:
"""Check classic use case."""
hass.data[DOMAIN] = {}
flow = config_flow.SomaFlowHandler()
flow.hass = hass
with patch.object(SomaApi, "list_devices", return_value={"result": "success"}):
result = await flow.async_step_user({"host": MOCK_HOST, "port": MOCK_PORT})
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data={"host": MOCK_HOST, "port": MOCK_PORT},
)
assert result["type"] is FlowResultType.CREATE_ENTRY