mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 17:27:52 +00:00
Improve config flow Luftdaten (#62589)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
925e4998b4
commit
496165711d
@ -8,17 +8,12 @@ from luftdaten.exceptions import LuftdatenConnectionError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import (
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
CONF_SCAN_INTERVAL,
|
||||
CONF_SENSORS,
|
||||
CONF_SHOW_ON_MAP,
|
||||
)
|
||||
from homeassistant.const import CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import CONF_SENSOR_ID, DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
from .const import CONF_SENSOR_ID, DOMAIN
|
||||
|
||||
|
||||
class LuftDatenFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
@ -44,8 +39,7 @@ class LuftDatenFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Handle the start of the config flow."""
|
||||
|
||||
if not user_input:
|
||||
if user_input is None:
|
||||
return self._show_form()
|
||||
|
||||
await self.async_set_unique_id(str(user_input[CONF_SENSOR_ID]))
|
||||
@ -61,18 +55,6 @@ class LuftDatenFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if not valid:
|
||||
return self._show_form({CONF_SENSOR_ID: "invalid_sensor"})
|
||||
|
||||
available_sensors = [
|
||||
x for x, x_values in luftdaten.values.items() if x_values is not None
|
||||
]
|
||||
|
||||
if available_sensors:
|
||||
user_input.update(
|
||||
{CONF_SENSORS: {CONF_MONITORED_CONDITIONS: available_sensors}}
|
||||
)
|
||||
|
||||
scan_interval = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||
user_input.update({CONF_SCAN_INTERVAL: scan_interval.total_seconds()})
|
||||
|
||||
return self.async_create_entry(
|
||||
title=str(user_input[CONF_SENSOR_ID]), data=user_input
|
||||
)
|
||||
|
@ -6,7 +6,7 @@ from luftdaten.exceptions import LuftdatenConnectionError
|
||||
from homeassistant.components.luftdaten import DOMAIN
|
||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP
|
||||
from homeassistant.const import CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
@ -60,6 +60,21 @@ async def test_communication_error(hass: HomeAssistant) -> None:
|
||||
assert result2.get("step_id") == SOURCE_USER
|
||||
assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"}
|
||||
|
||||
with patch("luftdaten.Luftdaten.get_data"), patch(
|
||||
"luftdaten.Luftdaten.validate_sensor", return_value=True
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result3.get("type") == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result3.get("title") == "12345"
|
||||
assert result3.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
}
|
||||
|
||||
|
||||
async def test_invalid_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test that an invalid sensor throws an error."""
|
||||
@ -82,6 +97,22 @@ async def test_invalid_sensor(hass: HomeAssistant) -> None:
|
||||
assert result2.get("type") == RESULT_TYPE_FORM
|
||||
assert result2.get("step_id") == SOURCE_USER
|
||||
assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"}
|
||||
assert "flow_id" in result2
|
||||
|
||||
with patch("luftdaten.Luftdaten.get_data"), patch(
|
||||
"luftdaten.Luftdaten.validate_sensor", return_value=True
|
||||
):
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={CONF_SENSOR_ID: 12345},
|
||||
)
|
||||
|
||||
assert result3.get("type") == RESULT_TYPE_CREATE_ENTRY
|
||||
assert result3.get("title") == "12345"
|
||||
assert result3.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
}
|
||||
|
||||
|
||||
async def test_step_user(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None:
|
||||
@ -101,7 +132,7 @@ async def test_step_user(hass: HomeAssistant, mock_setup_entry: MagicMock) -> No
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
CONF_SHOW_ON_MAP: True,
|
||||
},
|
||||
)
|
||||
|
||||
@ -109,6 +140,5 @@ async def test_step_user(hass: HomeAssistant, mock_setup_entry: MagicMock) -> No
|
||||
assert result2.get("title") == "12345"
|
||||
assert result2.get("data") == {
|
||||
CONF_SENSOR_ID: 12345,
|
||||
CONF_SHOW_ON_MAP: False,
|
||||
CONF_SCAN_INTERVAL: 600.0,
|
||||
CONF_SHOW_ON_MAP: True,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user