diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 2ef881ceaf4..b022a46cbe7 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -1904,8 +1904,12 @@ ENTITY_CONFIG_VALIDATOR: dict[ MQTT_DEVICE_PLATFORM_FIELDS = { ATTR_NAME: PlatformField(selector=TEXT_SELECTOR, required=True), - ATTR_SW_VERSION: PlatformField(selector=TEXT_SELECTOR, required=False), - ATTR_HW_VERSION: PlatformField(selector=TEXT_SELECTOR, required=False), + ATTR_SW_VERSION: PlatformField( + selector=TEXT_SELECTOR, required=False, section="advanced_settings" + ), + ATTR_HW_VERSION: PlatformField( + selector=TEXT_SELECTOR, required=False, section="advanced_settings" + ), ATTR_MODEL: PlatformField(selector=TEXT_SELECTOR, required=False), ATTR_MODEL_ID: PlatformField(selector=TEXT_SELECTOR, required=False), ATTR_CONFIGURATION_URL: PlatformField( @@ -2725,6 +2729,19 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow): for field_key, value in data_schema.schema.items() } + @callback + def get_suggested_values_from_device_data( + self, data_schema: vol.Schema + ) -> dict[str, Any]: + """Get suggestions from device data based on the data schema.""" + device_data = self._subentry_data["device"] + return { + field_key: self.get_suggested_values_from_device_data(value.schema) + if isinstance(value, section) + else device_data.get(field_key) + for field_key, value in data_schema.schema.items() + } + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> SubentryFlowResult: @@ -2754,15 +2771,25 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow): reconfig=True, ) if user_input is not None: - _, errors = validate_user_input(user_input, MQTT_DEVICE_PLATFORM_FIELDS) + new_device_data, errors = validate_user_input( + user_input, MQTT_DEVICE_PLATFORM_FIELDS + ) + if "mqtt_settings" in user_input: + new_device_data["mqtt_settings"] = user_input["mqtt_settings"] if not errors: - self._subentry_data[CONF_DEVICE] = cast(MqttDeviceData, user_input) + self._subentry_data[CONF_DEVICE] = cast(MqttDeviceData, new_device_data) if self.source == SOURCE_RECONFIGURE: return await self.async_step_summary_menu() return await self.async_step_entity() - data_schema = self.add_suggested_values_to_schema( - data_schema, device_data if user_input is None else user_input - ) + data_schema = self.add_suggested_values_to_schema( + data_schema, device_data if user_input is None else user_input + ) + elif self.source == SOURCE_RECONFIGURE: + data_schema = self.add_suggested_values_to_schema( + data_schema, + self.get_suggested_values_from_device_data(data_schema), + ) + return self.async_show_form( step_id=CONF_DEVICE, data_schema=data_schema, diff --git a/homeassistant/components/mqtt/strings.json b/homeassistant/components/mqtt/strings.json index 592ea8686e1..96b5bd15d28 100644 --- a/homeassistant/components/mqtt/strings.json +++ b/homeassistant/components/mqtt/strings.json @@ -134,20 +134,27 @@ "data": { "name": "[%key:common::config_flow::data::name%]", "configuration_url": "Configuration URL", - "sw_version": "Software version", - "hw_version": "Hardware version", "model": "Model", "model_id": "Model ID" }, "data_description": { "name": "The name of the manually added MQTT device.", "configuration_url": "A link to the webpage that can manage the configuration of this device. Can be either a 'http://', 'https://' or an internal 'homeassistant://' URL.", - "sw_version": "The software version of the device. E.g. '2025.1.0'.", - "hw_version": "The hardware version of the device. E.g. 'v1.0 rev a'.", "model": "E.g. 'Cleanmaster Pro'.", "model_id": "E.g. '123NK2PRO'." }, "sections": { + "advanced_settings": { + "name": "Advanced device settings", + "data": { + "sw_version": "Software version", + "hw_version": "Hardware version" + }, + "data_description": { + "sw_version": "The software version of the device. E.g. '2025.1.0'.", + "hw_version": "The hardware version of the device. E.g. 'v1.0 rev a'." + } + }, "mqtt_settings": { "name": "MQTT settings", "data": { diff --git a/tests/components/mqtt/test_config_flow.py b/tests/components/mqtt/test_config_flow.py index 2177a7de8e1..12f77a95c48 100644 --- a/tests/components/mqtt/test_config_flow.py +++ b/tests/components/mqtt/test_config_flow.py @@ -4073,7 +4073,7 @@ async def test_subentry_reconfigure_update_device_properties( result["flow_id"], user_input={ "name": "Beer notifier", - "sw_version": "1.1", + "advanced_settings": {"sw_version": "1.1"}, "model": "Beer bottle XL", "model_id": "bn003", "configuration_url": "https://example.com",