Use constants in Alpha2 config flow (#107518)
This commit is contained in:
parent
efffbc08aa
commit
f2483bf660
2 changed files with 14 additions and 9 deletions
|
@ -8,7 +8,7 @@ import aiohttp
|
|||
from moehlenhoff_alpha2 import Alpha2Base
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.const import CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
@ -24,7 +24,7 @@ UPDATE_INTERVAL = timedelta(seconds=60)
|
|||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up a config entry."""
|
||||
base = Alpha2Base(entry.data["host"])
|
||||
base = Alpha2Base(entry.data[CONF_HOST])
|
||||
coordinator = Alpha2BaseCoordinator(hass, base)
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
|
|
@ -1,27 +1,30 @@
|
|||
"""Alpha2 config flow."""
|
||||
import asyncio
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import aiohttp
|
||||
from moehlenhoff_alpha2 import Alpha2Base
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.config_entries import ConfigFlow
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DATA_SCHEMA = vol.Schema({vol.Required("host"): str})
|
||||
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
||||
|
||||
|
||||
async def validate_input(data):
|
||||
async def validate_input(data: dict[str, Any]) -> dict[str, str]:
|
||||
"""Validate the user input allows us to connect.
|
||||
|
||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||
"""
|
||||
|
||||
base = Alpha2Base(data["host"])
|
||||
base = Alpha2Base(data[CONF_HOST])
|
||||
try:
|
||||
await base.update_data()
|
||||
except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError):
|
||||
|
@ -34,16 +37,18 @@ async def validate_input(data):
|
|||
return {"title": base.name}
|
||||
|
||||
|
||||
class Alpha2BaseConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
class Alpha2BaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Möhlenhoff Alpha2 config flow."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Handle a flow initialized by the user."""
|
||||
errors = {}
|
||||
if user_input is not None:
|
||||
self._async_abort_entries_match({"host": user_input["host"]})
|
||||
self._async_abort_entries_match({CONF_HOST: user_input[CONF_HOST]})
|
||||
result = await validate_input(user_input)
|
||||
if result.get("error"):
|
||||
errors["base"] = result["error"]
|
||||
|
|
Loading…
Add table
Reference in a new issue