1 """Config flow for Mold indicator."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
6 from typing
import Any, cast
8 import voluptuous
as vol
16 SchemaCommonFlowHandler,
17 SchemaConfigFlowHandler,
32 CONF_CALIBRATION_FACTOR,
39 from .sensor
import MoldIndicator
43 handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
45 """Validate already existing entry."""
46 handler.parent_handler._async_abort_entries_match({**handler.options, **user_input})
47 if user_input[CONF_CALIBRATION_FACTOR] == 0.0:
52 DATA_SCHEMA_OPTIONS = vol.Schema(
60 DATA_SCHEMA_CONFIG = vol.Schema(
62 vol.Required(CONF_NAME, default=DEFAULT_NAME):
TextSelector(),
65 domain=Platform.SENSOR, device_class=SensorDeviceClass.TEMPERATURE
70 domain=Platform.SENSOR, device_class=SensorDeviceClass.HUMIDITY
75 domain=Platform.SENSOR, device_class=SensorDeviceClass.TEMPERATURE
79 ).extend(DATA_SCHEMA_OPTIONS.schema)
84 schema=DATA_SCHEMA_CONFIG,
85 validate_user_input=validate_input,
86 preview=
"mold_indicator",
92 validate_user_input=validate_input,
93 preview=
"mold_indicator",
99 """Handle a config flow for Mold indicator."""
101 config_flow = CONFIG_FLOW
102 options_flow = OPTIONS_FLOW
105 """Return config entry title."""
106 return cast(str, options[CONF_NAME])
110 """Set up preview WS API."""
111 websocket_api.async_register_command(hass, ws_start_preview)
114 @websocket_api.websocket_command(
{
vol.Required("type"):
"mold_indicator/start_preview",
115 vol.Required(
"flow_id"): str,
116 vol.Required(
"flow_type"): vol.Any(
"config_flow",
"options_flow"),
117 vol.Required(
"user_input"): dict,
126 """Generate a preview."""
128 if msg[
"flow_type"] ==
"config_flow":
129 flow_status = hass.config_entries.flow.async_get(msg[
"flow_id"])
130 flow_sets = hass.config_entries.flow._handler_progress_index.get(
131 flow_status[
"handler"]
134 config_entry = hass.config_entries.async_get_entry(flow_status[
"handler"])
135 indoor_temp = msg[
"user_input"].
get(CONF_INDOOR_TEMP)
136 outdoor_temp = msg[
"user_input"].
get(CONF_OUTDOOR_TEMP)
137 indoor_hum = msg[
"user_input"].
get(CONF_INDOOR_HUMIDITY)
138 name = msg[
"user_input"].
get(CONF_NAME)
140 flow_status = hass.config_entries.options.async_get(msg[
"flow_id"])
141 config_entry = hass.config_entries.async_get_entry(flow_status[
"handler"])
144 indoor_temp = config_entry.options[CONF_INDOOR_TEMP]
145 outdoor_temp = config_entry.options[CONF_OUTDOOR_TEMP]
146 indoor_hum = config_entry.options[CONF_INDOOR_HUMIDITY]
147 name = config_entry.options[CONF_NAME]
150 def async_preview_updated(state: str, attributes: Mapping[str, Any]) ->
None:
151 """Forward config entry state events to websocket."""
152 connection.send_message(
153 websocket_api.event_message(
154 msg[
"id"], {
"attributes": attributes,
"state": state}
161 hass.config.units
is METRIC_SYSTEM,
165 msg[
"user_input"].
get(CONF_CALIBRATION_FACTOR),
168 preview_entity.hass = hass
170 connection.send_result(msg[
"id"])
171 connection.subscriptions[msg[
"id"]] = preview_entity.async_start_preview(
172 async_preview_updated
174
str async_config_entry_title(self, Mapping[str, Any] options)
None async_setup_preview(HomeAssistant hass)
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_input(SchemaCommonFlowHandler handler, dict[str, Any] user_input)