Switch for swiss_public_transport to unique_id instead of unique_entry (#107910)
* use unique_id instead of unique_entry * move entry mock out of patch context
This commit is contained in:
parent
3d410a1d6e
commit
802f0da493
2 changed files with 24 additions and 32 deletions
|
@ -39,12 +39,10 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
"""Async user step to set up the connection."""
|
"""Async user step to set up the connection."""
|
||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self._async_abort_entries_match(
|
await self.async_set_unique_id(
|
||||||
{
|
f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}"
|
||||||
CONF_START: user_input[CONF_START],
|
|
||||||
CONF_DESTINATION: user_input[CONF_DESTINATION],
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
opendata = OpendataTransport(
|
opendata = OpendataTransport(
|
||||||
|
@ -60,9 +58,6 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
_LOGGER.exception("Unknown error")
|
_LOGGER.exception("Unknown error")
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
else:
|
else:
|
||||||
await self.async_set_unique_id(
|
|
||||||
f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}"
|
|
||||||
)
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}",
|
title=f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}",
|
||||||
data=user_input,
|
data=user_input,
|
||||||
|
@ -77,12 +72,10 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
async def async_step_import(self, import_input: dict[str, Any]) -> FlowResult:
|
async def async_step_import(self, import_input: dict[str, Any]) -> FlowResult:
|
||||||
"""Async import step to set up the connection."""
|
"""Async import step to set up the connection."""
|
||||||
self._async_abort_entries_match(
|
await self.async_set_unique_id(
|
||||||
{
|
f"{import_input[CONF_START]} {import_input[CONF_DESTINATION]}"
|
||||||
CONF_START: import_input[CONF_START],
|
|
||||||
CONF_DESTINATION: import_input[CONF_DESTINATION],
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
self._abort_if_unique_id_configured()
|
||||||
|
|
||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
opendata = OpendataTransport(
|
opendata = OpendataTransport(
|
||||||
|
@ -102,9 +95,6 @@ class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
)
|
)
|
||||||
return self.async_abort(reason="unknown")
|
return self.async_abort(reason="unknown")
|
||||||
|
|
||||||
await self.async_set_unique_id(
|
|
||||||
f"{import_input[CONF_START]} {import_input[CONF_DESTINATION]}"
|
|
||||||
)
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=import_input[CONF_NAME],
|
title=import_input[CONF_NAME],
|
||||||
data=import_input,
|
data=import_input,
|
||||||
|
|
|
@ -65,7 +65,7 @@ async def test_flow_user_init_data_success(hass: HomeAssistant) -> None:
|
||||||
(IndexError(), "unknown"),
|
(IndexError(), "unknown"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_flow_user_init_data_unknown_error_and_recover(
|
async def test_flow_user_init_data_error_and_recover(
|
||||||
hass: HomeAssistant, raise_error, text_error
|
hass: HomeAssistant, raise_error, text_error
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test unknown errors."""
|
"""Test unknown errors."""
|
||||||
|
@ -88,9 +88,6 @@ async def test_flow_user_init_data_unknown_error_and_recover(
|
||||||
# Recover
|
# Recover
|
||||||
mock_OpendataTransport.side_effect = None
|
mock_OpendataTransport.side_effect = None
|
||||||
mock_OpendataTransport.return_value = True
|
mock_OpendataTransport.return_value = True
|
||||||
result = await hass.config_entries.flow.async_init(
|
|
||||||
config_flow.DOMAIN, context={"source": "user"}
|
|
||||||
)
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=MOCK_DATA_STEP,
|
user_input=MOCK_DATA_STEP,
|
||||||
|
@ -108,20 +105,26 @@ async def test_flow_user_init_data_already_configured(hass: HomeAssistant) -> No
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=config_flow.DOMAIN,
|
domain=config_flow.DOMAIN,
|
||||||
data=MOCK_DATA_STEP,
|
data=MOCK_DATA_STEP,
|
||||||
|
unique_id=f"{MOCK_DATA_STEP[CONF_START]} {MOCK_DATA_STEP[CONF_DESTINATION]}",
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_init(
|
with patch(
|
||||||
config_flow.DOMAIN, context={"source": "user"}
|
"homeassistant.components.swiss_public_transport.config_flow.OpendataTransport.async_get_data",
|
||||||
)
|
autospec=True,
|
||||||
|
return_value=True,
|
||||||
|
):
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
config_flow.DOMAIN, context={"source": "user"}
|
||||||
|
)
|
||||||
|
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=MOCK_DATA_STEP,
|
user_input=MOCK_DATA_STEP,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == FlowResultType.ABORT
|
assert result["type"] == FlowResultType.ABORT
|
||||||
assert result["reason"] == "already_configured"
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
|
||||||
MOCK_DATA_IMPORT = {
|
MOCK_DATA_IMPORT = {
|
||||||
|
@ -161,9 +164,7 @@ async def test_import(
|
||||||
(IndexError(), "unknown"),
|
(IndexError(), "unknown"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_import_cannot_connect_error(
|
async def test_import_error(hass: HomeAssistant, raise_error, text_error) -> None:
|
||||||
hass: HomeAssistant, raise_error, text_error
|
|
||||||
) -> None:
|
|
||||||
"""Test import flow cannot_connect error."""
|
"""Test import flow cannot_connect error."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.swiss_public_transport.config_flow.OpendataTransport.async_get_data",
|
"homeassistant.components.swiss_public_transport.config_flow.OpendataTransport.async_get_data",
|
||||||
|
@ -187,6 +188,7 @@ async def test_import_already_configured(hass: HomeAssistant) -> None:
|
||||||
entry = MockConfigEntry(
|
entry = MockConfigEntry(
|
||||||
domain=config_flow.DOMAIN,
|
domain=config_flow.DOMAIN,
|
||||||
data=MOCK_DATA_IMPORT,
|
data=MOCK_DATA_IMPORT,
|
||||||
|
unique_id=f"{MOCK_DATA_IMPORT[CONF_START]} {MOCK_DATA_IMPORT[CONF_DESTINATION]}",
|
||||||
)
|
)
|
||||||
entry.add_to_hass(hass)
|
entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue