1 """Config flow for Threshold integration."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
8 import voluptuous
as vol
17 SchemaCommonFlowHandler,
18 SchemaConfigFlowHandler,
23 from .binary_sensor
import ThresholdSensor
24 from .const
import CONF_HYSTERESIS, CONF_LOWER, CONF_UPPER, DEFAULT_HYSTERESIS, DOMAIN
28 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
30 """Validate the threshold mode, and set limits to None if not set."""
31 if CONF_LOWER
not in user_input
and CONF_UPPER
not in user_input:
33 return {CONF_LOWER:
None, CONF_UPPER:
None, **user_input}
36 OPTIONS_SCHEMA = vol.Schema(
39 CONF_HYSTERESIS, default=DEFAULT_HYSTERESIS
40 ): selector.NumberSelector(
41 selector.NumberSelectorConfig(
42 mode=selector.NumberSelectorMode.BOX, step=
"any"
45 vol.Optional(CONF_LOWER): selector.NumberSelector(
46 selector.NumberSelectorConfig(
47 mode=selector.NumberSelectorMode.BOX, step=
"any"
50 vol.Optional(CONF_UPPER): selector.NumberSelector(
51 selector.NumberSelectorConfig(
52 mode=selector.NumberSelectorMode.BOX, step=
"any"
55 vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
56 selector.EntitySelectorConfig(domain=SENSOR_DOMAIN)
61 CONFIG_SCHEMA = vol.Schema(
63 vol.Required(CONF_NAME): selector.TextSelector(),
65 ).extend(OPTIONS_SCHEMA.schema)
69 CONFIG_SCHEMA, preview=
"threshold", validate_user_input=_validate_mode
75 OPTIONS_SCHEMA, preview=
"threshold", validate_user_input=_validate_mode
81 """Handle a config or options flow for Threshold."""
83 config_flow = CONFIG_FLOW
84 options_flow = OPTIONS_FLOW
87 """Return config entry title."""
88 name: str = options[CONF_NAME]
93 """Set up preview WS API."""
94 websocket_api.async_register_command(hass, ws_start_preview)
97 @websocket_api.websocket_command(
{
vol.Required("type"):
"threshold/start_preview",
98 vol.Required(
"flow_id"): str,
99 vol.Required(
"flow_type"): vol.Any(
"config_flow",
"options_flow"),
100 vol.Required(
"user_input"): dict,
109 """Generate a preview."""
111 if msg[
"flow_type"] ==
"config_flow":
112 entity_id = msg[
"user_input"][CONF_ENTITY_ID]
113 name = msg[
"user_input"][CONF_NAME]
115 flow_status = hass.config_entries.options.async_get(msg[
"flow_id"])
116 config_entry = hass.config_entries.async_get_entry(flow_status[
"handler"])
119 entity_id = config_entry.options[CONF_ENTITY_ID]
120 name = config_entry.options[CONF_NAME]
123 def async_preview_updated(state: str, attributes: Mapping[str, Any]) ->
None:
124 """Forward config entry state events to websocket."""
125 connection.send_message(
126 websocket_api.event_message(
127 msg[
"id"], {
"attributes": attributes,
"state": state}
134 msg[
"user_input"].
get(CONF_LOWER),
135 msg[
"user_input"].
get(CONF_UPPER),
136 msg[
"user_input"].
get(CONF_HYSTERESIS),
140 preview_entity.hass = hass
142 connection.send_result(msg[
"id"])
143 connection.subscriptions[msg[
"id"]] = preview_entity.async_start_preview(
144 async_preview_updated
146
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_mode(SchemaCommonFlowHandler handler, dict[str, Any] user_input)