1 """Config flow for statistics."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from datetime
import timedelta
7 from typing
import Any, cast
9 import voluptuous
as vol
18 SchemaCommonFlowHandler,
19 SchemaConfigFlowHandler,
26 DurationSelectorConfig,
40 CONF_KEEP_LAST_SAMPLE,
44 CONF_SAMPLES_MAX_BUFFER_SIZE,
45 CONF_STATE_CHARACTERISTIC,
49 STATS_NUMERIC_SUPPORT,
55 """Return schema with state characteristics."""
57 split_entity_id(handler.options[CONF_ENTITY_ID])[0] == BINARY_SENSOR_DOMAIN
60 options = STATS_BINARY_SUPPORT
62 options = STATS_NUMERIC_SUPPORT
68 options=
list(options),
69 translation_key=CONF_STATE_CHARACTERISTIC,
71 mode=SelectSelectorMode.DROPDOWN,
79 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
81 """Validate options selected."""
83 user_input.get(CONF_SAMPLES_MAX_BUFFER_SIZE)
is None
84 and user_input.get(CONF_MAX_AGE)
is None
89 user_input.get(CONF_KEEP_LAST_SAMPLE)
is True
90 and user_input.get(CONF_MAX_AGE)
is None
94 handler.parent_handler._async_abort_entries_match({**handler.options, **user_input})
99 DATA_SCHEMA_SETUP = vol.Schema(
101 vol.Required(CONF_NAME, default=DEFAULT_NAME):
TextSelector(),
107 DATA_SCHEMA_OPTIONS = vol.Schema(
119 vol.Optional(CONF_PRECISION, default=DEFAULT_PRECISION):
NumberSelector(
127 schema=DATA_SCHEMA_SETUP,
128 next_step=
"state_characteristic",
131 schema=get_state_characteristics, next_step=
"options"
134 schema=DATA_SCHEMA_OPTIONS,
135 validate_user_input=validate_options,
136 preview=
"statistics",
142 validate_user_input=validate_options,
143 preview=
"statistics",
149 """Handle a config flow for Statistics."""
151 config_flow = CONFIG_FLOW
152 options_flow = OPTIONS_FLOW
155 """Return config entry title."""
156 return cast(str, options[CONF_NAME])
160 """Set up preview WS API."""
161 websocket_api.async_register_command(hass, ws_start_preview)
164 @websocket_api.websocket_command(
{
vol.Required("type"):
"statistics/start_preview",
165 vol.Required(
"flow_id"): str,
166 vol.Required(
"flow_type"): vol.Any(
"config_flow",
"options_flow"),
167 vol.Required(
"user_input"): dict,
170 @websocket_api.async_response
176 """Generate a preview."""
178 if msg[
"flow_type"] ==
"config_flow":
179 flow_status = hass.config_entries.flow.async_get(msg[
"flow_id"])
180 flow_sets = hass.config_entries.flow._handler_progress_index.get(
181 flow_status[
"handler"]
185 for active_flow
in flow_sets:
186 options = active_flow._common_handler.options
187 config_entry = hass.config_entries.async_get_entry(flow_status[
"handler"])
188 entity_id = options[CONF_ENTITY_ID]
189 name = options[CONF_NAME]
190 state_characteristic = options[CONF_STATE_CHARACTERISTIC]
192 flow_status = hass.config_entries.options.async_get(msg[
"flow_id"])
193 config_entry = hass.config_entries.async_get_entry(flow_status[
"handler"])
196 entity_id = config_entry.options[CONF_ENTITY_ID]
197 name = config_entry.options[CONF_NAME]
198 state_characteristic = config_entry.options[CONF_STATE_CHARACTERISTIC]
201 def async_preview_updated(state: str, attributes: Mapping[str, Any]) ->
None:
202 """Forward config entry state events to websocket."""
203 connection.send_message(
204 websocket_api.event_message(
205 msg[
"id"], {
"attributes": attributes,
"state": state}
209 sampling_size = msg[
"user_input"].
get(CONF_SAMPLES_MAX_BUFFER_SIZE)
211 sampling_size =
int(sampling_size)
214 if max_age_input := msg[
"user_input"].
get(CONF_MAX_AGE):
216 hours=max_age_input[
"hours"],
217 minutes=max_age_input[
"minutes"],
218 seconds=max_age_input[
"seconds"],
225 state_characteristic,
228 msg[
"user_input"].
get(CONF_KEEP_LAST_SAMPLE),
229 msg[
"user_input"].
get(CONF_PRECISION),
230 msg[
"user_input"].
get(CONF_PERCENTILE),
232 preview_entity.hass = hass
234 connection.send_result(msg[
"id"])
235 connection.subscriptions[msg[
"id"]] = await preview_entity.async_start_preview(
236 async_preview_updated
238
None async_setup_preview(HomeAssistant hass)
str async_config_entry_title(self, Mapping[str, Any] options)
web.Response get(self, web.Request request, str config_key)
None ws_start_preview(HomeAssistant hass, websocket_api.ActiveConnection connection, dict[str, Any] msg)
dict[str, Any] validate_options(SchemaCommonFlowHandler handler, dict[str, Any] user_input)
vol.Schema get_state_characteristics(SchemaCommonFlowHandler handler)
tuple[str, str] split_entity_id(str entity_id)