forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_flow.py
111 lines (97 loc) · 3.93 KB
/
config_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Config flow for swiss_public_transport."""
import logging
from typing import Any
from opendata_transport import OpendataTransport
from opendata_transport.exceptions import (
OpendataTransportConnectionError,
OpendataTransportError,
)
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_NAME
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from .const import CONF_DESTINATION, CONF_START, DOMAIN, PLACEHOLDERS
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_START): cv.string,
vol.Required(CONF_DESTINATION): cv.string,
}
)
_LOGGER = logging.getLogger(__name__)
class SwissPublicTransportConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Swiss public transport config flow."""
VERSION = 1
MINOR_VERSION = 2
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Async user step to set up the connection."""
errors: dict[str, str] = {}
if user_input is not None:
self._async_abort_entries_match(
{
CONF_START: user_input[CONF_START],
CONF_DESTINATION: user_input[CONF_DESTINATION],
}
)
session = async_get_clientsession(self.hass)
opendata = OpendataTransport(
user_input[CONF_START], user_input[CONF_DESTINATION], session
)
try:
await opendata.async_get_data()
except OpendataTransportConnectionError:
errors["base"] = "cannot_connect"
except OpendataTransportError:
errors["base"] = "bad_config"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unknown error")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(
f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}"
)
return self.async_create_entry(
title=f"{user_input[CONF_START]} {user_input[CONF_DESTINATION]}",
data=user_input,
)
return self.async_show_form(
step_id="user",
data_schema=DATA_SCHEMA,
errors=errors,
description_placeholders=PLACEHOLDERS,
)
async def async_step_import(self, import_input: dict[str, Any]) -> FlowResult:
"""Async import step to set up the connection."""
self._async_abort_entries_match(
{
CONF_START: import_input[CONF_START],
CONF_DESTINATION: import_input[CONF_DESTINATION],
}
)
session = async_get_clientsession(self.hass)
opendata = OpendataTransport(
import_input[CONF_START], import_input[CONF_DESTINATION], session
)
try:
await opendata.async_get_data()
except OpendataTransportConnectionError:
return self.async_abort(reason="cannot_connect")
except OpendataTransportError:
return self.async_abort(reason="bad_config")
except Exception: # pylint: disable=broad-except
_LOGGER.error(
"Unknown error raised by python-opendata-transport for '%s %s', check at http://transport.opendata.ch/examples/stationboard.html if your station names and your parameters are valid",
import_input[CONF_START],
import_input[CONF_DESTINATION],
)
return self.async_abort(reason="unknown")
await self.async_set_unique_id(
f"{import_input[CONF_START]} {import_input[CONF_DESTINATION]}"
)
return self.async_create_entry(
title=import_input[CONF_NAME],
data=import_input,
)