diff --git a/homeassistant/components/mqtt/config_flow.py b/homeassistant/components/mqtt/config_flow.py index 1f317d9f743..2bbfd9e3515 100644 --- a/homeassistant/components/mqtt/config_flow.py +++ b/homeassistant/components/mqtt/config_flow.py @@ -465,7 +465,7 @@ class PlatformField: required: bool validator: Callable[..., Any] error: str | None = None - default: str | int | bool | vol.Undefined = vol.UNDEFINED + default: str | int | bool | None | vol.Undefined = vol.UNDEFINED is_schema_default: bool = False exclude_from_reconfig: bool = False conditions: tuple[dict[str, Any], ...] | None = None @@ -515,6 +515,7 @@ COMMON_ENTITY_FIELDS = { required=False, validator=str, exclude_from_reconfig=True, + default=None, ), CONF_ENTITY_PICTURE: PlatformField( selector=TEXT_SELECTOR, required=False, validator=cv.url, error="invalid_url" @@ -1324,7 +1325,10 @@ def data_schema_from_fields( vol.Required(field_name, default=field_details.default) if field_details.required else vol.Optional( - field_name, default=field_details.default + field_name, + default=field_details.default + if field_details.default is not None + else vol.UNDEFINED, ): field_details.selector(component_data_with_user_input) # type: ignore[operator] if field_details.custom_filtering else field_details.selector @@ -1375,12 +1379,14 @@ def data_schema_from_fields( @callback def subentry_schema_default_data_from_fields( data_schema_fields: dict[str, PlatformField], + component_data: dict[str, Any], ) -> dict[str, Any]: """Generate custom data schema from platform fields or device data.""" return { key: field.default for key, field in data_schema_fields.items() if field.is_schema_default + or (field.default is not vol.UNDEFINED and key not in component_data) } @@ -2206,7 +2212,7 @@ class MQTTSubentryFlowHandler(ConfigSubentryFlow): for component_data in self._subentry_data["components"].values(): platform = component_data[CONF_PLATFORM] subentry_default_data = subentry_schema_default_data_from_fields( - PLATFORM_ENTITY_FIELDS[platform] + PLATFORM_ENTITY_FIELDS[platform] | COMMON_ENTITY_FIELDS, component_data ) component_data.update(subentry_default_data) diff --git a/tests/components/mqtt/common.py b/tests/components/mqtt/common.py index d811b601036..4e402046e2c 100644 --- a/tests/components/mqtt/common.py +++ b/tests/components/mqtt/common.py @@ -87,6 +87,7 @@ MOCK_SUBENTRY_NOTIFY_COMPONENT2 = { MOCK_SUBENTRY_NOTIFY_COMPONENT_NO_NAME = { "5269352dd9534c908d22812ea5d714cd": { "platform": "notify", + "name": None, "command_topic": "test-topic", "command_template": "{{ value }}", "entity_picture": "https://example.com/5269352dd9534c908d22812ea5d714cd",