Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for System Monitor."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 import voluptuous as vol
9 
10 from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
11 from homeassistant.config_entries import ConfigFlowResult
12 from homeassistant.core import callback
13 from homeassistant.helpers import entity_registry as er
15  SchemaCommonFlowHandler,
16  SchemaConfigFlowHandler,
17  SchemaFlowFormStep,
18 )
20  SelectSelector,
21  SelectSelectorConfig,
22  SelectSelectorMode,
23 )
24 from homeassistant.util import slugify
25 
26 from .const import CONF_PROCESS, DOMAIN
27 from .util import get_all_running_processes
28 
29 
31  handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
32 ) -> dict[str, Any]:
33  """Validate sensor input."""
34  # Standard behavior is to merge the result with the options.
35  # In this case, we want to add a sub-item so we update the options directly.
36  sensors: dict[str, list] = handler.options.setdefault(BINARY_SENSOR_DOMAIN, {})
37  processes = sensors.setdefault(CONF_PROCESS, [])
38  previous_processes = processes.copy()
39  processes.clear()
40  processes.extend(user_input[CONF_PROCESS])
41 
42  entity_registry = er.async_get(handler.parent_handler.hass)
43  for process in previous_processes:
44  if process not in processes and (
45  entity_id := entity_registry.async_get_entity_id(
46  BINARY_SENSOR_DOMAIN, DOMAIN, slugify(f"binary_process_{process}")
47  )
48  ):
49  entity_registry.async_remove(entity_id)
50 
51  return {}
52 
53 
54 async def get_sensor_setup_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
55  """Return process sensor setup schema."""
56  hass = handler.parent_handler.hass
57  processes = list(await hass.async_add_executor_job(get_all_running_processes, hass))
58  return vol.Schema(
59  {
60  vol.Required(CONF_PROCESS): SelectSelector(
62  options=processes,
63  multiple=True,
64  custom_value=True,
65  mode=SelectSelectorMode.DROPDOWN,
66  sort=True,
67  )
68  )
69  }
70  )
71 
72 
73 async def get_suggested_value(handler: SchemaCommonFlowHandler) -> dict[str, Any]:
74  """Return suggested values for sensor setup."""
75  sensors: dict[str, list] = handler.options.get(BINARY_SENSOR_DOMAIN, {})
76  processes: list[str] = sensors.get(CONF_PROCESS, [])
77  return {CONF_PROCESS: processes}
78 
79 
80 CONFIG_FLOW = {
81  "user": SchemaFlowFormStep(schema=vol.Schema({})),
82 }
83 OPTIONS_FLOW = {
84  "init": SchemaFlowFormStep(
85  get_sensor_setup_schema,
86  suggested_values=get_suggested_value,
87  validate_user_input=validate_sensor_setup,
88  )
89 }
90 
91 
93  """Handle a config flow for System Monitor."""
94 
95  config_flow = CONFIG_FLOW
96  options_flow = OPTIONS_FLOW
97  VERSION = 1
98  MINOR_VERSION = 3
99 
100  def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
101  """Return config entry title."""
102  return "System Monitor"
103 
104  @callback
106  self, data: Mapping[str, Any], **kwargs: Any
107  ) -> ConfigFlowResult:
108  """Finish config flow and create a config entry."""
109  if self._async_current_entries_async_current_entries():
110  return self.async_abortasync_abortasync_abort(reason="already_configured")
111  return super().async_create_entry(data, **kwargs)
ConfigFlowResult async_create_entry(self, Mapping[str, Any] data, **Any kwargs)
Definition: config_flow.py:107
list[ConfigEntry] _async_current_entries(self, bool|None include_ignore=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] validate_sensor_setup(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
Definition: config_flow.py:32
vol.Schema get_sensor_setup_schema(SchemaCommonFlowHandler handler)
Definition: config_flow.py:54
dict[str, Any] get_suggested_value(SchemaCommonFlowHandler handler)
Definition: config_flow.py:73