2024-05-29 22:00:57 +00:00
|
|
|
import logging
|
|
|
|
|
import voluptuous as vol
|
2025-04-15 00:43:09 +00:00
|
|
|
from .const import (
|
|
|
|
|
DOMAIN,
|
|
|
|
|
CONF_NAME,
|
|
|
|
|
CONF_TOPIC,
|
|
|
|
|
CONF_QOS,
|
|
|
|
|
AVAILABLE_STATES,
|
|
|
|
|
CONF_AWAKE_DURATION,
|
|
|
|
|
CONF_SLEEP_DURATION,
|
|
|
|
|
CONF_AWAKE_STATES,
|
|
|
|
|
CONF_SLEEP_STATES,
|
|
|
|
|
DEFAULT_AWAKE_DURATION,
|
|
|
|
|
DEFAULT_SLEEP_DURATION,
|
|
|
|
|
DEFAULT_AWAKE_STATES,
|
|
|
|
|
DEFAULT_SLEEP_STATES,
|
|
|
|
|
CONF_NOTIFY_TARGET,
|
|
|
|
|
)
|
2024-05-29 22:00:57 +00:00
|
|
|
from homeassistant import config_entries
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
|
from voluptuous import Schema, Required, In, Optional
|
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
|
2025-04-15 00:43:09 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2024-05-29 22:00:57 +00:00
|
|
|
|
|
|
|
|
class MyConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
2025-04-15 00:43:09 +00:00
|
|
|
"""Handle a config flow for SAAS."""
|
|
|
|
|
VERSION = 2
|
2024-05-29 22:00:57 +00:00
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
|
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
2025-04-15 00:43:09 +00:00
|
|
|
"""Handle the initial step."""
|
2024-05-29 22:00:57 +00:00
|
|
|
errors = {}
|
2025-04-15 00:43:09 +00:00
|
|
|
|
|
|
|
|
# Build notify targets list
|
|
|
|
|
notify_services = self.hass.services.async_services().get("notify", {})
|
|
|
|
|
notify_targets = {
|
|
|
|
|
key.replace("mobile_app_", "").title(): key
|
|
|
|
|
for key in notify_services.keys()
|
|
|
|
|
if key.startswith("mobile_app_")
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 22:00:57 +00:00
|
|
|
if user_input is not None:
|
2025-04-15 00:43:09 +00:00
|
|
|
# Map back the chosen label to service name
|
|
|
|
|
if user_input.get(CONF_NOTIFY_TARGET):
|
|
|
|
|
user_input[CONF_NOTIFY_TARGET] = notify_targets.get(user_input[CONF_NOTIFY_TARGET])
|
|
|
|
|
if not user_input.get(CONF_NAME):
|
2024-05-29 22:00:57 +00:00
|
|
|
errors[CONF_NAME] = "required"
|
|
|
|
|
if not errors:
|
|
|
|
|
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)
|
2025-04-15 00:43:09 +00:00
|
|
|
|
2024-05-29 22:00:57 +00:00
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="user",
|
|
|
|
|
data_schema=Schema(
|
|
|
|
|
{
|
|
|
|
|
Required(CONF_NAME): str,
|
|
|
|
|
Required(CONF_TOPIC): str,
|
|
|
|
|
Required(CONF_QOS, default=0): In([0, 1, 2]),
|
|
|
|
|
Required(CONF_AWAKE_DURATION, default=DEFAULT_AWAKE_DURATION): int,
|
|
|
|
|
Required(CONF_SLEEP_DURATION, default=DEFAULT_SLEEP_DURATION): int,
|
|
|
|
|
Required(CONF_AWAKE_STATES, default=DEFAULT_AWAKE_STATES): cv.multi_select(AVAILABLE_STATES),
|
|
|
|
|
Required(CONF_SLEEP_STATES, default=DEFAULT_SLEEP_STATES): cv.multi_select(AVAILABLE_STATES),
|
2025-04-15 00:43:09 +00:00
|
|
|
Optional(CONF_NOTIFY_TARGET): vol.In(list(notify_targets.keys())),
|
2024-05-29 22:00:57 +00:00
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
errors=errors,
|
|
|
|
|
)
|
|
|
|
|
|
2025-04-15 00:43:09 +00:00
|
|
|
async def async_migrate_entry(self, hass, entry):
|
|
|
|
|
"""Migrate old config entries to the new schema."""
|
|
|
|
|
_LOGGER.debug("Migrating config entry %s from version %s", entry.entry_id, entry.version)
|
|
|
|
|
data = {**entry.data}
|
|
|
|
|
options = {**entry.options}
|
|
|
|
|
|
|
|
|
|
# If you renamed keys in entry.data/options, do it here when entry.version == 1
|
|
|
|
|
# e.g.:
|
|
|
|
|
# if entry.version == 1:
|
|
|
|
|
# data["topic_template"] = data.pop("topic")
|
|
|
|
|
# entry.version = 2
|
|
|
|
|
|
|
|
|
|
# For no data changes, just bump the version:
|
|
|
|
|
entry.version = self.VERSION
|
|
|
|
|
hass.config_entries.async_update_entry(entry, data=data, options=options)
|
|
|
|
|
_LOGGER.info("Migrated config entry %s to version %s", entry.entry_id, entry.version)
|
|
|
|
|
return True
|
|
|
|
|
|
2024-05-29 22:00:57 +00:00
|
|
|
@staticmethod
|
|
|
|
|
@callback
|
2025-04-15 00:43:09 +00:00
|
|
|
def async_get_options_flow(entry):
|
|
|
|
|
"""Get the options flow handler."""
|
|
|
|
|
return OptionsFlowHandler(entry)
|
|
|
|
|
|
2024-05-29 22:00:57 +00:00
|
|
|
|
|
|
|
|
class OptionsFlowHandler(config_entries.OptionsFlow):
|
2025-04-15 00:43:09 +00:00
|
|
|
"""Handle SAAS options."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, entry):
|
2024-05-29 22:00:57 +00:00
|
|
|
"""Initialize options flow."""
|
2025-04-15 00:43:09 +00:00
|
|
|
super().__init__()
|
|
|
|
|
self._config_entry = entry # use private attribute
|
2024-05-29 22:00:57 +00:00
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
|
"""Manage the options."""
|
2025-04-15 00:43:09 +00:00
|
|
|
# Load current options or fall back to data
|
|
|
|
|
current = self._config_entry.options.copy()
|
|
|
|
|
for key in [
|
|
|
|
|
CONF_NAME,
|
|
|
|
|
CONF_TOPIC,
|
|
|
|
|
CONF_QOS,
|
|
|
|
|
CONF_AWAKE_DURATION,
|
|
|
|
|
CONF_SLEEP_DURATION,
|
|
|
|
|
CONF_AWAKE_STATES,
|
|
|
|
|
CONF_SLEEP_STATES,
|
|
|
|
|
CONF_NOTIFY_TARGET,
|
|
|
|
|
]:
|
|
|
|
|
if key not in current and key in self._config_entry.data:
|
|
|
|
|
current[key] = self._config_entry.data[key]
|
|
|
|
|
|
|
|
|
|
# Build notify targets list
|
|
|
|
|
notify_services = self.hass.services.async_services().get("notify", {})
|
|
|
|
|
notify_targets = {
|
|
|
|
|
key.replace("mobile_app_", "").title(): key
|
|
|
|
|
for key in notify_services.keys()
|
|
|
|
|
if key.startswith("mobile_app_")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
|
step_id="init",
|
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
|
{
|
|
|
|
|
Required(CONF_NAME, default=current.get(CONF_NAME, "")): str,
|
|
|
|
|
Required(CONF_TOPIC, default=current.get(CONF_TOPIC, "")): str,
|
|
|
|
|
Required(CONF_QOS, default=current.get(CONF_QOS, 0)): In([0, 1, 2]),
|
|
|
|
|
Required(CONF_AWAKE_DURATION, default=current.get(CONF_AWAKE_DURATION, DEFAULT_AWAKE_DURATION)): int,
|
|
|
|
|
Required(CONF_SLEEP_DURATION, default=current.get(CONF_SLEEP_DURATION, DEFAULT_SLEEP_DURATION)): int,
|
|
|
|
|
Required(CONF_AWAKE_STATES, default=current.get(CONF_AWAKE_STATES, DEFAULT_AWAKE_STATES)): cv.multi_select(AVAILABLE_STATES),
|
|
|
|
|
Required(CONF_SLEEP_STATES, default=current.get(CONF_SLEEP_STATES, DEFAULT_SLEEP_STATES)): cv.multi_select(AVAILABLE_STATES),
|
|
|
|
|
Optional(CONF_NOTIFY_TARGET, default=current.get(CONF_NOTIFY_TARGET, "")): vol.In(list(notify_targets.keys())),
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
errors={},
|
|
|
|
|
)
|