1 """Config flow for 1-Wire component."""
3 from __future__
import annotations
5 from copy
import deepcopy
8 import voluptuous
as vol
24 DEVICE_SUPPORT_OPTIONS,
26 INPUT_ENTRY_CLEAR_OPTIONS,
27 INPUT_ENTRY_DEVICE_SELECTION,
28 OPTION_ENTRY_DEVICE_OPTIONS,
29 OPTION_ENTRY_SENSOR_PRECISION,
30 PRECISION_MAPPING_FAMILY_28,
32 from .onewirehub
import CannotConnect, OneWireHub
34 DATA_SCHEMA = vol.Schema(
36 vol.Required(CONF_HOST, default=DEFAULT_HOST): str,
37 vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
42 async
def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
43 """Validate the user input allows us to connect.
45 Data has the keys from DATA_SCHEMA with values provided by the user.
50 host = data[CONF_HOST]
51 port = data[CONF_PORT]
53 await hub.connect(host, port)
56 return {
"title": host}
60 """Handle 1-Wire config flow."""
65 """Initialize 1-Wire config flow."""
66 self.onewire_config: dict[str, Any] = {}
69 self, user_input: dict[str, Any] |
None =
None
70 ) -> ConfigFlowResult:
71 """Handle 1-Wire config flow start.
73 Let user manually input configuration.
75 errors: dict[str, str] = {}
80 CONF_HOST: user_input[CONF_HOST],
81 CONF_PORT: user_input[CONF_PORT],
85 self.onewire_config.
update(user_input)
90 errors[
"base"] =
"cannot_connect"
93 title=info[
"title"], data=self.onewire_config
98 data_schema=DATA_SCHEMA,
105 config_entry: ConfigEntry,
106 ) -> OnewireOptionsFlowHandler:
107 """Get the options flow for this handler."""
112 """Handle OneWire Config options."""
114 configurable_devices: dict[str, str]
115 """Mapping of the configurable devices.
120 devices_to_configure: dict[str, str]
121 """Mapping of the devices selected for configuration.
127 """Friendly name of the currently selected device."""
129 def __init__(self, config_entry: ConfigEntry) ->
None:
130 """Initialize options flow."""
134 self, user_input: dict[str, Any] |
None =
None
135 ) -> ConfigFlowResult:
136 """Manage the options."""
137 device_registry = dr.async_get(self.hass)
140 for device
in dr.async_entries_for_config_entry(
143 if device.name
and device.name[0:2]
in DEVICE_SUPPORT_OPTIONS
147 return self.
async_abortasync_abort(reason=
"no_configurable_devices")
152 self, user_input: dict[str, Any] |
None =
None
153 ) -> ConfigFlowResult:
154 """Select what devices to configure."""
156 if user_input
is not None:
157 if user_input.get(INPUT_ENTRY_CLEAR_OPTIONS):
161 selected_devices: list[str] = (
162 user_input.get(INPUT_ENTRY_DEVICE_SELECTION)
or []
167 for friendly_name
in selected_devices
171 errors[
"base"] =
"device_not_selected"
174 step_id=
"device_selection",
175 data_schema=vol.Schema(
178 INPUT_ENTRY_CLEAR_OPTIONS,
182 INPUT_ENTRY_DEVICE_SELECTION,
184 description=
"Multiselect with list of devices to choose from",
197 self, user_input: dict[str, Any] |
None =
None
198 ) -> ConfigFlowResult:
199 """Config precision option for device."""
200 if user_input
is not None:
207 data_schema = vol.Schema(
210 OPTION_ENTRY_SENSOR_PRECISION,
212 onewire_id, OPTION_ENTRY_SENSOR_PRECISION,
"temperature"
214 ): vol.In(PRECISION_MAPPING_FAMILY_28),
219 step_id=
"configure_device",
220 data_schema=data_schema,
221 description_placeholders={
"sensor_id": self.current_device},
226 if entry.name_by_user:
227 return f
"{entry.name_by_user} ({onewire_id})"
231 """Get current list of sensors that are configured."""
232 configured_sensors = self.
optionsoptions.
get(OPTION_ENTRY_DEVICE_OPTIONS)
233 if not configured_sensors:
238 if onewire_id
in configured_sensors
242 """Get current value for setting."""
243 if entry_device_options := self.
optionsoptions.
get(OPTION_ENTRY_DEVICE_OPTIONS):
244 if device_options := entry_device_options.get(device_id):
245 return device_options.get(setting)
249 """Update the global config with the new options for the current device."""
250 options: dict[str, dict[str, Any]] = self.
optionsoptions.setdefault(
251 OPTION_ENTRY_DEVICE_OPTIONS, {}
255 device_options: dict[str, Any] = options.setdefault(onewire_id, {})
256 if onewire_id[0:2] ==
"28":
257 device_options[OPTION_ENTRY_SENSOR_PRECISION] = user_input[
258 OPTION_ENTRY_SENSOR_PRECISION
261 self.
optionsoptions.
update({OPTION_ENTRY_DEVICE_OPTIONS: options})
OnewireOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_device_selection(self, dict[str, Any]|None user_input=None)
list[str] _get_current_configured_sensors(self)
None _update_device_options(self, dict[str, Any] user_input)
Any _get_current_setting(self, str device_id, str setting, Any default)
None __init__(self, ConfigEntry config_entry)
ConfigFlowResult async_step_configure_device(self, dict[str, Any]|None user_input=None)
str _get_device_friendly_name(DeviceEntry entry, str onewire_id)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
web.Response get(self, web.Request request, str config_key)
IssData update(pyiss.ISS iss)
dict[str, str] validate_input(HomeAssistant hass, dict[str, Any] data)