2021-11-30 19:53:41 +01:00
|
|
|
"""Adds config flow for Trafikverket Weather integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-30 22:54:10 +01:00
|
|
|
import json
|
|
|
|
|
2021-11-30 19:53:41 +01:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_API_KEY, CONF_MONITORED_CONDITIONS, CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
from .const import CONF_STATION, DOMAIN
|
|
|
|
from .sensor import SENSOR_TYPES
|
|
|
|
|
2021-11-30 22:54:10 +01:00
|
|
|
SENSOR_LIST: set[str] = {description.key for (description) in SENSOR_TYPES}
|
2021-11-30 19:53:41 +01:00
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_STATION): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TVWeatherConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for Trafikverket Weatherstation integration."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
entry: config_entries.ConfigEntry
|
|
|
|
|
|
|
|
async def async_step_import(self, config: dict):
|
|
|
|
"""Import a configuration from config.yaml."""
|
|
|
|
|
|
|
|
self.context.update(
|
|
|
|
{"title_placeholders": {CONF_NAME: f"YAML import {DOMAIN}"}}
|
|
|
|
)
|
|
|
|
|
|
|
|
self._async_abort_entries_match({CONF_NAME: config[CONF_NAME]})
|
|
|
|
return await self.async_step_user(user_input=config)
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle the initial step."""
|
|
|
|
errors = {}
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
name = user_input[CONF_NAME]
|
|
|
|
api_key = user_input[CONF_API_KEY]
|
|
|
|
station = user_input[CONF_STATION]
|
2021-11-30 22:54:10 +01:00
|
|
|
conditions = json.dumps(list(SENSOR_LIST))
|
2021-11-30 19:53:41 +01:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=name,
|
|
|
|
data={
|
|
|
|
CONF_NAME: name,
|
|
|
|
CONF_API_KEY: api_key,
|
|
|
|
CONF_STATION: station,
|
|
|
|
CONF_MONITORED_CONDITIONS: conditions,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=DATA_SCHEMA,
|
|
|
|
errors=errors,
|
|
|
|
)
|