2020-01-03 20:34:20 +00:00
|
|
|
"""Config flow for sentry integration."""
|
2020-08-20 09:37:27 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-03 20:34:20 +00:00
|
|
|
import logging
|
2021-03-18 13:31:38 +00:00
|
|
|
from typing import Any
|
2020-01-03 20:34:20 +00:00
|
|
|
|
|
|
|
from sentry_sdk.utils import BadDsn, Dsn
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-08-20 09:37:27 +00:00
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.core import callback
|
2021-04-29 11:40:51 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2020-08-20 09:37:27 +00:00
|
|
|
|
2021-03-30 04:02:56 +00:00
|
|
|
from .const import (
|
2020-08-20 09:37:27 +00:00
|
|
|
CONF_DSN,
|
|
|
|
CONF_ENVIRONMENT,
|
|
|
|
CONF_EVENT_CUSTOM_COMPONENTS,
|
|
|
|
CONF_EVENT_HANDLED,
|
|
|
|
CONF_EVENT_THIRD_PARTY_PACKAGES,
|
|
|
|
CONF_LOGGING_EVENT_LEVEL,
|
|
|
|
CONF_LOGGING_LEVEL,
|
|
|
|
CONF_TRACING,
|
|
|
|
CONF_TRACING_SAMPLE_RATE,
|
|
|
|
DEFAULT_LOGGING_EVENT_LEVEL,
|
|
|
|
DEFAULT_LOGGING_LEVEL,
|
|
|
|
DEFAULT_TRACING_SAMPLE_RATE,
|
|
|
|
DOMAIN,
|
|
|
|
LOGGING_LEVELS,
|
|
|
|
)
|
2020-01-03 20:34:20 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DATA_SCHEMA = vol.Schema({vol.Required(CONF_DSN): str})
|
|
|
|
|
|
|
|
|
2020-08-20 09:37:27 +00:00
|
|
|
class SentryConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
2020-01-03 20:34:20 +00:00
|
|
|
"""Handle a Sentry config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2020-08-20 09:37:27 +00:00
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(
|
|
|
|
config_entry: config_entries.ConfigEntry,
|
|
|
|
) -> SentryOptionsFlow:
|
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return SentryOptionsFlow(config_entry)
|
|
|
|
|
|
|
|
async def async_step_user(
|
2021-03-18 13:31:38 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2020-01-03 20:34:20 +00:00
|
|
|
"""Handle a user config flow."""
|
|
|
|
if self._async_current_entries():
|
2020-08-20 09:37:27 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
2020-01-03 20:34:20 +00:00
|
|
|
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
2020-08-20 09:37:27 +00:00
|
|
|
Dsn(user_input["dsn"])
|
2020-01-03 20:34:20 +00:00
|
|
|
except BadDsn:
|
|
|
|
errors["base"] = "bad_dsn"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
2020-08-20 09:37:27 +00:00
|
|
|
else:
|
|
|
|
return self.async_create_entry(title="Sentry", data=user_input)
|
2020-01-03 20:34:20 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
|
|
|
)
|
|
|
|
|
2020-08-20 09:37:27 +00:00
|
|
|
|
|
|
|
class SentryOptionsFlow(config_entries.OptionsFlow):
|
|
|
|
"""Handle Sentry options."""
|
|
|
|
|
2021-05-20 13:58:17 +00:00
|
|
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
2020-08-20 09:37:27 +00:00
|
|
|
"""Initialize Sentry options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
|
|
|
|
async def async_step_init(
|
2021-03-18 13:31:38 +00:00
|
|
|
self, user_input: dict[str, Any] | None = None
|
2021-04-29 11:40:51 +00:00
|
|
|
) -> FlowResult:
|
2020-08-20 09:37:27 +00:00
|
|
|
"""Manage Sentry options."""
|
|
|
|
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(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LOGGING_EVENT_LEVEL,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_LOGGING_EVENT_LEVEL, DEFAULT_LOGGING_EVENT_LEVEL
|
|
|
|
),
|
|
|
|
): vol.In(LOGGING_LEVELS),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_LOGGING_LEVEL,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_LOGGING_LEVEL, DEFAULT_LOGGING_LEVEL
|
|
|
|
),
|
|
|
|
): vol.In(LOGGING_LEVELS),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_ENVIRONMENT,
|
|
|
|
default=self.config_entry.options.get(CONF_ENVIRONMENT),
|
|
|
|
): str,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_EVENT_HANDLED,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_EVENT_HANDLED, False
|
|
|
|
),
|
|
|
|
): bool,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_EVENT_CUSTOM_COMPONENTS,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_EVENT_CUSTOM_COMPONENTS, False
|
|
|
|
),
|
|
|
|
): bool,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_EVENT_THIRD_PARTY_PACKAGES,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_EVENT_THIRD_PARTY_PACKAGES, False
|
|
|
|
),
|
|
|
|
): bool,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_TRACING,
|
|
|
|
default=self.config_entry.options.get(CONF_TRACING, False),
|
|
|
|
): bool,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_TRACING_SAMPLE_RATE,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_TRACING_SAMPLE_RATE, DEFAULT_TRACING_SAMPLE_RATE
|
|
|
|
),
|
|
|
|
): vol.All(vol.Coerce(float), vol.Range(min=0.0, max=1.0)),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|