Improve config flow Luftdaten (#62589)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof 2021-12-22 13:00:51 +01:00 committed by GitHub
parent 925e4998b4
commit 496165711d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 25 deletions

View File

@ -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
)

View File

@ -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,
}