Files
Notification-System-Integra…/config_flow.py
2026-06-11 11:34:40 +00:00

96 lines
3.0 KiB
Python

"""Config flow for Notification Store."""
from __future__ import annotations
from typing import Any
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_NAME
from .const import (
CONF_SCAN_INTERVAL,
CONF_STORE_PATH,
DEFAULT_NAME,
DEFAULT_SCAN_INTERVAL,
DEFAULT_STORE_PATH,
DOMAIN,
MIN_SCAN_INTERVAL,
)
class NotificationStoreConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Notification Store."""
VERSION = 1
async def async_step_user(self, user_input: dict[str, Any] | None = None):
"""Handle the initial step."""
await self.async_set_unique_id(DOMAIN)
self._abort_if_unique_id_configured()
errors: dict[str, str] = {}
if user_input is not None:
scan_interval = int(user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
if scan_interval < MIN_SCAN_INTERVAL:
errors[CONF_SCAN_INTERVAL] = "scan_interval_too_low"
else:
return self.async_create_entry(
title=user_input.get(CONF_NAME, DEFAULT_NAME),
data=user_input,
)
schema = vol.Schema(
{
vol.Required(CONF_NAME, default=DEFAULT_NAME): str,
vol.Required(CONF_STORE_PATH, default=DEFAULT_STORE_PATH): str,
vol.Required(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): int,
}
)
return self.async_show_form(
step_id="user",
data_schema=schema,
errors=errors,
)
@staticmethod
def async_get_options_flow(config_entry: config_entries.ConfigEntry):
"""Create the options flow."""
return NotificationStoreOptionsFlow(config_entry)
class NotificationStoreOptionsFlow(config_entries.OptionsFlow):
"""Handle options for Notification Store."""
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input: dict[str, Any] | None = None):
"""Manage options."""
errors: dict[str, str] = {}
current = {**self.config_entry.data, **self.config_entry.options}
if user_input is not None:
scan_interval = int(user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL))
if scan_interval < MIN_SCAN_INTERVAL:
errors[CONF_SCAN_INTERVAL] = "scan_interval_too_low"
else:
return self.async_create_entry(title="", data=user_input)
schema = vol.Schema(
{
vol.Required(CONF_STORE_PATH, default=current.get(CONF_STORE_PATH, DEFAULT_STORE_PATH)): str,
vol.Required(CONF_SCAN_INTERVAL, default=current.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)): int,
}
)
return self.async_show_form(
step_id="init",
data_schema=schema,
errors=errors,
)