mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Use constants in Alpha2 config flow (#107518)
This commit is contained in:
parent
efffbc08aa
commit
f2483bf660
@ -8,7 +8,7 @@ import aiohttp
|
|||||||
from moehlenhoff_alpha2 import Alpha2Base
|
from moehlenhoff_alpha2 import Alpha2Base
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
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:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up a config entry."""
|
"""Set up a config entry."""
|
||||||
base = Alpha2Base(entry.data["host"])
|
base = Alpha2Base(entry.data[CONF_HOST])
|
||||||
coordinator = Alpha2BaseCoordinator(hass, base)
|
coordinator = Alpha2BaseCoordinator(hass, base)
|
||||||
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
@ -1,27 +1,30 @@
|
|||||||
"""Alpha2 config flow."""
|
"""Alpha2 config flow."""
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from moehlenhoff_alpha2 import Alpha2Base
|
from moehlenhoff_alpha2 import Alpha2Base
|
||||||
import voluptuous as vol
|
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
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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.
|
"""Validate the user input allows us to connect.
|
||||||
|
|
||||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
base = Alpha2Base(data["host"])
|
base = Alpha2Base(data[CONF_HOST])
|
||||||
try:
|
try:
|
||||||
await base.update_data()
|
await base.update_data()
|
||||||
except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError):
|
except (aiohttp.client_exceptions.ClientConnectorError, asyncio.TimeoutError):
|
||||||
@ -34,16 +37,18 @@ async def validate_input(data):
|
|||||||
return {"title": base.name}
|
return {"title": base.name}
|
||||||
|
|
||||||
|
|
||||||
class Alpha2BaseConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
class Alpha2BaseConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Möhlenhoff Alpha2 config flow."""
|
"""Möhlenhoff Alpha2 config flow."""
|
||||||
|
|
||||||
VERSION = 1
|
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."""
|
"""Handle a flow initialized by the user."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
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)
|
result = await validate_input(user_input)
|
||||||
if result.get("error"):
|
if result.get("error"):
|
||||||
errors["base"] = result["error"]
|
errors["base"] = result["error"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user