Skip to content

Commit 1d646d0

Browse files
MartinHjelmareballoobTheJulianJES
authored
Ask user for Z-Wave RF region if country is missing (#150959)
Co-authored-by: Paulus Schoutsen <[email protected]> Co-authored-by: TheJulianJES <[email protected]>
1 parent 135c80d commit 1d646d0

File tree

3 files changed

+408
-4
lines changed

3 files changed

+408
-4
lines changed

homeassistant/components/zwave_js/config_flow.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from homeassistant.core import HomeAssistant, callback
3636
from homeassistant.data_entry_flow import AbortFlow
3737
from homeassistant.exceptions import HomeAssistantError
38+
from homeassistant.helpers import selector
3839
from homeassistant.helpers.hassio import is_hassio
3940
from homeassistant.helpers.service_info.hassio import HassioServiceInfo
4041
from homeassistant.helpers.service_info.usb import UsbServiceInfo
@@ -88,6 +89,8 @@
8889
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: CONF_LR_S2_AUTHENTICATED_KEY,
8990
}
9091

92+
CONF_ADDON_RF_REGION = "rf_region"
93+
9194
EXAMPLE_SERVER_URL = "ws://localhost:3000"
9295
ON_SUPERVISOR_SCHEMA = vol.Schema({vol.Optional(CONF_USE_ADDON, default=True): bool})
9396
MIN_MIGRATION_SDK_VERSION = AwesomeVersion("6.61")
@@ -103,6 +106,19 @@
103106
"#how-to-migrate-from-one-adapter-to-a-new-adapter-using-z-wave-js-ui"
104107
)
105108

109+
RF_REGIONS = [
110+
"Australia/New Zealand",
111+
"China",
112+
"Europe",
113+
"Hong Kong",
114+
"India",
115+
"Israel",
116+
"Japan",
117+
"Korea",
118+
"Russia",
119+
"USA",
120+
]
121+
106122

107123
def get_manual_schema(user_input: dict[str, Any]) -> vol.Schema:
108124
"""Return a schema for the manual step."""
@@ -195,10 +211,12 @@ def __init__(self) -> None:
195211
self.backup_data: bytes | None = None
196212
self.backup_filepath: Path | None = None
197213
self.use_addon = False
214+
self._addon_config_updates: dict[str, Any] = {}
198215
self._migrating = False
199216
self._reconfigure_config_entry: ZwaveJSConfigEntry | None = None
200217
self._usb_discovery = False
201218
self._recommended_install = False
219+
self._rf_region: str | None = None
202220

203221
async def async_step_install_addon(
204222
self, user_input: dict[str, Any] | None = None
@@ -236,6 +254,21 @@ async def async_step_start_addon(
236254
self, user_input: dict[str, Any] | None = None
237255
) -> ConfigFlowResult:
238256
"""Start Z-Wave JS add-on."""
257+
if self.hass.config.country is None and (
258+
not self._rf_region or self._rf_region == "Automatic"
259+
):
260+
# If the country is not set, we need to check the RF region add-on config.
261+
addon_info = await self._async_get_addon_info()
262+
rf_region: str | None = addon_info.options.get(CONF_ADDON_RF_REGION)
263+
self._rf_region = rf_region
264+
if rf_region is None or rf_region == "Automatic":
265+
# If the RF region is not set, we need to ask the user to select it.
266+
return await self.async_step_rf_region()
267+
if config_updates := self._addon_config_updates:
268+
# If we have updates to the add-on config, set them before starting the add-on.
269+
self._addon_config_updates = {}
270+
await self._async_set_addon_config(config_updates)
271+
239272
if not self.start_task:
240273
self.start_task = self.hass.async_create_task(self._async_start_addon())
241274

@@ -629,6 +662,33 @@ async def async_step_intent_custom(
629662
return await self.async_step_on_supervisor({CONF_USE_ADDON: True})
630663
return await self.async_step_on_supervisor()
631664

665+
async def async_step_rf_region(
666+
self, user_input: dict[str, Any] | None = None
667+
) -> ConfigFlowResult:
668+
"""Handle RF region selection step."""
669+
if user_input is not None:
670+
# Store the selected RF region
671+
self._addon_config_updates[CONF_ADDON_RF_REGION] = self._rf_region = (
672+
user_input["rf_region"]
673+
)
674+
return await self.async_step_start_addon()
675+
676+
schema = vol.Schema(
677+
{
678+
vol.Required("rf_region"): selector.SelectSelector(
679+
selector.SelectSelectorConfig(
680+
options=RF_REGIONS,
681+
mode=selector.SelectSelectorMode.DROPDOWN,
682+
)
683+
),
684+
}
685+
)
686+
687+
return self.async_show_form(
688+
step_id="rf_region",
689+
data_schema=schema,
690+
)
691+
632692
async def async_step_on_supervisor(
633693
self, user_input: dict[str, Any] | None = None
634694
) -> ConfigFlowResult:
@@ -728,7 +788,7 @@ async def async_step_network_type(
728788
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
729789
}
730790

731-
await self._async_set_addon_config(addon_config_updates)
791+
self._addon_config_updates = addon_config_updates
732792
return await self.async_step_start_addon()
733793

734794
# Network already exists, go to security keys step
@@ -799,7 +859,7 @@ async def async_step_configure_security_keys(
799859
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
800860
}
801861

802-
await self._async_set_addon_config(addon_config_updates)
862+
self._addon_config_updates = addon_config_updates
803863
return await self.async_step_start_addon()
804864

805865
data_schema = vol.Schema(
@@ -1004,7 +1064,7 @@ async def async_step_instruct_unplug(
10041064
if user_input is not None:
10051065
if self.usb_path:
10061066
# USB discovery was used, so the device is already known.
1007-
await self._async_set_addon_config({CONF_ADDON_DEVICE: self.usb_path})
1067+
self._addon_config_updates[CONF_ADDON_DEVICE] = self.usb_path
10081068
return await self.async_step_start_addon()
10091069
# Now that the old controller is gone, we can scan for serial ports again
10101070
return await self.async_step_choose_serial_port()
@@ -1136,6 +1196,8 @@ async def async_step_configure_addon_reconfigure(
11361196
CONF_ADDON_LR_S2_AUTHENTICATED_KEY: self.lr_s2_authenticated_key,
11371197
}
11381198

1199+
addon_config_updates = self._addon_config_updates | addon_config_updates
1200+
self._addon_config_updates = {}
11391201
await self._async_set_addon_config(addon_config_updates)
11401202

11411203
if addon_info.state == AddonState.RUNNING and not self.restart_addon:
@@ -1207,7 +1269,7 @@ async def async_step_choose_serial_port(
12071269
"""Choose a serial port."""
12081270
if user_input is not None:
12091271
self.usb_path = user_input[CONF_USB_PATH]
1210-
await self._async_set_addon_config({CONF_ADDON_DEVICE: self.usb_path})
1272+
self._addon_config_updates[CONF_ADDON_DEVICE] = self.usb_path
12111273
return await self.async_step_start_addon()
12121274

12131275
try:

homeassistant/components/zwave_js/strings.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@
113113
"description": "[%key:component::zwave_js::config::step::on_supervisor::description%]",
114114
"title": "[%key:component::zwave_js::config::step::on_supervisor::title%]"
115115
},
116+
"rf_region": {
117+
"title": "Z-Wave region",
118+
"description": "Select the RF region for your Z-Wave network.",
119+
"data": {
120+
"rf_region": "RF region"
121+
},
122+
"data_description": {
123+
"rf_region": "The radio frequency region for your Z-Wave network. This must match the region of your Z-Wave devices."
124+
}
125+
},
116126
"start_addon": {
117127
"title": "Configuring add-on"
118128
},

0 commit comments

Comments
 (0)