59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
"""Config flow for the HA Probe integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
import probatio
|
|
|
|
from homeassistant.components import webhook
|
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
from .const import CONF_WEBHOOK_ID, DOMAIN
|
|
|
|
WEBHOOK_ID_PATTERN = re.compile(r"^[a-z0-9_-]+$")
|
|
|
|
|
|
class HaProbeConfigFlow(ConfigFlow, domain=DOMAIN):
|
|
"""Config flow for HA Probe."""
|
|
|
|
VERSION = 1
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> ConfigFlowResult:
|
|
"""Handle the initial setup step."""
|
|
errors: dict[str, str] = {}
|
|
|
|
if user_input is not None:
|
|
webhook_id = user_input[CONF_WEBHOOK_ID].strip()
|
|
if not WEBHOOK_ID_PATTERN.match(webhook_id):
|
|
errors[CONF_WEBHOOK_ID] = "invalid_webhook_id"
|
|
elif self._webhook_id_in_use(webhook_id):
|
|
errors[CONF_WEBHOOK_ID] = "webhook_id_in_use"
|
|
else:
|
|
return self.async_create_entry(
|
|
title=user_input[CONF_NAME], data=user_input
|
|
)
|
|
|
|
data_schema = probatio.Schema(
|
|
{
|
|
probatio.Required(CONF_NAME, default="HA Probe"): str,
|
|
probatio.Required(
|
|
CONF_WEBHOOK_ID, default=webhook.async_generate_id()
|
|
): str,
|
|
}
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=data_schema, errors=errors
|
|
)
|
|
|
|
def _webhook_id_in_use(self, webhook_id: str) -> bool:
|
|
return any(
|
|
entry.data.get(CONF_WEBHOOK_ID) == webhook_id
|
|
for entry in self._async_current_entries()
|
|
)
|