2021-12-28 20:40:00 +00:00
|
|
|
"""Config flow to configure the Sensor.Community integration."""
|
2021-12-22 11:20:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2019-10-19 16:22:32 +00:00
|
|
|
from luftdaten import Luftdaten
|
|
|
|
from luftdaten.exceptions import LuftdatenConnectionError
|
2018-11-06 13:27:52 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2021-12-22 12:00:51 +00:00
|
|
|
from homeassistant.const import CONF_SHOW_ON_MAP
|
2018-11-06 13:27:52 +00:00
|
|
|
from homeassistant.core import callback
|
2021-12-22 11:20:35 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2019-01-31 01:12:59 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-12-22 12:00:51 +00:00
|
|
|
from .const import CONF_SENSOR_ID, DOMAIN
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
|
2021-12-28 20:40:00 +00:00
|
|
|
class SensorCommunityFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a Sensor.Community config flow."""
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
@callback
|
2021-12-22 11:20:35 +00:00
|
|
|
def _show_form(self, errors: dict[str, str] | None = None) -> FlowResult:
|
2018-11-06 13:27:52 +00:00
|
|
|
"""Show the form to the user."""
|
|
|
|
return self.async_show_form(
|
2021-12-22 11:20:35 +00:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_SENSOR_ID): cv.positive_int,
|
|
|
|
vol.Optional(CONF_SHOW_ON_MAP, default=False): bool,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=errors or {},
|
2018-11-06 13:27:52 +00:00
|
|
|
)
|
|
|
|
|
2021-12-22 11:20:35 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2018-11-06 13:27:52 +00:00
|
|
|
"""Handle the start of the config flow."""
|
2021-12-22 12:00:51 +00:00
|
|
|
if user_input is None:
|
2018-11-06 13:27:52 +00:00
|
|
|
return self._show_form()
|
|
|
|
|
2021-12-19 11:42:52 +00:00
|
|
|
await self.async_set_unique_id(str(user_input[CONF_SENSOR_ID]))
|
|
|
|
self._abort_if_unique_id_configured()
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-12-28 20:40:00 +00:00
|
|
|
sensor_community = Luftdaten(user_input[CONF_SENSOR_ID])
|
2018-11-06 13:27:52 +00:00
|
|
|
try:
|
2021-12-28 20:40:00 +00:00
|
|
|
await sensor_community.get_data()
|
|
|
|
valid = await sensor_community.validate_sensor()
|
2019-10-19 16:22:32 +00:00
|
|
|
except LuftdatenConnectionError:
|
2020-10-25 22:40:12 +00:00
|
|
|
return self._show_form({CONF_SENSOR_ID: "cannot_connect"})
|
2018-11-06 13:27:52 +00:00
|
|
|
|
|
|
|
if not valid:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._show_form({CONF_SENSOR_ID: "invalid_sensor"})
|
2018-11-06 13:27:52 +00:00
|
|
|
|
2021-12-19 11:42:52 +00:00
|
|
|
return self.async_create_entry(
|
|
|
|
title=str(user_input[CONF_SENSOR_ID]), data=user_input
|
|
|
|
)
|