1 """Adds config flow for Scrape integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from typing
import Any, cast
9 import voluptuous
as vol
16 DOMAIN
as SENSOR_DOMAIN,
32 CONF_UNIT_OF_MEASUREMENT,
36 HTTP_BASIC_AUTHENTICATION,
37 HTTP_DIGEST_AUTHENTICATION,
43 SchemaCommonFlowHandler,
44 SchemaConfigFlowHandler,
65 from .
import COMBINED_SCHEMA
86 options=[HTTP_BASIC_AUTHENTICATION, HTTP_DIGEST_AUTHENTICATION],
87 mode=SelectSelectorMode.DROPDOWN,
95 vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL):
BooleanSelector(),
96 vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT):
NumberSelector(
99 vol.Optional(CONF_ENCODING, default=DEFAULT_ENCODING):
TextSelector(),
113 cls.value
for cls
in SensorDeviceClass
if cls != SensorDeviceClass.ENUM
115 mode=SelectSelectorMode.DROPDOWN,
116 translation_key=
"device_class",
122 options=[cls.value
for cls
in SensorStateClass],
123 mode=SelectSelectorMode.DROPDOWN,
124 translation_key=
"state_class",
130 options=[cls.value
for cls
in UnitOfTemperature],
132 mode=SelectSelectorMode.DROPDOWN,
133 translation_key=
"unit_of_measurement",
141 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
143 """Validate rest setup."""
148 await rest.async_update()
149 except Exception
as err:
151 if rest.data
is None:
157 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
159 """Validate sensor input."""
160 user_input[CONF_INDEX] =
int(user_input[CONF_INDEX])
161 user_input[CONF_UNIQUE_ID] =
str(uuid.uuid1())
165 sensors: list[dict[str, Any]] = handler.options.setdefault(SENSOR_DOMAIN, [])
166 sensors.append(user_input)
171 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
173 """Store sensor index in flow state."""
174 handler.flow_state[
"_idx"] =
int(user_input[CONF_INDEX])
179 """Return schema for selecting a sensor."""
182 vol.Required(CONF_INDEX): vol.In(
184 str(index): config[CONF_NAME]
185 for index, config
in enumerate(handler.options[SENSOR_DOMAIN])
193 handler: SchemaCommonFlowHandler,
195 """Return suggested values for sensor editing."""
196 idx: int = handler.flow_state[
"_idx"]
197 return dict(handler.options[SENSOR_DOMAIN][idx])
201 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
203 """Update edited sensor."""
204 user_input[CONF_INDEX] =
int(user_input[CONF_INDEX])
209 idx: int = handler.flow_state[
"_idx"]
210 handler.options[SENSOR_DOMAIN][idx].
update(user_input)
211 for key
in DATA_SCHEMA_EDIT_SENSOR.schema:
212 if isinstance(key, vol.Optional)
and key
not in user_input:
214 handler.options[SENSOR_DOMAIN][idx].pop(key,
None)
219 """Return schema for sensor removal."""
222 vol.Required(CONF_INDEX): cv.multi_select(
224 str(index): config[CONF_NAME]
225 for index, config
in enumerate(handler.options[SENSOR_DOMAIN])
233 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
235 """Validate remove sensor."""
236 removed_indexes: set[str] = set(user_input[CONF_INDEX])
240 entity_registry = er.async_get(handler.parent_handler.hass)
241 sensors: list[dict[str, Any]] = []
242 sensor: dict[str, Any]
243 for index, sensor
in enumerate(handler.options[SENSOR_DOMAIN]):
244 if str(index)
not in removed_indexes:
245 sensors.append(sensor)
246 elif entity_id := entity_registry.async_get_entity_id(
247 SENSOR_DOMAIN, DOMAIN, sensor[CONF_UNIQUE_ID]
249 entity_registry.async_remove(entity_id)
250 handler.options[SENSOR_DOMAIN] = sensors
254 DATA_SCHEMA_RESOURCE = vol.Schema(RESOURCE_SETUP)
255 DATA_SCHEMA_EDIT_SENSOR = vol.Schema(SENSOR_SETUP)
256 DATA_SCHEMA_SENSOR = vol.Schema(
258 vol.Optional(CONF_NAME, default=DEFAULT_NAME):
TextSelector(),
265 schema=DATA_SCHEMA_RESOURCE,
267 validate_user_input=validate_rest_setup,
270 schema=DATA_SCHEMA_SENSOR,
271 validate_user_input=validate_sensor_setup,
276 [
"resource",
"add_sensor",
"select_edit_sensor",
"remove_sensor"]
279 DATA_SCHEMA_RESOURCE,
280 validate_user_input=validate_rest_setup,
284 suggested_values=
None,
285 validate_user_input=validate_sensor_setup,
288 get_select_sensor_schema,
289 suggested_values=
None,
290 validate_user_input=validate_select_sensor,
291 next_step=
"edit_sensor",
294 DATA_SCHEMA_EDIT_SENSOR,
295 suggested_values=get_edit_sensor_suggested_values,
296 validate_user_input=validate_sensor_edit,
299 get_remove_sensor_schema,
300 suggested_values=
None,
301 validate_user_input=validate_remove_sensor,
307 """Handle a config flow for Scrape."""
309 config_flow = CONFIG_FLOW
310 options_flow = OPTIONS_FLOW
313 """Return config entry title."""
314 return cast(str, options[CONF_RESOURCE])
str async_config_entry_title(self, Mapping[str, Any] options)
IssData update(pyiss.ISS iss)
RestData create_rest_data_from_config(HomeAssistant hass, ConfigType config)
vol.Schema get_remove_sensor_schema(SchemaCommonFlowHandler handler)
dict[str, Any] validate_sensor_setup(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
dict[str, Any] validate_remove_sensor(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
dict[str, Any] validate_select_sensor(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
dict[str, Any] validate_sensor_edit(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
dict[str, Any] get_edit_sensor_suggested_values(SchemaCommonFlowHandler handler)
dict[str, Any] validate_rest_setup(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
vol.Schema get_select_sensor_schema(SchemaCommonFlowHandler handler)
HomeAssistant async_get_hass()