|
| 1 | +"""Config flow for Datadog.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from datadog import DogStatsd |
| 6 | +import voluptuous as vol |
| 7 | + |
| 8 | +from homeassistant.config_entries import ( |
| 9 | + ConfigEntry, |
| 10 | + ConfigFlow, |
| 11 | + ConfigFlowResult, |
| 12 | + OptionsFlow, |
| 13 | +) |
| 14 | +from homeassistant.const import CONF_HOST, CONF_PORT, CONF_PREFIX |
| 15 | +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant, callback |
| 16 | +from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue |
| 17 | + |
| 18 | +from .const import ( |
| 19 | + CONF_RATE, |
| 20 | + DEFAULT_HOST, |
| 21 | + DEFAULT_PORT, |
| 22 | + DEFAULT_PREFIX, |
| 23 | + DEFAULT_RATE, |
| 24 | + DOMAIN, |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class DatadogConfigFlow(ConfigFlow, domain=DOMAIN): |
| 29 | + """Handle a config flow for Datadog.""" |
| 30 | + |
| 31 | + VERSION = 1 |
| 32 | + |
| 33 | + async def async_step_user( |
| 34 | + self, user_input: dict[str, Any] | None = None |
| 35 | + ) -> ConfigFlowResult: |
| 36 | + """Handle user config flow.""" |
| 37 | + errors: dict[str, str] = {} |
| 38 | + if user_input: |
| 39 | + # Validate connection to Datadog Agent |
| 40 | + success = await validate_datadog_connection( |
| 41 | + self.hass, |
| 42 | + user_input, |
| 43 | + ) |
| 44 | + self._async_abort_entries_match( |
| 45 | + {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]} |
| 46 | + ) |
| 47 | + if not success: |
| 48 | + errors["base"] = "cannot_connect" |
| 49 | + else: |
| 50 | + return self.async_create_entry( |
| 51 | + title=f"Datadog {user_input['host']}", |
| 52 | + data={ |
| 53 | + CONF_HOST: user_input[CONF_HOST], |
| 54 | + CONF_PORT: user_input[CONF_PORT], |
| 55 | + }, |
| 56 | + options={ |
| 57 | + CONF_PREFIX: user_input[CONF_PREFIX], |
| 58 | + CONF_RATE: user_input[CONF_RATE], |
| 59 | + }, |
| 60 | + ) |
| 61 | + |
| 62 | + return self.async_show_form( |
| 63 | + step_id="user", |
| 64 | + data_schema=vol.Schema( |
| 65 | + { |
| 66 | + vol.Required(CONF_HOST, default=DEFAULT_HOST): str, |
| 67 | + vol.Required(CONF_PORT, default=DEFAULT_PORT): int, |
| 68 | + vol.Required(CONF_PREFIX, default=DEFAULT_PREFIX): str, |
| 69 | + vol.Required(CONF_RATE, default=DEFAULT_RATE): int, |
| 70 | + } |
| 71 | + ), |
| 72 | + errors=errors, |
| 73 | + ) |
| 74 | + |
| 75 | + async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: |
| 76 | + """Handle import from configuration.yaml.""" |
| 77 | + # Check for duplicates |
| 78 | + self._async_abort_entries_match( |
| 79 | + {CONF_HOST: user_input[CONF_HOST], CONF_PORT: user_input[CONF_PORT]} |
| 80 | + ) |
| 81 | + |
| 82 | + result = await self.async_step_user(user_input) |
| 83 | + |
| 84 | + if errors := result.get("errors"): |
| 85 | + await deprecate_yaml_issue(self.hass, False) |
| 86 | + return self.async_abort(reason=errors["base"]) |
| 87 | + |
| 88 | + await deprecate_yaml_issue(self.hass, True) |
| 89 | + return result |
| 90 | + |
| 91 | + @staticmethod |
| 92 | + @callback |
| 93 | + def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow: |
| 94 | + """Get the options flow handler.""" |
| 95 | + return DatadogOptionsFlowHandler() |
| 96 | + |
| 97 | + |
| 98 | +class DatadogOptionsFlowHandler(OptionsFlow): |
| 99 | + """Handle Datadog options.""" |
| 100 | + |
| 101 | + async def async_step_init( |
| 102 | + self, user_input: dict[str, Any] | None = None |
| 103 | + ) -> ConfigFlowResult: |
| 104 | + """Manage the Datadog options.""" |
| 105 | + errors: dict[str, str] = {} |
| 106 | + data = self.config_entry.data |
| 107 | + options = self.config_entry.options |
| 108 | + |
| 109 | + if user_input is None: |
| 110 | + user_input = {} |
| 111 | + |
| 112 | + success = await validate_datadog_connection( |
| 113 | + self.hass, |
| 114 | + {**data, **user_input}, |
| 115 | + ) |
| 116 | + if success: |
| 117 | + return self.async_create_entry( |
| 118 | + data={ |
| 119 | + CONF_PREFIX: user_input[CONF_PREFIX], |
| 120 | + CONF_RATE: user_input[CONF_RATE], |
| 121 | + } |
| 122 | + ) |
| 123 | + |
| 124 | + errors["base"] = "cannot_connect" |
| 125 | + return self.async_show_form( |
| 126 | + step_id="init", |
| 127 | + data_schema=vol.Schema( |
| 128 | + { |
| 129 | + vol.Required(CONF_PREFIX, default=options[CONF_PREFIX]): str, |
| 130 | + vol.Required(CONF_RATE, default=options[CONF_RATE]): int, |
| 131 | + } |
| 132 | + ), |
| 133 | + errors=errors, |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +async def validate_datadog_connection( |
| 138 | + hass: HomeAssistant, user_input: dict[str, Any] |
| 139 | +) -> bool: |
| 140 | + """Attempt to send a test metric to the Datadog agent.""" |
| 141 | + try: |
| 142 | + client = DogStatsd(user_input[CONF_HOST], user_input[CONF_PORT]) |
| 143 | + await hass.async_add_executor_job(client.increment, "connection_test") |
| 144 | + except (OSError, ValueError): |
| 145 | + return False |
| 146 | + else: |
| 147 | + return True |
| 148 | + |
| 149 | + |
| 150 | +async def deprecate_yaml_issue( |
| 151 | + hass: HomeAssistant, |
| 152 | + import_success: bool, |
| 153 | +) -> None: |
| 154 | + """Create an issue to deprecate YAML config.""" |
| 155 | + if import_success: |
| 156 | + async_create_issue( |
| 157 | + hass, |
| 158 | + HOMEASSISTANT_DOMAIN, |
| 159 | + f"deprecated_yaml_{DOMAIN}", |
| 160 | + is_fixable=False, |
| 161 | + issue_domain=DOMAIN, |
| 162 | + breaks_in_ha_version="2026.2.0", |
| 163 | + severity=IssueSeverity.WARNING, |
| 164 | + translation_key="deprecated_yaml", |
| 165 | + translation_placeholders={ |
| 166 | + "domain": DOMAIN, |
| 167 | + "integration_title": "Datadog", |
| 168 | + }, |
| 169 | + ) |
| 170 | + else: |
| 171 | + async_create_issue( |
| 172 | + hass, |
| 173 | + DOMAIN, |
| 174 | + "deprecated_yaml_import_connection_error", |
| 175 | + breaks_in_ha_version="2026.2.0", |
| 176 | + is_fixable=False, |
| 177 | + issue_domain=DOMAIN, |
| 178 | + severity=IssueSeverity.WARNING, |
| 179 | + translation_key="deprecated_yaml_import_connection_error", |
| 180 | + translation_placeholders={ |
| 181 | + "domain": DOMAIN, |
| 182 | + "integration_title": "Datadog", |
| 183 | + "url": f"/config/integrations/dashboard/add?domain={DOMAIN}", |
| 184 | + }, |
| 185 | + ) |
0 commit comments