mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +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
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_SHOW_ON_MAP
|
||||||
CONF_MONITORED_CONDITIONS,
|
|
||||||
CONF_SCAN_INTERVAL,
|
|
||||||
CONF_SENSORS,
|
|
||||||
CONF_SHOW_ON_MAP,
|
|
||||||
)
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
import homeassistant.helpers.config_validation as cv
|
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):
|
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
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> FlowResult:
|
) -> FlowResult:
|
||||||
"""Handle the start of the config flow."""
|
"""Handle the start of the config flow."""
|
||||||
|
if user_input is None:
|
||||||
if not user_input:
|
|
||||||
return self._show_form()
|
return self._show_form()
|
||||||
|
|
||||||
await self.async_set_unique_id(str(user_input[CONF_SENSOR_ID]))
|
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:
|
if not valid:
|
||||||
return self._show_form({CONF_SENSOR_ID: "invalid_sensor"})
|
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(
|
return self.async_create_entry(
|
||||||
title=str(user_input[CONF_SENSOR_ID]), data=user_input
|
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 import DOMAIN
|
||||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
|
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
|
||||||
from homeassistant.config_entries import SOURCE_USER
|
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.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import (
|
from homeassistant.data_entry_flow import (
|
||||||
RESULT_TYPE_ABORT,
|
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("step_id") == SOURCE_USER
|
||||||
assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"}
|
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:
|
async def test_invalid_sensor(hass: HomeAssistant) -> None:
|
||||||
"""Test that an invalid sensor throws an error."""
|
"""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("type") == RESULT_TYPE_FORM
|
||||||
assert result2.get("step_id") == SOURCE_USER
|
assert result2.get("step_id") == SOURCE_USER
|
||||||
assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"}
|
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:
|
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"],
|
result["flow_id"],
|
||||||
user_input={
|
user_input={
|
||||||
CONF_SENSOR_ID: 12345,
|
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("title") == "12345"
|
||||||
assert result2.get("data") == {
|
assert result2.get("data") == {
|
||||||
CONF_SENSOR_ID: 12345,
|
CONF_SENSOR_ID: 12345,
|
||||||
CONF_SHOW_ON_MAP: False,
|
CONF_SHOW_ON_MAP: True,
|
||||||
CONF_SCAN_INTERVAL: 600.0,
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user