Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for sentry integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from sentry_sdk.utils import BadDsn, Dsn
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.core import callback
18 
19 from .const import (
20  CONF_DSN,
21  CONF_ENVIRONMENT,
22  CONF_EVENT_CUSTOM_COMPONENTS,
23  CONF_EVENT_HANDLED,
24  CONF_EVENT_THIRD_PARTY_PACKAGES,
25  CONF_LOGGING_EVENT_LEVEL,
26  CONF_LOGGING_LEVEL,
27  CONF_TRACING,
28  CONF_TRACING_SAMPLE_RATE,
29  DEFAULT_LOGGING_EVENT_LEVEL,
30  DEFAULT_LOGGING_LEVEL,
31  DEFAULT_TRACING_SAMPLE_RATE,
32  DOMAIN,
33  LOGGING_LEVELS,
34 )
35 
36 _LOGGER = logging.getLogger(__name__)
37 
38 DATA_SCHEMA = vol.Schema({vol.Required(CONF_DSN): str})
39 
40 
41 class SentryConfigFlow(ConfigFlow, domain=DOMAIN):
42  """Handle a Sentry config flow."""
43 
44  VERSION = 1
45 
46  @staticmethod
47  @callback
49  config_entry: ConfigEntry,
50  ) -> SentryOptionsFlow:
51  """Get the options flow for this handler."""
52  return SentryOptionsFlow()
53 
54  async def async_step_user(
55  self, user_input: dict[str, Any] | None = None
56  ) -> ConfigFlowResult:
57  """Handle a user config flow."""
58  if self._async_current_entries_async_current_entries():
59  return self.async_abortasync_abortasync_abort(reason="single_instance_allowed")
60 
61  errors = {}
62  if user_input is not None:
63  try:
64  Dsn(user_input["dsn"])
65  except BadDsn:
66  errors["base"] = "bad_dsn"
67  except Exception:
68  _LOGGER.exception("Unexpected exception")
69  errors["base"] = "unknown"
70  else:
71  return self.async_create_entryasync_create_entryasync_create_entry(title="Sentry", data=user_input)
72 
73  return self.async_show_formasync_show_formasync_show_form(
74  step_id="user", data_schema=DATA_SCHEMA, errors=errors
75  )
76 
77 
79  """Handle Sentry options."""
80 
81  async def async_step_init(
82  self, user_input: dict[str, Any] | None = None
83  ) -> ConfigFlowResult:
84  """Manage Sentry options."""
85  if user_input is not None:
86  return self.async_create_entryasync_create_entry(title="", data=user_input)
87 
88  return self.async_show_formasync_show_form(
89  step_id="init",
90  data_schema=vol.Schema(
91  {
92  vol.Optional(
93  CONF_LOGGING_EVENT_LEVEL,
94  default=self.config_entryconfig_entryconfig_entry.options.get(
95  CONF_LOGGING_EVENT_LEVEL, DEFAULT_LOGGING_EVENT_LEVEL
96  ),
97  ): vol.In(LOGGING_LEVELS),
98  vol.Optional(
99  CONF_LOGGING_LEVEL,
100  default=self.config_entryconfig_entryconfig_entry.options.get(
101  CONF_LOGGING_LEVEL, DEFAULT_LOGGING_LEVEL
102  ),
103  ): vol.In(LOGGING_LEVELS),
104  vol.Optional(
105  CONF_ENVIRONMENT,
106  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_ENVIRONMENT),
107  ): str,
108  vol.Optional(
109  CONF_EVENT_HANDLED,
110  default=self.config_entryconfig_entryconfig_entry.options.get(
111  CONF_EVENT_HANDLED, False
112  ),
113  ): bool,
114  vol.Optional(
115  CONF_EVENT_CUSTOM_COMPONENTS,
116  default=self.config_entryconfig_entryconfig_entry.options.get(
117  CONF_EVENT_CUSTOM_COMPONENTS, False
118  ),
119  ): bool,
120  vol.Optional(
121  CONF_EVENT_THIRD_PARTY_PACKAGES,
122  default=self.config_entryconfig_entryconfig_entry.options.get(
123  CONF_EVENT_THIRD_PARTY_PACKAGES, False
124  ),
125  ): bool,
126  vol.Optional(
127  CONF_TRACING,
128  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_TRACING, False),
129  ): bool,
130  vol.Optional(
131  CONF_TRACING_SAMPLE_RATE,
132  default=self.config_entryconfig_entryconfig_entry.options.get(
133  CONF_TRACING_SAMPLE_RATE, DEFAULT_TRACING_SAMPLE_RATE
134  ),
135  ): vol.All(vol.Coerce(float), vol.Range(min=0.0, max=1.0)),
136  }
137  ),
138  )
SentryOptionsFlow async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:50
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:56
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:83
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
None config_entry(self, ConfigEntry value)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)